题目链接:

QSC and Master

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

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
题意:
给n对数,如果相邻的一对数的第一个的gcd!=1,那么这两个数就可以一块拿走,获得第二个数的和的收益,求最大的收益;
思路:
dp[l][r]表示区间[l,r]的最大收益,转移的时候可以发现要么是分成两段dp[l][r]=max(dp[l][k],dp[k+1][r]);
要么是把中间的都取完,然后让a[l]和a[r]一块取,那么就要判断中间的[l+1,r-1]是否能取完,
用前缀和sum[r]-sum[l-1]==dp[l][r]就可以说区间都被取完了,因为这些数都是正数;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>using namespace std;#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));typedef  long long LL;template<class T> void read(T&num) {char CH; bool F=false;for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {if(!p) { puts("0"); return; }while(p) stk[++ tp] = p%10, p/=10;while(tp) putchar(stk[tp--] + '0');putchar('\n');
}const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=(1<<20)+10;
const int maxn=300+10;
const double eps=1e-12;LL a[maxn],b[maxn],sum[maxn],dp[maxn][maxn];
LL gcd(LL x,LL y)
{if(y==0)return x;return gcd(y,x%y);
}
int main()
{int t,n;read(t);while(t--){read(n);For(i,1,n)read(a[i]);For(i,1,n)read(b[i]),sum[i]=sum[i-1]+b[i];mst(dp,0);for(int r=1;r<=n;r++){for(int l=r-1;l>0;l--){for(int k=l;k<=r;k++)dp[l][r]=max(dp[l][r],dp[l][k]+dp[k+1][r]);if(gcd(a[l],a[r])!=1&&dp[l+1][r-1]==sum[r-1]-sum[l])dp[l][r]=max(dp[l+1][r-1]+b[l]+b[r],dp[l][r]);}}print(dp[1][n]);}return 0;
}

  

转载于:https://www.cnblogs.com/zhangchengc919/p/5889619.html

hdu-5900 QSC and Master(区间dp)相关推荐

  1. HDU 2476 String painter (区间DP)

    题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成 ...

  2. hdu 4597 + uva 10891(一类区间dp)

    题目链接:http://vjudge.net/problem/viewProblem.action?id=19461 思路:一类经典的博弈类区间dp,我们令dp[l][r]表示玩家A从区间[l, r] ...

  3. hdu 5151 Sit sit sit(区间dp+排列组合)

    题目链接:hdu 5151 Sit sit sit 题意: 一共有并排N个椅子, N个学生依次去坐,同时满足3个条件就不能坐下去: 1,该椅子不在最左,不在最右. 2,该椅子左右都有人坐了. 3,左右 ...

  4. HDU 5115 Dire Wolf ——(区间DP)

    比赛的时候以为很难,其实就是一个区间DP= =..思路见:点我. 区间DP一定要记住先枚举区间长度啊= =~!因为区间dp都是由短的区间更新长的区间的,所以先把短的区间更新完.. 代码如下: 1 #i ...

  5. HDU 5151 Sit sit sit 区间DP + 排列组合

    Sit sit sit 问题描述 在一个XX大学中有NN张椅子排成一排,椅子上都没有人,每张椅子都有颜色,分别为蓝色或者红色. 接下来依次来了NN个学生,标号依次为1,2,3,...,N. 对于每个学 ...

  6. hdu 5151 Sit sit sit(区间dp)

    题意: 给出一排座位现在要让所有学生入座,每个座位都有颜色,0表示蓝色,1表示红色,但是如果同时满足以下条件学生就不会坐下这个位置: 1.座位的左右都有座位 2.座位的左右都有人坐下了 3.座位的左右 ...

  7. 【HDU - 5900】QSC and Master(区间dp)

    题干: Every school has some legends, Northeastern University is the same. Enter from the north gate of ...

  8. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  9. HDU 5115 Dire Wolf 区间dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5115 Dire Wolf Time Limit: 5000/5000 MS (Java/Others ...

最新文章

  1. 【最小费用最大流】Going Home
  2. 试写函数显示当前具体时间:
  3. Android面试,BroadCastReceiver的两种注册方式的异同
  4. [转]四边形不等式优化dp(POJ1160)
  5. 内含福利|CSDN携手字节跳动:云原生Meetup北京站报名热烈启动,1月8日见!
  6. java浮点数数转二进制的数吗_深入理解计算机系统(2.7)-二进制小数和IEEE浮点标准 - Java 技术驿站-Java 技术驿站...
  7. 遥感、制图学中各种图的区别
  8. java日期加一天_Java 关于日期加一天(日期往后多一天)
  9. 60、剑指offer--把二叉树打印成多行
  10. 正则判断手机号地区_国内外手机号码正则表达式汇总
  11. python掷骰子小游戏编程入门_MicroPython实战:开发一款掷骰子小游戏
  12. 新能源充电桩主板二代新上市,迎来充电桩产业新一轮发展
  13. VMware系统启动假死,一直处于“繁忙”状态
  14. JZOJ3395. 【NOIP2013模拟】Freda的传呼机
  15. 4年Java经验面试总结(转)
  16. macbook系统占用硬盘大_Sketch占满MacBook200G硬盘的解决方法
  17. 程序员必须知道的9大数据挖掘工具
  18. 测试网线是否正常的方法有哪些?
  19. 牛客网-Java专项练习11
  20. MFC 加载jpg等图片

热门文章

  1. ASP隐藏文件地址,并在下载时替换文件名
  2. 前端开发的开始---基于OO的Ajax类
  3. [转载 css]word-wrap,word-break,white-space,text-overflow的区别和用法
  4. 图像显著区域检测代码及其效果图 saliency region detection
  5. C#测试程序运行时间
  6. 《OpenCV图像处理》——1.7 用户交互工具
  7. 20.三层技术之OSPF区域划分与路由重分发(2)
  8. Windows API获取系统配置文件的配置参数
  9. Discuz X2下tag伪静态详细设置方法
  10. 让几个div靠外面容器底部对齐