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. 对于每个学 ...
随机推荐
- 【Unity3D】计算二维向量夹角(-180到180)
在Unity3D中,有时候我们需要计算二维向量的夹角.二维向量夹角一般在0~180度之前,可以直接调用Vector2.Angle(Vector2 from, Vector2 to)来计算. 但是在有些 ...
- logging模块使用示例
日志等级说明: UNSET < DEBUG < INFO < WARNNING < ERROR < CRITICAL import logging logger = l ...
- android通过Canvas和Paint截取无锯齿圆形图片
一个通过Canvas和Paint截取无锯齿圆形图片. /** * 根据原图和变长绘制圆形图片 * * @param source * @param min * @return */ public st ...
- zpf 路由功能
2015-4-11 20:51:06 又搞了一天, 解决了一堆bug, 重新规划了类文件夹, 改善自动加载功能 最新的特性就是支持子域名路由了 因为整个框架还在完善当中, 而且里边有好多接口有我自己申 ...
- C Primer Plus_第四章_字符串和格式化输入输出_编程练习
Practice 1.输入名字和姓氏,以"名字,姓氏"的格式输出打印. #include int main(void) { char name[20]; char family[2 ...
- C Primer Plus_第一章_概览_复习题与编程练习
REVIEW 1.就编程而言,可移植性表示什么? me 一个系统上编写的程序经过很少改动或者不需改动就可以在另一个系统上运行.如果修改是必须的,则通常只改变伴随主程序的一个头文件中的几项内容即可.(P ...
- 魅族MX3\MX2 在MTP模式下恢复手机误删数据教程
昨天帮室友的魅族mx3升级系统,结果不小心把他手机里的照片删了.但是自从flyme升级到3后,以前的U盘模式就改成了MTP模式,这样再连接电脑后就没办法用电脑上的数据恢复软件恢复数据了,因为压根就检测 ...
- 【linux】虚拟机安装centos后ping ip地址出现错误:Network is unreachable
来源:https://my.oschina.net/stonezing/blog/515480 方案一: 进入/etc/sysconfig/network-scripts/ 查看这下面的文件 每个人的 ...
- 浅析 Cordova for iOS
转自@夏小BO的技术博客: Cordova,对这个名字大家可能比较陌生,大家肯定听过 PhoneGap 这个名字,Cordova 就是 PhoneGap 被 Adobe 收购后所改的名字.(Cordo ...
- ASINetworkQueues(经典2)
ASINetworkQueues, 它的delegate提供更为丰富的功能 提 供的更多的回调方法如下: a,requestDidStartSelector,请求发起时会调此方法,你可以在此方法中跟据 ...