码迷,mamicode.com
首页 > 其他好文 > 详细

[HDOJ5900]QSC and Master(区间dp)

时间:2016-09-19 18:06:42      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: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)

标签:

原文地址:http://www.cnblogs.com/vincentX/p/5885906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!