HDU 5900 QSC and Master 区间DP
QSC and Master
Enter from the north gate of Northeastern University,You are facing the main building of Northeastern University.Ninety-nine percent of the students have not been there,It is said that there is a monster in it.
QSCI am a curious NEU_ACMer,This is the story he told us.
It’s a certain period,QSCI am in a dark night, secretly sneaked into the East Building,hope to see the master.After a serious search,He finally saw the little master in a dark corner. The master said:
“You and I, we're interfacing.please solve my little puzzle!
There are N pairs of numbers,Each pair consists of a key and a value,Now you need to move out some of the pairs to get the score.You can move out two continuous pairs,if and only if their keys are non coprime(their gcd is not one).The final score you get is the sum of all pair’s value which be moved out. May I ask how many points you can get the most?
The answer you give is directly related to your final exam results~The young man~”
QSC is very sad when he told the story,He failed his linear algebra that year because he didn't work out the puzzle.
Could you solve this puzzle?
(Data range:1<=N<=300
1<=Ai.key<=1,000,000,000
0<Ai.value<=1,000,000,000)
Each test case start with one integer N . Next line contains N integers,means Ai.key.Next line contains N integers,means Ai.value.
3
1 2 3
1 1 1
3
1 2 4
1 1 1
4
1 3 4 3
1 1 1 1
2
0
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = +, M = 1e2+, mod = 1e9+, inf = 0x3fffffff; LL sum[N],dp[N][N];
int f[N][N],n,m,value[N],key[N];
int gcd(int a,int b) { return b == ? a : gcd(b, a%b);}
void DP() {
for(int i = ; i < n; ++i) f[i][i+] = gcd(key[i],key[i+]) == ? : ;
for(int l = ; l <= n; l++) {
for(int i = ; i + l - <= n; ++i) {
int r = i + l - ;
f[i][r] = (f[i+][r-] && gcd(key[i],key[r])!=);
for(int k=i;k<r;++k)
f[i][r] += (f[i][k]&&f[k+][r]);
}
}
}
void solve() {
for(int l = ; l <= n; ++l) {
for(int i = ; i + l - <= n; ++i) {
int r = i + l - ;
if(f[i][r]) {
dp[i][r] = sum[r] - sum[i-];continue;
}
for(int k = i; k < r; ++k) {
dp[i][r] = max(dp[i][r],dp[i][k] + dp[k+][r]);
}
}
}
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
sum[] = ;
scanf("%d",&n);
memset(dp,,sizeof(dp));
memset(f,,sizeof(f));
for(int i = ; i <= n; ++i) scanf("%d",&key[i]);
for(int i = ; i <= n; ++i) scanf("%d",&value[i]),sum[i] = sum[i-] + value[i];
DP();
solve();
printf("%I64d\n",dp[][n]);
}
return ;
}
HDU 5900 QSC and Master 区间DP的更多相关文章
- HDU 5900 QSC and Master (区间DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意:给出序列$A_{i}.key$和$A_{i}.value$,若当前相邻的两个数$A_{ ...
- 2016 年沈阳网络赛---QSC and Master(区间DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 Problem Description Every school has some legend ...
- HDU 5900 QSC and Master
题目链接:传送门 题目大意:长度为n的key数组与value数组,若相邻的key互斥,则可以删去这两个数同时获得对应的两 个value值,问最多能获得多少 题目思路:区间DP 闲谈: 这个题一开始没有 ...
- HDU 5900 - QSC and Master [ DP ]
题意: 给n件物品,有key和value 每次可以把相邻的 GCD(key[i], key[i+1]) != 1 的两件物品,问移除的物品的总value最多是多少 key : 1 3 4 2 移除3 ...
- hdu 4597 + uva 10891(一类区间dp)
题目链接:http://vjudge.net/problem/viewProblem.action?id=19461 思路:一类经典的博弈类区间dp,我们令dp[l][r]表示玩家A从区间[l, r] ...
- HDU 2476 String painter (区间DP)
题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成 ...
- hdu_5900_QSC and Master(区间DP)
题目链接:hdu_5900_QSC and Master 题意: 有n个数,每个数有个key值,有个val,如果相邻的两个数的key的gcd大于1那么就可以得到这两个数的val的和,现在问怎么取使得到 ...
- HDU 4597 Play Game(区间DP(记忆化搜索))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 题目大意: 有两行卡片,每个卡片都有各自的权值. 两个人轮流取卡片,每次只能从任一行的左端或右端 ...
- HDU 5151 Sit sit sit 区间DP + 排列组合
Sit sit sit 问题描述 在一个XX大学中有NN张椅子排成一排,椅子上都没有人,每张椅子都有颜色,分别为蓝色或者红色. 接下来依次来了NN个学生,标号依次为1,2,3,...,N. 对于每个学 ...
随机推荐
- jquery若干问题
1.获取服务器端的控件 $("#<%=photopath.ClientID%>").uploadPreview({ Img: "ImgPr", Wi ...
- [第三方]SDWebImage获取网络图片控件的用法
#import "UIImageView+WebCache.h" @interface WeatherViewController ()<UISearchBarDelegat ...
- codeforces 505A. Mr. Kitayuta's Gift 解题报告
题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母 ...
- Zookeeper WINDOWS 安装配置
下载:zookeeper:http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz 解压zo ...
- 【编程题目】有两个序列 a,b,大小都为 n,序列元素的值任意整数,无序;(需要回头仔细研究)
32.(数组.规划)有两个序列 a,b,大小都为 n,序列元素的值任意整数,无序:要求:通过交换 a,b 中的元素,使[序列 a 元素的和]与[序列 b 元素的和]之间的差最小.例如: var a=[ ...
- HDU 4950 Monster
题目链接 题意:有一个怪物的血量为h,人攻击怪物,每回合可以杀掉a滴血,再回b滴血,k个回合之后人会休息一回合,即人不攻击而怪物回b滴血,问能否杀死.翻译过来就是给定一个数h,每轮可以先减a再加b,k ...
- 【XLL API 函数】xlSet
快速的将常数值放入到单元格区域中. 原型 Excel12(xlSet, LPXLOPER12 pxRes, 2, LPXLOPER12 pxReference,LPXLOPER pxValue); 参 ...
- 运行Myeclipse发生这事这是怎么回事,大神们
- IOS - 屏幕适配
原文:Beginning Auto Layout Tutorial in iOS 7: Part 1 感谢翻译小组成员@answer-huang(博客)热心翻译.如果您有不错的原创或译文,欢迎提交给我 ...
- [Android Pro] proguard
-optimizationpasses 5 # 指定代码的压缩级别 -dontusemixedcaseclassnames # 是否使用大小写混合 -dontskipnonpubliclibraryc ...