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]);
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define ll long long
#define mod 1000000007
#define PI acos(-1.0)
using namespace std;
int t;
int n;
ll key[];
ll value[];
ll dp[][];
ll sum[];
ll gcd(ll a,ll b)
{
if(b==)
return a;
else
return gcd(b,a%b);
}
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=; i<=t; i++)
{
memset(dp,,sizeof(dp));
scanf("%d",&n);
for(int j=; j<=n; j++)
scanf("%d",&key[j]);
sum[]=;
for(int j=; j<=n; j++)
{
scanf("%d",&value[j]);
sum[j]=sum[j-]+value[j];
}
for(int j=n; j>=; j--)
{
for(int d=j+; d<=n; d++)
{
for(int zhong=j; zhong<d; zhong++)
dp[j][d]=max(dp[j][d],dp[j][zhong]+dp[zhong+][d]);
if(gcd(key[j],key[d])>)
{
if(d==j+)
dp[j][d]=value[j]+value[d];
else
{
if(dp[j+][d-]==sum[d-]-sum[j])
dp[j][d]=max(dp[j][d],dp[j+][d-]+value[j]+value[d]);
}
}
}
}
printf("%I64d\n",dp[][n]);
}
}
return ;
}

最新文章

  1. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
  2. 最长上升子序列(LIS)模板
  3. git向gitHub上push和pull数据.
  4. nginx 多站点配置方法集合(转)
  5. 九度OJ 1480 最大上升子序列和 -- 动态规划
  6. nginx日志配置
  7. 百度背景画面切换效果,js做
  8. 哈希表原理及hashmap简单实现
  9. Docker Compose安装以及入门
  10. HoloLens开发手记 - 使用配件 Working with accessories
  11. 为Kubernetes集群部署本地镜像仓库
  12. vue生命周期理解图
  13. [Python_5] Python 线程
  14. Digital Color Meter 颜色值提取工具
  15. python笔记--冒泡排序升级版
  16. RAC DBCA 找不到共享磁盘
  17. javascript在字符串中提取网址并替换成超链接
  18. EF – 4.CRUD与事务
  19. java基础讲解14-----IO
  20. Excel数据常用操作,vlookup,text,trim,数据格式导致出错

热门文章

  1. Black 全面分析
  2. POJ 2255 Tree Recovery 树的遍历,分治 难度:0
  3. MM 不哭 (tyvj 1097)
  4. cmd的xcopy命令
  5. POJ 1777 mason素数
  6. java udp网络编程
  7. Typographical Concepts
  8. JS中关于 一个关于计时器功能效果的实现
  9. STL源码分析----神奇的 list 的 sort 算法实现
  10. UITableView详解(1)