QSC and Master

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 612    Accepted Submission(s): 214

Problem Description
Every school has some legends, Northeastern University is the same.

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)

 
Input
First line contains a integer T,means there are T(1≤T≤10) test case。

Each test case start with one integer N . Next line contains N integers,means Ai.key.Next line contains N integers,means Ai.value.

 
Output
For each test case,output the max score you could get in a line.
 
Sample Input
3
3
1 2 3
1 1 1
3
1 2 4
1 1 1
4
1 3 4 3
1 1 1 1
 
Sample Output
0
2
0
 
补个区间DP题,越来越发现区间DP题都好明显啊,首先范围都是100,200,300的,其次在考虑最优子结构时都是一段区间。
对于这个题,像其他的区间题一样,考虑区间[i,j]:
首先dp[i][j]初始化,那就是枚举k点啦。
然后处理:如果key[i]和key[j]是gcd的话,如果j=i+1,那么直接dp[i][j] = val[i]+val[j],否则必须[i+1,j-1]内全部去掉才可以dp[i][j] =
max(dp[i][j],dp[i+1][j-1]+val[i]+val[j]).可以用个前缀和来判断。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = ;
ll dp[maxn][maxn];
ll key[maxn];
ll val[maxn];
ll sum[maxn];
int n;
ll gcd(ll a,ll b)
{
return b == ? a : gcd(b, a % b);
}
void DP()
{
memset(dp,,sizeof(dp));
for(int l=;l<=n;l++)
{
for(int i=;i+l<=n;i++)
{
int j=i+l;
for(int k=i;k<j;k++)
dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+][j]);
// printf("%I64d\n",gcd(key[i],key[j]));
if(gcd(key[i],key[j])>)
{
if(j==i+) dp[i][j] = val[i]+val[j];
else if(dp[i+][j-]==sum[j-]-sum[i]) dp[i][j] = max(dp[i][j],dp[i+][j-]+val[i]+val[j]);
}
}
}
printf("%I64d\n",dp[][n]);
}
int main()
{
int T;cin>>T;
while(T--)
{
memset(sum,,sizeof(sum));
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%I64d",&key[i]);
for(int i=;i<=n;i++) scanf("%I64d",&val[i]),sum[i]=val[i]+sum[i-];
DP();
}
}
 

HDU 5900的更多相关文章

  1. HDU 5900 QSC and Master (区间DP)

    题目链接   http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意:给出序列$A_{i}.key$和$A_{i}.value$,若当前相邻的两个数$A_{ ...

  2. HDU 5900 QSC and Master 区间DP

    QSC and Master Problem Description   Every school has some legends, Northeastern University is the s ...

  3. 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

    QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  4. HDU 5900 - QSC and Master [ DP ]

    题意: 给n件物品,有key和value 每次可以把相邻的 GCD(key[i], key[i+1]) != 1 的两件物品,问移除的物品的总value最多是多少 key : 1 3 4 2  移除3 ...

  5. HDU 5900 QSC and Master

    题目链接:传送门 题目大意:长度为n的key数组与value数组,若相邻的key互斥,则可以删去这两个数同时获得对应的两 个value值,问最多能获得多少 题目思路:区间DP 闲谈: 这个题一开始没有 ...

  6. hdu 5900 区间dp

    题意:给你n对pair 里面有两个值,分别是key 和 val .你可以取相邻的两个pair 获得其中的val,前提是两个pair 的key 的 gcd 不为 1.当然你把相邻的两个取走了之后原本不相 ...

  7. BestCoder17 1001.Chessboard(hdu 5100) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5100 题目意思:有一个 n * n 的棋盘,需要用 k * 1 的瓷砖去覆盖,问最大覆盖面积是多少. ...

  8. HDU 5100

    http://acm.hdu.edu.cn/showproblem.php?pid=5100 用1*k方格覆盖n*n方格 有趣的一道题,查了下发现m67的博客还说过这个问题 其实就是两种摆法取个最大值 ...

  9. hdu 5100 Chessboard

    http://acm.hdu.edu.cn/showproblem.php?pid=5100 在比赛时没看懂题就没看,结束之后,看了解题报告才知道怎么做. 解题报告: 首先,若n<k,则棋盘连一 ...

随机推荐

  1. 关于oracle数据库(2)

    数据备份.数据删除.数据还原 连接数据库,查看scott用户下面的所有表 数据备份(数据导出)要输入导出文件路径和文件名(文件扩展名可输入也可以不输入) 导出成功后,可以在上面输入的文件路径下面看到导 ...

  2. iOS 视图控制器生命周期

    1.init: 2.viewDidLoad: 3.viewWillAppear: 4.viewDidAppear: 5.viewWillDisappear; 6.viewDidDisappear

  3. navicat查询sqlserver数据库编码

    首先:查看SQLserver编码格式的SQL语句为: go SELECT  COLLATIONPROPERTY('Chinese_PRC_Stroke_CI_AI_KS_WS', 'CodePage' ...

  4. Sql Sever语句 (续2)

    日期时间 把日期类型的字段下默认值或绑定里的 设置成getdate()   ,新建字段时候不设置会自动获取当前服务器时间 在当前时间加上xx年xx月xx天 select datedd(yy,100,g ...

  5. Calendar.getInstance()获取当天指定点上的时间[转载]

    ctoday.add(Calendar.DAY_OF_MONTH, 1); 明天时间 //获得当天0点时间public static int getTimesmorning(){Calendar ca ...

  6. Hbase查看

    Client HBase Client使用HBase的RPC机制与HMaster和HRegionServer进行通信,对于管理类操作,Client与HMaster进行RPC:对于数据读写类操作,Cli ...

  7. [原]innerText与innerHTML区别

    window.onload = function () {             document.getElementById('btn1').onclick = function () {    ...

  8. sd卡文件操作

    1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...

  9. 在命令提示符下,怎么查看windows开启了哪些服务?

    net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" /user: ...

  10. MFC中组合框CComboBox的使用

    假如添加三行数据,其中当选择前两行时,提示自定义提示框信息,选择第三行时,提示当前字符串的内容,实现如下: void Cuse_demo_dllDlg::OnCbnSelchangeCombo1(){ ...