(南京区域资格赛)E-AC Challenge

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/OscaronMar/article/details/82290280

题目链接:https://nanti.jisuanke.com/t/30994
E-AC Challenge

这里写图片描述
Your task is to calculate the maximum number of points he can get in the contest.
Input
The first line of input contains an integer, n, which is the number of problems.
这里写图片描述
Output
Output one line with one integer, the maximum number of points he can get in the
contest.
Sample Input
5
5 6 0
4 5 1 1
3 4 1 2
2 3 1 3

压状DP

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
struct Data{
    long long a, b, t; 
    int cnt;
};
Data data[30];
long long dp[1<<22];
int cnt[1 << 22];
void init(){
    memset(cnt, 0, sizeof(cnt));
    memset(dp, -INF, sizeof(dp));
    for(int i = 0; i < 1<<(21); i++){
        int n = i, s;
        for(s = 0; n; ++s){
            n &= (n-1);
        }
        cnt[i] = s;
    } 
}
int main(){
    init();
    int n, a;
    long long MAX = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%lld%lld%d", &data[i].a, &data[i].b, &data[i].cnt);
        data[i].t = 0;
        for(int j = 1; j <= data[i].cnt; j++){
            scanf("%d", &a);
            data[i].t = data[i].t | 1ll*( 1 << (a-1));
        }
    }
    dp[0] = 0;
    for(int i = 1; i < (1 << (n+1)); i++){
        for(int j = 1; j <= n; j++){
            if( i & (1<<(j-1))){
                long long tmp = i^(1 << (j-1));
                if((tmp & data[j].t) == data[j].t){
                    dp[i] = max(dp[i], dp[tmp]+cnt[i]*data[j].a+data[j].b);
                }
            }
            MAX = max(MAX, dp[i]);
        }
    }
    printf("%lld\n", MAX);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/OscaronMar/article/details/82290280