2016 ACM/ICPC Asia Regional Shenyang Online HDU 5900 QSC and Master(区间dp水题)
来源:互联网 发布:在线购物商城系统源码 编辑:程序博客网 时间:2024/05/31 19:33
QSC and Master
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 663 Accepted Submission(s): 240
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)
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.
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
331 2 31 1 131 2 41 1 141 3 4 31 1 1 1
Sample Output
020
【题意】n 个pair<int , int>,每次可以选相邻两个pair。如果他们的first不互质就可以把它们都删掉,并且获得second之和的分数,问最大得分。
【解题方法】区间dp水题。定义dp[i][j] 表示i~j的区间能得的最大分,那么有状态转移方程:dp[i][j]=dp[i][k]+dp[k+1][j]。
【AC 代码】
【解题方法】区间dp水题。定义dp[i][j] 表示i~j的区间能得的最大分,那么有状态转移方程:dp[i][j]=dp[i][k]+dp[k+1][j]。
【AC 代码】
////Created by just_sort 2016/9/12 17:01//Copyright (c) 2016 just_sort.All Rights Reserved//#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 305;const int maxm = 305;LL dp[maxn][maxn];LL a[maxn],b[maxn],sum[maxn];int main(){ int T,n; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=1; i<=n; i++){ scanf("%I64d",&a[i]); } sum[0]=0LL; for(int i=1; i<=n; i++){ scanf("%I64d",&b[i]); sum[i]=sum[i-1]+b[i]; } memset(dp,0,sizeof(dp)); for(int len=1; len<=n; len++){ for(int i=1; i+len<=n; i++){ if(dp[i+1][i+len-1]==sum[i+len-1]-sum[i]){ dp[i][i+len] = dp[i+1][i+len-1]; if(__gcd(a[i],a[i+len])>1) dp[i][i+len] += b[i]+b[i+len]; } for(int k=i; k<i+len; k++){ dp[i][i+len] = max(dp[i][i+len],dp[i][k]+dp[k+1][i+len]); } } } printf("%I64d\n",dp[1][n]); } return 0;}
0 0
- 2016 ACM/ICPC Asia Regional Shenyang Online HDU 5900 QSC and Master(区间dp水题)
- hdu 5900 QSC and Master 区间dp(2016 ACM/ICPC Asia Regional Shenyang Online)
- HDU5900 QSC and Master(区间DP)2016 ACM/ICPC Asia Regional Shenyang Online
- 2016 ACM/ICPC Asia Regional Shenyang Online 1009 QSC and Master 区间dp
- HDU 5900 QSC and Master(2016 ACM/ICPC Asia Regional Shenyang Online)
- 【HDU5900 2016 ACM ICPC Asia Regional Shenyang Online I】【区间DP】QSC and Master 相邻互质取数最大取值.cpp
- 2016 ACM/ICPC Asia Regional Shenyang Online HDU 5895 Mathematician QSC
- 2016 ACM/ICPC Asia Regional Shenyang Online HDU 5895 Mathematician QSC(矩阵快速幂+高次幂取模)★ ★
- 2016 ACM/ICPC Asia Regional Shenyang Online
- HDU 5898 odd-even number (数位DP) 2016 ACM/ICPC Asia Regional Shenyang Online
- HDU 5898&&2016 ACM/ICPC Asia Regional Shenyang Online/ odd-even number [数位DP]【动态规划】
- HDU 5898 odd-even number (数位DP) 2016 ACM/ICPC Asia Regional Shenyang Online
- HDU 5895&&2016 ACM/ICPC Asia Regional Shenyang Online1004 Mathematician QSC [矩阵加速+欧拉降幂]【数论】
- hdu 5451(2015 ACM/ICPC Asia Regional Shenyang Online)
- HDU 5458 Stability (2015 ACM/ICPC Asia Regional Shenyang Online)
- HDU-2017 ACM/ICPC Asia Regional Shenyang Online-签到题
- 2017 ACM/ICPC Asia Regional Shenyang Online 1001(hdu 6194)
- HDU 6199 gems gems gems (DP, 2017 ACM/ICPC Asia Regional Shenyang Online)
- PAT 1074. Reversing Linked List (25)(链表反转)
- HDU 5901 Count Primes (模板 + 数论知识)——2016 ACM/ICPC Asia Regional Shenyang Online
- Java数据结构之双向链表
- JavaScript的核心
- win7安装iis及web配置教程
- 2016 ACM/ICPC Asia Regional Shenyang Online HDU 5900 QSC and Master(区间dp水题)
- 84. Largest Rectangle in Histogram(dp)
- 用story review meeting 来准备 sprint planning
- 第三方okhttp网络访问框架使用教程
- python小爬虫
- iOS视频播放
- Python版libsvm安装
- 关于Notepad++编辑html文件出现乱码问题的解决方案
- Sprint Planning 是否需要分子任务