Codeforces 1079 E - The Unbearable Lightness of Weights

Posted widsom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 1079 E - The Unbearable Lightness of Weights相关的知识,希望对你有一定的参考价值。

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);
#define fopen 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);
    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
", ans);
    return 0;
}

 

以上是关于Codeforces 1079 E - The Unbearable Lightness of Weights的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 1325D - Ehab the Xorcist构造+思维

CodeForces 1084D The Fair Nut and the Best Path

Codeforces 1079D Barcelonian Distance(计算几何)

Codeforces 1059E. Split the Tree

Codeforces1325D Ehab the Xorcist

Codeforces Round #628 (Div. 2) D. Ehab the Xorcist(异或,思维题)