HDU5900-QSC and Master(区间dp)
来源:互联网 发布:360游戏优化器独立版 编辑:程序博客网 时间:2024/05/22 03:59
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=5900
思路
区间dp,这道题有个坑点就是比如3 4 8 9,比如4,8选掉了,3和9就变成了相邻的
状态表示:d[i][j] = 区间[i, j]能获得的最大值
转移:
1. d[i][j] = max(d[i][j], d[i][k] + d[k + 1][j])
2. 若a[i]和a[j]不互质,并且中间的数全部已经选掉了:d[i][j] = max(d[i][j], (LL)(d[i + 1][j - 1] + a[i].second + a[j].second))
代码
#include <iostream>#include <cstring>#include <stack>#include <vector>#include <set>#include <map>#include <cmath>#include <queue>#include <sstream>#include <iomanip>#include <fstream>#include <cstdio>#include <cstdlib>#include <climits>#include <deque>#include <bitset>#include <algorithm>using namespace std;#define PI acos(-1.0)#define LL long long#define PII pair<int, int>#define PLL pair<LL, LL>#define mp make_pair#define IN freopen("in.txt", "r", stdin)#define OUT freopen("out.txt", "wb", stdout)#define scan(x) scanf("%d", &x)#define scan2(x, y) scanf("%d%d", &x, &y)#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)#define sqr(x) (x) * (x)const int maxn = 305;LL d[maxn][maxn], sum[maxn];PII a[maxn];int n;LL solve() { memset(d, 0, sizeof(d)); a[0].first = 1; a[0].second = 0; for (int l = 2; l <= n; l++) { for (int i = 1; i <= n; i++) { int j = i + l - 1; if (j > n) continue; for (int k = i; k < j; k++) { d[i][j] = max(d[i][j], d[i][k] + d[k + 1][j]); } if (__gcd(a[i].first, a[j].first) > 1) { if (j == i + 1) d[i][j] = max(d[i][j], (LL)(a[i].second + a[j].second)); else if (d[i + 1][j - 1] == sum[j - 1] - sum[i]) d[i][j] = max(d[i][j], (LL)(d[i + 1][j - 1] + a[i].second + a[j].second)); } } } return d[1][n];}int main() { int T; scan(T); while (T--) { scan(n); sum[0] = 0; for (int i = 1; i <= n; i++) scan(a[i].first); for (int i = 1; i <= n; i++) { scan(a[i].second); sum[i] = sum[i - 1] + a[i].second; } printf("%lld\n", solve()); } return 0;}
0 0
- HDU5900-QSC and Master(区间dp)
- hdu5900 QSC and Master(区间dp)
- hdu5900 QSC and Master(区间DP)
- HDU5900 QSC and Master(区间dp)
- HDU5900 QSC and Master(区间dp)
- HDU5900 QSC and Master(区间dp)
- 【HDU5900】【区间DP】QSC and master 题解
- HDU5900 QSC and Master(区间DP)2016 ACM/ICPC Asia Regional Shenyang Online
- HDU5900-QSC and Master
- hdu5900 QSC and Master
- HDU5900-QSC and Master
- 1008 QSC and Master (区间dp)
- QSC and Master 区间dp
- 【HDU5900 2016 ACM ICPC Asia Regional Shenyang Online I】【区间DP】QSC and Master 相邻互质取数最大取值.cpp
- HDU 5900 QSC and Master(区间DP)
- HDU-5900-QSC and Master(区间DP)
- hdu 5900 QSC and Master (区间dp)
- HDU 5900 QSC and Master(区间DP)
- SSH 连接VirtualBox虚拟机中——Ubuntu16.04.1
- LINUX C——第七章Platform总线
- Hello JNI
- Android获取当前活动(最顶层)的activity
- 解决安卓线程内部返回数据问题
- HDU5900-QSC and Master(区间dp)
- 《js高级程序设计》之最佳实践(笔记)
- 哪些知识值得关注
- 我的MYSQL学习心得(二) 数据类型宽度
- [NOIP2009]最优贸易(spfa)
- Sum of Two Integers
- 我的MYSQL学习心得(三) 查看字段长度
- Android学习之Activity与Service进行通信的三种方式
- python 文件操作read(),readline(),readlines()