题意:有n个品种的物品,每个物品的价格为c[ i ],每次你可以从你拥有的物品中挑取部分去商店去兑换。兑换有如下限制:
(1)你拿去兑换的物品(不妨设拿去兑换的物品集合为s),都要换成集合s所没有的。比如(a,b)--->(v,a)就是不合法的
(2)你拿去兑换的物品集s1里的价值和cost1与你兑换回来的物品集s2里的价值和cost2需要满足:cost1+d>=cost2
每天你只能去兑换一次。
问通过兑换可以获得的物品集价值和的最大值以及所需的兑换天数
题目链接:http://codeforces.com/contest/365/problem/D
题解:首先背包枚举下所有可能的状态,然后w表示当前可以到达最大的价值,枚举w+1~w+d可行的状态,转移到最大的可行状态,这里有一点值得思考,为什么转移时会满足条件1?因为从一个低状态w到另一个高状态w+i(i>=1&&i<=d),因为每种状态我们都只是每个物品最多放1次所到达的,定义状态1: w = a[p_1] + a[p_2] + ... + a[p_k], 状态2: w + i = a[q_1]+a[q_2] + ... + a[q_k'], 2个相同物品,比如a[p_i] = a[q_j], 违反了规则1, 我们完全可以保持a[p_i]在w状态里不动, 也就是说相同的物品从状态1(低状态)到状态2(高状态)里若存在相同元素,该元素不需调整。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define x0 x0___
#define y0 y0___
#define pb push_back
#define SZ(X) ((int)X.size())
#define mp make_pair
#define Fi first
#define Se second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define ALL(X) X.begin(),X.end()
#define RALL(X) X.rbegin(),X.rend()
#define rep(i,j,k) for(int i = j;i <= k;i ++)
#define per(i,j,k) for(int i = j;i >= k;i --)
#define mem(a,p) memset(a,p,sizeof(a))
const ll MOD = 1E9 + 7;
ll qmod(ll a,ll b,ll c) {ll res=1;a%=c; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%c;a=a*a%c;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
template<typename T, typename S>
void upmax(T& a,S b){if(a<b) a=b;}
template<typename T, typename S>
void upmin(T& a,S b){if(a>b) a=b;}
void gettle() {while(1);}
void getre() {int t=0;t/=t;}
/
/
/
/
const int N = 57;
int dp[N * 10000];
int a[N];
int main()
{
int n;
int d;
int s = 0;
scanf("%d %d", &n, &d);
dp[0] = 1;
rep (i, 1, n) {
scanf("%d", &a[i]);
s += a[i];
per (j, s, a[i]) {
dp[j] |= dp[j - a[i]];
}
}
int w, day;
w = day = 0;
while(true) {
bool fg = 0;
per (i, w + d, w + 1) {
if(dp[i]) {
fg = 1;
day ++;
w = i;
break;
}
}
if(!fg) break;
}
return !printf("%d %d\n", w, day);
}