B. Free Market----暴力+dp
来源:互联网 发布:金庸哪派武功厉害知乎 编辑:程序博客网 时间:2024/06/08 14:32
John Doe has recently found a "Free Market" in his city — that is the place where you can exchange some of your possessions for other things for free.
John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y.
For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, ifs(x) + d < s(y) (s(x) is the total price of items in the set x).
During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in.
The first line contains two space-separated integers n, d (1 ≤ n ≤ 50, 1 ≤ d ≤ 104) — the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≤ ci ≤ 104).
Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set.
3 21 3 10
4 3
3 51 2 3
6 2
10 1000010000 9999 1 10000 10000 10000 1 2 3 4
50010 6
In the first sample John can act like this:
- Take the first item (1 - 0 ≤ 2).
- Exchange the first item for the second one (3 - 1 ≤ 2).
- Take the first item (1 - 0 ≤ 2).
题目链接:http://codeforces.com/contest/364/problem/B
题意:有n个品种的物品,每个物品的价格为c[ i ],每次你可以从你拥有的物品中挑取部分去商店去兑换。兑换有如下限制:
(1)你拿去兑换的物品(不妨设拿去兑换的物品集合为s),都要换成集合s所没有的。比如(a,b)--->(v,a)就是不合法的
(2)你拿去兑换的物品集s1里的价值和cost1与你兑换回来的物品集s2里的价值和cost2需要满足:cost1+d>=cost2
每天你只能去兑换一次。
问通过兑换可以获得的物品集价值和的最大值以及所需的兑换天数
还是看了题解。。。思路很简单,暴力d,看看谁满足条件就可以了,题解一下子就看懂了,为什么我当初没有想到。。。
http://blog.csdn.net/u011645923/article/details/38797767
代码:
#include <cstdio>#include <cstring>#include <iostream>using namespace std;int n,d;int dp[700000];int main(){scanf("%d%d",&n,&d); int sum=0; memset(dp,0,sizeof(dp)); dp[0]=1; for(int i=1;i<=n;i++){ int x; scanf("%d",&x); sum+=x; for(int j=sum;j>=x;j--){ if(dp[j-x]){ dp[j]=1; } } } int ans=0; sum=0; while(1){ sum+=d;//这一次的状态上限,暴力 int pos=-1; for(int i=0;i<d;i++){ if(dp[sum-i]){//该次最大可到达的状态 pos=i;//记录位置 break; } } if(pos==-1){ sum-=d; break; } sum-=pos; ans++; } printf("%d %d\n",sum,ans);return 0;}
- B. Free Market----暴力+dp
- codeforces 364B Free Market dp
- CodeForces 364 B. Free Market
- cf-364B-Free Market
- test2 Problem B. Market (背包dp)
- B. Working out----暴力dp
- bzoj1578Stock Market(dp)
- [背包 贪心] Codeforces #365D. Free Market
- codeforces 294B Shaass and Bookshelf (暴力dp)
- [简单DP] uva12951 Stock Market
- Market
- Market
- Market
- Market
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)-B - Bear and Compressing-暴力DP
- CF 629 A组合 B暴力 Cdp D线段树优化DP
- Codeforces Round #367 (Div. 2) A(暴力) B(二分查找) C(DP) D(01字典树)
- TopCoder 636 B 【暴力】
- IE中Active控件实现自动下载注册(基于Tomcat服务器)
- CDQZ Challenge 4
- vue.js学习笔记-6
- 0810记录补充
- 2017多校训练6-1003:Inversion
- B. Free Market----暴力+dp
- Flask
- MySQL分布式中间件:MyCAT
- 构造函数简单示意
- 工作小结
- 如何安装emmet?
- Hibernate核心接口和工作原理
- hdu 6106 Classes
- 简单顶点漫反射