QSC and Master

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

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
 
Source
题意:n 个pair<int , int>,每次可以选相邻两个pair。如果他们的first不互质就可以把它们都删掉,并且获得second之和的分数,问最大得分
题解:dp[i][j]表示区间[i,j]内的最大得分 还是区间dp区间合并的老套路 但是对于题目要求的 有很巧的姿势处理
先预处理出value值的前缀和
对于当前的区间[i,j]当key[i],key[j]不互质时进行转移 若j==i+1 则说明当前的这个区间只有两个数 相邻且不互质 更新dp[i][j]=value[i]+value[j]
若当前区间的长度大于>2 只有dp[i+1][j-1]==sum[j-1]-sum[i+1-1]时 也就是中间的数全部提供贡献被移除 i,j才能够相邻
dp[i][j]=max(dp[i][j],dp[i+1][j-1]+value[i]+value[j]);
  1. /******************************
  2. code by drizzle
  3. blog: www.cnblogs.com/hsd-/
  4. ^ ^ ^ ^
  5. O O
  6. ******************************/
  7. //#include<bits/stdc++.h>
  8. #include<iostream>
  9. #include<cstring>
  10. #include<cmath>
  11. #include<cstdio>
  12. #define ll long long
  13. #define mod 1000000007
  14. #define PI acos(-1.0)
  15. using namespace std;
  16. int t;
  17. int n;
  18. ll key[];
  19. ll value[];
  20. ll dp[][];
  21. ll sum[];
  22. ll gcd(ll a,ll b)
  23. {
  24. if(b==)
  25. return a;
  26. else
  27. return gcd(b,a%b);
  28. }
  29. int main()
  30. {
  31. while(scanf("%d",&t)!=EOF)
  32. {
  33. for(int i=; i<=t; i++)
  34. {
  35. memset(dp,,sizeof(dp));
  36. scanf("%d",&n);
  37. for(int j=; j<=n; j++)
  38. scanf("%d",&key[j]);
  39. sum[]=;
  40. for(int j=; j<=n; j++)
  41. {
  42. scanf("%d",&value[j]);
  43. sum[j]=sum[j-]+value[j];
  44. }
  45. for(int j=n; j>=; j--)
  46. {
  47. for(int d=j+; d<=n; d++)
  48. {
  49. for(int zhong=j; zhong<d; zhong++)
  50. dp[j][d]=max(dp[j][d],dp[j][zhong]+dp[zhong+][d]);
  51. if(gcd(key[j],key[d])>)
  52. {
  53. if(d==j+)
  54. dp[j][d]=value[j]+value[d];
  55. else
  56. {
  57. if(dp[j+][d-]==sum[d-]-sum[j])
  58. dp[j][d]=max(dp[j][d],dp[j+][d-]+value[j]+value[d]);
  59. }
  60. }
  61. }
  62. }
  63. printf("%I64d\n",dp[][n]);
  64. }
  65. }
  66. return ;
  67. }

2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp的更多相关文章

  1. 2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

    odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  2. 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

    hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  3. 2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  4. 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  5. 2016 ACM/ICPC Asia Regional Shenyang Online

    I:QSC and Master 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意: 给出n对数keyi,vali表示当前这对数的键值和权值 ...

  6. 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

    I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  8. HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  9. HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

随机推荐

  1. STL 自学

    STL 一.vector动态数组 1 包含头函数 #include<vector> 2 函数的声明: vector<int> v; vector<int> v[ma ...

  2. C++-Effective C++ Items

    Item2:尽量以const,enum,inline替换#define 原因:1, #define ASPECT_RATIO 1.63 编译错误时产生魔数,应以const double Aspect_ ...

  3. 一步步学习JSON

    什么是Json json是JavaScript Object Notation(javascript对象表示法)的缩写,是一种轻量的数据格式,是基于javascript的一个子集.与XML一样,jso ...

  4. Apache Jmeter(2)

    上一节中,我们了解了jmeter的一此主要元件,那么这些元件如何使用到性能测试中呢.这一节创建一个简单的测试计划来使用这些元件.该计划对应的测试需求. 1)测试目标网站是fnng.cnblogs.co ...

  5. Matlab与C/C++联合编程之Matlab以MEX方式调用C/C++代码(三)

    最近写了个Matlab程序,好慢呐……所以开始学习Matlab与C/C++混合编程.下面写了个测试代码,显示一个Double类型矩阵中的元素. 源代码 #include "mex.h&quo ...

  6. SharePoint 跨域还原网站一则

    博客地址:http://blog.csdn.net/foxdave 源端:执行PowerShell命令备份网站集 Backup-SPSite http://server_name/sites/site ...

  7. viewController的自动扩展属性导致TableViewGroupStyle时向上填充

    self.automaticallyAdjustsScrollViewInsets = NO; 需设置这个属性

  8. Unix/Linux获取进程的详细信息

    Linux的进程的信息都记录在/proc/<pid>/下面,其实常用的ps.top命令也是从这里读取信息的.常用的信息有: cmd(命令).cmdline(完整的命令行参数).envrio ...

  9. [转] linux中巧用ctrl-z后台运行程序

    背景: 最近在执行一些长时间程序的时候,老是一不小心忘了输入‘&’ , 结果终端就卡在那里了,很是郁闷 以前总是再新开一个终端. 今天翻看<鸟哥的linux私房菜>的时候,发现介绍 ...

  10. Linux----七个有效的文本编辑习惯

    如果你要花大量的时间键入文本, 写程序或编写HTML脚本, 你可以通过有效地使用一个好的编辑器来替你节省时间. 本文将引导你如果快速地完成你的编辑工作, 并且减少你的错误. 本文将以开放源码软件Vim ...