码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 1079 E - The Unbearable Lightness of Weights

时间:2018-11-22 02:37:53      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:printf   ble   ack   sig   weight   nbsp   unsigned   div   1.0   

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", 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;
}

 

Codeforces 1079 E - The Unbearable Lightness of Weights

标签:printf   ble   ack   sig   weight   nbsp   unsigned   div   1.0   

原文地址:https://www.cnblogs.com/widsom/p/9998721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!