计蒜客- AC Challenge 状压dp
Posted the-way-of-cas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计蒜客- 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 imes 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 imes 5 + 6 = 111×5+6=11 points.
On the second minute, Dlsj submitted the second problem, and get 2×4+5=132 imes 4 + 5 = 132×4+5=13 points.
On the third minute, Dlsj submitted the third problem, and get 3×3+4=133 imes 3 + 4 = 133×3+4=13 points.
On the forth minute, Dlsj submitted the forth problem, and get 4×2+3=114 imes 2 + 3 = 114×2+3=11 points.
On the fifth minute, Dlsj submitted the fifth problem, and get 5×1+2=75 imes 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; }
以上是关于计蒜客- AC Challenge 状压dp的主要内容,如果未能解决你的问题,请参考以下文章
计蒜客 Flashing Fluorescents(状压DP)
ICPC2018南京网络赛 AC Challenge(一维状压dp)
ACM-ICPC 2018 南京赛区网络预赛 E AC Challenge 状压DP