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. 对于每个学 ...
随机推荐
- 使用Java数组实现双色球选号
package com.hm.test; import java.util.Random; /** * 模拟双色球生成 * *1.从1到16中产生一个篮球的随机数 *2.从1到33中产生出6个红色的球 ...
- 多字段 java对象排序
public class ReflexUtil { static Logger logger = LoggerFactory.getLogger(ReflexUtil.class); //getMet ...
- 5.nodejs权威指南--HTTP
1. HTTP 1.1 服务器 var http = require('http'); var server = http.createServer(function(req,rsp){ req.on ...
- FFmpeg for XP(x86) 2016-03-23 static 静态编译程序
FFmpeg for XP(x86) 2016-03-23 static 静态编译适用于32位XP系统,能加的扩展都加了,结果文件大小非常大. 最新版加了不少视频和音频滤镜. ffmpeg.20160 ...
- MAC OS X 常用通用快捷键
注:由于使用的是Windows键盘,习惯了Ctrl + c/v复制粘贴,所以修改了修饰键,Command(⌘)键和Control(^)键互换,以下的Ctrl键均为Command键,对应键盘上的实际左C ...
- 【linux】学习2
鸟哥那本书的第6章 文件权限: ^ ^ ^ ^ ^ ^ ^ 1 ...
- .NET微信公众号开发-3.0查询自定义菜单
一.前言 前面我们已经创建好了我们的自定义菜单.那么我们现在要如何查询我们自定义的菜单.原理都是一样的,而且都是相当简单,只是接口地址文档换掉了. 2.0开始编码 同样我们首先创建好我的查询页面,在这 ...
- Oracle基础函数
--1,大小写控制函数 SELECT LOWER('Hello World') 转小写, UPPER('Hello World') 转大写, INITCAP('hello world') 首字母大写 ...
- iOS添加另一个控制器的时候要注意啊
ios的父控制器控制器和子控制器之间的注意点: " >> #warning 需要把新创建出来的控制器添加成子控制器 . 不然子控制器里面的子控件无法响应点击事件 " 一 ...
- [Android Pro] 小心ReleaseByteArrayElements 中的参数问题
referen to : http://blog.csdn.net/rainlight/article/details/818964 在Sun的官方文档中,关于该函数的用法如下 The array i ...