首页 技术 正文
技术 2022年11月14日
0 收藏 673 点赞 2,749 浏览 2905 个字

C – I love sneakers!

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store.
***C – I love sneakers!(动态规划,分组背包)
There are several brands of sneakers that Iserlohn wants to collect, such as Air Jordan and Nike Pro. And each brand has released various products. For the reason that Iserlohn is definitely a sneaker-mania, he desires to buy at least one product for each brand.
Although the fixed price of each product has been labeled, Iserlohn sets values for each of them based on his own tendency. With handsome but limited money, he wants to maximize the total value of the shoes he is going to buy. Obviously, as a collector, he won’t buy the same product twice.
Now, Iserlohn needs you to help him find the best solution of his problem, which means to maximize the total value of the products he can buy. 

Input

Input contains multiple test cases. Each test case begins with three integers 1<=N<=100 representing the total number of products, 1 <= M<= 10000 the money Iserlohn gets, and 1<=K<=10 representing the sneaker brands. The following N lines each represents a product with three positive integers 1<=a<=k, b and c, 0<=b,c<100000, meaning the brand’s number it belongs, the labeled price, and the value of this product. Process to End Of File. 

Output

For each test case, print an integer which is the maximum total value of the sneakers that Iserlohn purchases. Print “Impossible” if Iserlohn’s demands can’t be satisfied. 

Sample Input

5 10000 31 4 62 5 73 4 991 55 772 44 66 

Sample Output

255 题意:一个人去买运动鞋,他手里拥有的钱为m,运动鞋有k种品牌,每种品牌都可能有多双运动鞋,并且对于每种品牌,他都至少要买一双。给出n双运动鞋,运动鞋的品牌号,价格,价值分别为a,b,c。问他用那些钱去买运动鞋,能获得的最大价值是多少。思路:

1. 要求每一组之中至少有一个被选中,和之前的最多有一个稍有区别,需要把题目再次细分。

2. dp[j][w] 表示选定 j 个品牌并且花费w的最大价值,dp[j]w] 为正数表示状态存在,为负数表示状态不存在。

3. dp[j][w] = max(dp[j][w], dp[j][w – s[i].b] +s[i].c);  第j类品牌有选择并且要选择第i件物品。(不选择第i件物品是相等的,所以略过转移方程)

dp[j][w] = max(dp[j][w], dp[j – 1][w- s[i].b] +s[i].c);  第j类品牌前面没有选择并且要选择第i件物品。

4. 由于状态是从 0 到 i 的且 dp[0][w] = 0,其他为 -INF。所以只有当第一类品牌的状态存在时,才能推导出来第二类品牌的存在状态,以此类推。

5. 题目中有 2 个陷阱,一是可能会存在某品牌数量为 0 的情况,另外会存在费用或价格为 0 的情况,所以状态转移方程不能调换。

AC代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x7fffffff using namespace std;
struct ks{
int id;
int b;
int c;
};
ks s[]={};
int dp[][]={};
int number[]={};
bool cmp(ks a1,ks a2){
if(a1.id==a2.id)
return a1.b<a2.b;
else
return a1.id<a2.id;
} int main()
{
freopen("1.txt","r",stdin);
int n,money,k;
int a;
int i,j;
while(cin>>n>>money>>k){
for(i=;i<n;i++){
cin>>s[i].id>>s[i].b>>s[i].c;//录入信息
}
sort(s,s+n,cmp);//按产品号和价格的升序进行排列
for(i=;i<k;i++){
for(j=;j<=money;j++)
dp[i][j]=-INF;
}
dp[][]=;//重置为0,为了第一重循环时的使用
int w;
j=;
for(i=;i<n;i++){
if(i&&s[i-].id!=s[i].id)
j++;//记录鞋的品牌种数
for(w=money;w>=s[i].b;w--)//一定要从后向前推否则会取重的
{//技巧性的实现了至少购买1双鞋子
if(dp[j][w-s[i].b]!=-INF&&dp[j][w-s[i].b]+s[i].c>dp[j][w])//第j种鞋子已经买过了,并且买下第i双鞋子;
dp[j][w]=dp[j][w-s[i].b]+s[i].c;
if(j&&dp[j-][w-s[i].b]!=-INF&&dp[j-][w-s[i].b]+s[i].c>dp[j][w])//第j种鞋子没有买过,并且买下第i双鞋子;
dp[j][w]=dp[j-][w-s[i].b]+s[i].c;
}
}
int ans=-;
for(i=money;i>=;i--){
if(ans<dp[j][i])
ans=dp[j][i];
}
if(ans==-||j<k-)
cout<<"Impossible"<<endl;
else
cout<<ans<<endl;
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844