1. 程式人生 > >Codeforces Round #213 (Div. 1) B - Free Market 思維+揹包 好題

Codeforces Round #213 (Div. 1) B - Free Market 思維+揹包 好題

B - Free Market

思路:這個題怎麼說呢,迷惑性很大,題目裡說了交換了兩個集合的時候如果有相同元素不能交換,感覺如果沒

這句話能很快寫出來, 其實當交換的兩個集合有重複元素的時候只要交換那些不重複的就好啦,這樣就相當於能

交換兩個有交的集合,那麼對於兩個集合S1, S2我們不需要去考慮有沒有交只需要考慮sumS1 + d >= sumS2是否

滿足條件。所以求個揹包從前往後貪心就好啦。

 

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define
mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ull unsigned long long using namespace std; const int N = 50 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7
; const double eps = 1e-8; int n, d, c[N], all, day; bitset<500001> dp; int main() { scanf("%d%d", &n, &d); dp[0] = 1; for(int i = 1; i <= n; i++) { int x; scanf("%d", &x); dp |= dp << x; } while(1) { int nxt = -1;
for(int i = all+1; i <= all+d && i <= 500000; i++) { if(dp[i]) { nxt = i; } } if(nxt == -1) break; day++; all = nxt; } printf("%d %d\n", all, day); return 0; } /* */