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. 对于每个学 ...
随机推荐
- ios 使用autolayout 后button 的frame 无法设置问题!
问题见这里,只能通过bounds和center进行设置!http://www.cocoachina.com/bbs/read.php?tid-236862.html 待研究!!!!~~~
- cxTreeList 控件说明
树.cxTreeList 属性: Align:布局,靠左,靠右,居中等 AlignWithMargins:带边框的布局 Anchors:停靠 (akTop上,akBottom下,akLeft左,akR ...
- perl 从文件里读出变量无法使用解决办法
最近在写一个perl函数,把test case 放到配置文件里,读出来然后使用system运行. 我的本意是: 配置文件conf ping -c $count $ip #在主程序中定义$ip和$cou ...
- C++ STL 的实现:
C++ STL 的实现: 1.vector 底层数据结构为数组 ,支持快速随机访问 2.list 底层数据结构为双向链表,支持快速增删 3.deque 底层数据结构为一个中央控制器和多个缓 ...
- [SQL Server]如何快速查找使用了某个字段的所有存储过程
[SQL Server]如何快速查找使用了某个字段的所有存储过程 当一个系统中使用了很多的表,并且存在大量的存储过程,当数据库中的某个表删除了某个字段,那么相应的存储过程也需要改动,但是我 ...
- supersr--NSURLSessionConfiguration-下载进度
//// ViewController.m// 下载进度 // // Created by Super on 14/7/4. // Copyright (c) 2014年 iOS. All r ...
- self dismissModalViewControllerAnimated:YES 无效(dismissviewcontrolleranimated无效)
作为一个viewController(VC),想要消失的时候可以从parent VC里面调用dismissModalViewControllerAnimated来消去改VC,也可以在该VC里面手动调用 ...
- [Android Pro] StarUML 版本破解
参考:http://bbs.chinapyg.com/thread-79022-1-1.html 官网下载地址 : http://staruml.en.softonic.com 各平台版本均适用,本文 ...
- [网络流24题] 太空飞行计划(cogs 727)
[问题描述] W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,-,Em},和进行这些实验需要使用的全部仪 ...
- Linux Shell多命令执行
有三种: :只是顺序执行,命令之间没有任何关联,不相互影响.如 ls;date;cd /etc/ 如,创建100M的文件. && 命令之间有关系,只有前一条命令正确执行才会执行下面一 ...