计蒜客- AC Challenge 状压dp

题目链接:https://nanti.jisuanke.com/t/30994

Dlsj is competing in a contest with n(0<n≤20)n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems.

However, he can submit iii-th problem if and only if he has submitted (and passed, of course) sis_isi? problems, the pi,1p_{i, 1}pi,1?-th, pi,2p_{i, 2}pi,2?-th, ........., pi,sip_{i, s_i}pi,si??-th problem before.(0<pi,j≤n,0<j≤si,0<i≤n)(0 < p_{i, j} \le n,0 < j \le s_i,0 < i \le n)(0<pi,j?≤n,0<j≤si?,0<i≤n) After the submit of a problem, he has to wait for one minute, or cooling down time to submit another problem. As soon as the cooling down phase ended, he will submit his solution (and get "Accepted" of course) for the next problem he selected to solve or he will say that the contest is too easy and leave the arena.

"I wonder if I can leave the contest arena when the problems are too easy for me."
"No problem."
—— CCF NOI Problem set

If he submits and passes the iii-th problem on ttt-th minute(or the ttt-th problem he solve is problem iii), he can get t×ai+bit \times a_i + b_it×ai?+bi? points. (∣ai∣,∣bi∣≤109)(|a_i|, |b_i| \le 10^9)(∣ai?∣,∣bi?∣≤109).

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, nnn, which is the number of problems.

Then follows nnn lines, the iii-th line contains si+3s_i + 3si?+3 integers, ai,bi,si,p1,p2,...,psia_i,b_i,s_i,p_1,p_2,...,p_{s_i}ai?,bi?,si?,p1?,p2?,...,psi??as described in the description above.

Output

Output one line with one integer, the maximum number of points he can get in the contest.

Hint

In the first sample.

On the first minute, Dlsj submitted the first problem, and get 1×5+6=111 \times 5 + 6 = 111×5+6=11 points.

On the second minute, Dlsj submitted the second problem, and get 2×4+5=132 \times 4 + 5 = 132×4+5=13 points.

On the third minute, Dlsj submitted the third problem, and get 3×3+4=133 \times 3 + 4 = 133×3+4=13 points.

On the forth minute, Dlsj submitted the forth problem, and get 4×2+3=114 \times 2 + 3 = 114×2+3=11 points.

On the fifth minute, Dlsj submitted the fifth problem, and get 5×1+2=75 \times 1 + 2 = 75×1+2=7 points.

So he can get 11+13+13+11+7=5511+13+13+11+7=5511+13+13+11+7=55 points in total.

In the second sample, you should note that he doesn‘t have to solve all the problems.

样例输入1

5
5 6 0
4 5 1 1
3 4 1 2
2 3 1 3
1 2 1 4

样例输出1

55

样例输入2

1
-100 0 0

样例输出2

0

题意:给n个题目,在不同的时间提交会获得不同的分数,每个题目都有s个前提限制,已经提交的题目中包括前提,该题目才能提交,问可以获得的最高分。思路:最多20到题目,用一个20位的2进制数就可以记录一个状态,例如11010,表示第2,4,5道题目已经提交,该状态可由01010,10010,11000转移而来。   dp[zt]表示该状态下的最优提交方案的得分,输出dp数组的最大值   注意:递归会超时

代码:
#include <iostream>
#include <algorithm>
#include <string>
#define ll long long
#define maxn 22
#define inf 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;

int biao[maxn];
void setbiao(int n) {
    biao[1] = 1;
    for (int i = 2; i <= n; i++) {
        biao[i] = biao[i - 1] * 2;
    }
}
const int maxdp = 1 << 21;
ll dp[maxdp];
ll ans;
struct node_pb
{
    int a, b;
    int qt;
}pro[maxn];
int n;

int count1(int x) {
    int num = 0;
    for (int i = 0; i < n; i++) {
        if (x&(1 << i)) num++;
    }
    return num;
}

/*t:第几分钟 zt:当前已经做的题*/
void res(int t = 0, int zt = 0) {
    if (t > n) return;
    int nxzt;
    t++;
    for (int i = 1; i <= n; i++) {
        if (((biao[i] & zt) == 0) && ((pro[i].qt&zt) == pro[i].qt)) {
            nxzt = zt + biao[i];
            dp[nxzt] = max(dp[zt] + pro[i].a*t + pro[i].b,dp[nxzt]);
            ans = max(ans, dp[nxzt]);
            res(t, nxzt);
        }
    }
}

int main() {
    fio;
    int s, tqt;
    cin >> n;
    setbiao(n);
    for (int i = 1; i <= n; i++) {
        cin >> pro[i].a >> pro[i].b >> s;
        for (int j = 1; j <= s; j++) {
            cin >> tqt;
            pro[i].qt += biao[tqt];
        }
    }
    //res();  //会超时
    memset(dp, -inf, sizeof dp);
    dp[0] = 0;
    int num_1, yzt;
    for (int zt = 1; zt <= (1 << n); zt++) {
        num_1 = count1(zt);
        for (int i = 1; i <= n; i++) {
            yzt = zt - biao[i];
            if ((zt&biao[i])>0 && (yzt&pro[i].qt)==pro[i].qt) {
                dp[zt] = max(dp[yzt] + num_1*pro[i].a+pro[i].b, dp[zt]);
                ans = max(ans, dp[zt]);
            }
        }
    }

    cout << ans << endl;

    return 0;
}

原文地址:https://www.cnblogs.com/the-way-of-cas/p/9574086.html

时间: 2024-08-04 06:00:57

计蒜客- AC Challenge 状压dp的相关文章

计蒜客 Flashing Fluorescents(状压DP)

You have nn lights, each with its own button, in a line. Pressing a light’s button will toggle that light’s state; if the light is on, it will turn off, and if the light is off, it will turn on. The lights change at 1 second timesteps. You can press

poj 1699 Best Sequence(AC自动机+状压DP)

题目链接:poj 1699 Best Sequence 题目大意:给定N个DNA序列,问说最少多长的字符串包含所有序列. 解题思路:AC自动机+状压DP,先对字符串构造AC自动机,然后在dp[s][i]表示匹配了s,移动到节点i时候的最短步数. #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <iostream> #include &

hdu 2825 aC自动机+状压dp

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5640    Accepted Submission(s): 1785 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

hdu 3247 AC自动+状压dp+bfs处理

Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 2382    Accepted Submission(s): 750 Problem Description Great! Your new software is almost finished! The only thing left to

【HDU3341】 Lost&#39;s revenge (AC自动机+状压DP)

Lost's revenge Time Limit: 5000MS Memory Limit: 65535KB 64bit IO Format: %I64d & %I64u Description Lost and AekdyCoin are friends. They always play "number game"(A boring game based on number theory) together. We all know that AekdyCoin is t

HDU 3341 Lost&#39;s revenge(AC自动机+状压DP)

Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4548    Accepted Submission(s): 1274 Problem Description Lost and AekdyCoin are friends. They always play "number game"(A bor

[AC自动机+状压dp] hdu 4534 郑厂长系列故事——新闻净化

题意:中文的题目,意思就是说有很多串,每个串都有权值,权值为999的串必须出现,-999的串必须不出现.权值在-999~999之间. 然后必须出现的串不超过8个.然后给一个全为小写目标串,问最少需要删除多少个字母才能够保证必须出现的串都出现,次数一样保证权值最大.输出次数和权值. 然后根据样例,那些必须出现的串,其实权值是0. 思路: 很明显一开始建自动机构成trie图,但是需要注意的就是mark和sum的更新.个人是把所有中间的节点的sum全部赋值成了-inf. 接着只有8个必须出现的串,所以

[AC自动机+状压dp] hdu 2825 Wireless Password

题意: 给n,m,k ,再给出m个单词 问长度为n的字符串,至少在m个单词中含有k个的组成方案有多少种. 思路: 由于m最大是10,所以可以采取状压的思想 首先建立trie图,在每个单词的结束节点标记一个mark=(1<<id),id为单词的编号 然后需要注意的,对于每个节点,应该顺着fail指针遍历一遍, 把所有的mark取一个并集. 因为就是如果单词出现包含的话,比如 she和he 我拿了she,其实等于两个都拿了. dp[i][j][k]  i步在节点j状态k的方案数 然后就是一个四重循

2018icpc南京网络赛-E AC Challenge(状压+dfs)

题意: n道题,每道题有ai和bi,完成这道题需要先完成若干道题,完成这道题可以得到分数t*ai+bi,其中t是时间 1s, n<=20 思路: 由n的范围状压,状态最多1e6 然后dfs,注意代码中dfs里的剪枝, 对一个状态statu,因为贪心的取最大值就行,所以及时剪枝 代码: 当时写不出来真是菜的活该 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath>