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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值