题目链接: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;
}
本文详细解析了一道ACM竞赛题目中的压状DP算法实现过程,通过具体实例介绍了如何利用状态压缩的方法来求解最大得分问题。文章提供了一份完整的C++代码示例,并解释了关键步骤。
684

被折叠的 条评论
为什么被折叠?



