Codeforces 1079 E - The Unbearable Lightness of Weights
阿新 • • 發佈:2018-11-22
E - The Unbearable Lightness of Weights
思路:
分組揹包dp
每組最多隻能選一個
一些優化可以快很多
程式碼:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair#define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<pii, int> #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #definefopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 105, M = 1e4 + 5; int cnt[N]; int dp[N][M]; int main() { int n, a; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a), cnt[a]++; dp[0][0] = 1; int dif = 0, tot = 0, sum = 0; for (int i = 1; i < N; i++) { if(cnt[i]) { tot += cnt[i]; sum += cnt[i]*i; for (int j = sum; j >= i; j--) { for (int k = 1; k <= cnt[i]; k++) { if(j < k*i) break; for (int l = k; l <= tot; l++) { dp[l][j] = min(dp[l][j]+dp[l-k][j-k*i], 2); } } } dif++; } } if(dif <= 2) return 0*printf("%d\n", n); int ans = 1; for (int i = 1; i < N; i++) { if(cnt[i]) { for (int j = 1; j <= cnt[i]; j++) if(dp[j][j*i] == 1) ans = max(ans, j); } } printf("%d\n", ans); return 0; }