首页 > 代码库 > [HDOJ5900]QSC and Master(区间dp)
[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 >> a13 #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 << 121 #define rrt rt << 1 | 122 #define pi 3.1415926535923 #define RT return24 #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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。