[HDOJ5900]QSC and Master(区间dp)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HDOJ5900]QSC and Master(区间dp)相关的知识,希望对你有一定的参考价值。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5900

巧妙的地方在于第四个转移,假如dp(i,j)区间内所有的都取了,那状态的值应该是s(j)-s(i-1)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define fr first
 4 #define sc second
 5 #define cl clear
 6 #define BUG puts("here!!!")
 7 #define W(a) while(a--)
 8 #define pb(a) push_back(a)
 9 #define Rint(a) scanf("%d", &a)
10 #define Rll(a) scanf("%I64d", &a)
11 #define Rs(a) scanf("%s", a)
12 #define Cin(a) cin >> a
13 #define FRead() freopen("in", "r", stdin)
14 #define FWrite() freopen("out", "w", stdout)
15 #define Rep(i, len) for(int i = 0; i < (len); i++)
16 #define For(i, a, len) for(int i = (a); i < (len); i++)
17 #define Cls(a) memset((a), 0, sizeof(a))
18 #define Clr(a, x) memset((a), (x), sizeof(a))
19 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
20 #define lrt rt << 1
21 #define rrt rt << 1 | 1
22 #define pi 3.14159265359
23 #define RT return
24 #define lowbit(x) x & (-x)
25 #define onecnt(x) __builtin_popcount(x)
26 typedef long long LL;
27 typedef long double LD;
28 typedef unsigned long long ULL;
29 typedef pair<int, int> pii;
30 typedef pair<string, int> psi;
31 typedef pair<LL, LL> pll;
32 typedef map<string, int> msi;
33 typedef vector<int> vi;
34 typedef vector<LL> vl;
35 typedef vector<vl> vvl;
36 typedef vector<bool> vb;
37 
38 const int maxn = 330;
39 int n;
40 pii a[maxn];
41 LL s[maxn];
42 LL dp[maxn][maxn];
43 
44 signed main() {
45   //FRead();
46   int T;
47   Rint(T);
48   W(T) {
49     Cls(s);
50     Rint(n);
51     For(i, 1, n+1) Rint(a[i].first);
52     For(i, 1, n+1) {
53       Rint(a[i].second);
54       s[i] = s[i-1] + a[i].second;
55     }
56     Cls(dp);
57     For(k, 2, n+1) {
58       For(i, 1, n-k+2) {
59         int j = i + k - 1;
60         For(p, i, j) dp[i][j] = max(dp[i][j], dp[i][p]+dp[p+1][j]);
61         if(__gcd(a[i].first, a[j].first) > 1) {
62           if(k == 2) dp[i][j] = a[i].second + a[j].second;
63           else {
64             if(s[j-1] - s[i] == dp[i+1][j-1]) {
65               dp[i][j] = max(dp[i][j], dp[i+1][j-1]+a[i].second+a[j].second);
66             }
67           }
68           dp[i][j] = max(dp[i][j], max(dp[i][j-1], dp[i+1][j]));
69         }
70       }
71     }
72     printf("%I64d\n", dp[1][n]);
73   }
74   RT 0;
75 }

 

以上是关于[HDOJ5900]QSC and Master(区间dp)的主要内容,如果未能解决你的问题,请参考以下文章

(HDU 5900)QSC and Master(区间DP)

hdu 5900 QSC and Master 区间dp

HDU5900 QSC and Master(区间DP + 最小费用最大流)

HDU 5900 - QSC and Master [ DP ]

Hdu 5900 QSC and Master

HDU5900-QSC and Master-区间DP+区间覆盖DP