HDU 5900

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]).可以用个前缀和来判断。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 using namespace std;
 7 typedef long long ll;
 8 const int maxn = 305;
 9 ll dp[maxn][maxn];
10 ll key[maxn];
11 ll val[maxn];
12 ll sum[maxn];
13 int n;
14 ll gcd(ll a,ll b)
15 {
16     return b == 0 ? a : gcd(b, a % b);
17 }
18 void DP()
19 {
20     memset(dp,0,sizeof(dp));
21     for(int l=1;l<=n;l++)
22     {
23         for(int i=1;i+l<=n;i++)
24         {
25             int j=i+l;
26             for(int k=i;k<j;k++)
27                 dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+1][j]);
28           //  printf("%I64d\n",gcd(key[i],key[j]));
29             if(gcd(key[i],key[j])>1)
30             {
31                 if(j==i+1) dp[i][j] = val[i]+val[j];
32                 else if(dp[i+1][j-1]==sum[j-1]-sum[i]) dp[i][j] = max(dp[i][j],dp[i+1][j-1]+val[i]+val[j]);
33             }
34         }
35     }
36     printf("%I64d\n",dp[1][n]);
37 }
38 int main()
39 {
40     int T;cin>>T;
41     while(T--)
42     {
43         memset(sum,0,sizeof(sum));
44         scanf("%d",&n);
45         for(int i=1;i<=n;i++) scanf("%I64d",&key[i]);
46         for(int i=1;i<=n;i++) scanf("%I64d",&val[i]),sum[i]=val[i]+sum[i-1];
47         DP();
48     }
49 }
时间: 2024-09-28 05:02:28

HDU 5900的相关文章

HDU 5900(区间DP)

HDU 5900 QSC and Master 题意:给一串数的key和value,如果相邻两元素key不是互质的就可以将这俩移除并获得这俩的value值,移除后两侧的元素便是相邻了,问最终最大能获得多少value值. 思路:区间DP,区间长度为1时dp[i][i]为0,添加一个标记数组vis[i][j],记录区间内是否所有数字都为可以移除,每次处理需要讨论最后留下区间两侧的情况. #include <cstdio> #include <cstring> #include <

HDU 5900 QSC and Master (区间DP)

题目链接   http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意:给出序列Ai.key和Ai.value,若当前相邻的两个数Ai.key和Ai+1.key的最大公约数大于1,则可以把这两个数消去,同时消去Ai.value和Ai+1.value,每次消去得到的分数为Ai和Ai+1的value值,问最大可能得分. 注意:当Ai和Ai+1被消去后,Ai-1和Ai+2成为了新的相邻的数.若符合条件则可以继续消去. 思路:很明显是区间DP,但是我比赛中如何也

HDU 5900 - QSC and Master [ DP ]

题意: 给n件物品,有key和value 每次可以把相邻的 GCD(key[i], key[i+1]) != 1 的两件物品,问移除的物品的总value最多是多少 key : 1 3 4 2  移除34以后12也相邻了,可以移除 分析: 先预处理出所有GCD[l][r], 意味 l <= i <= r的区域可以全部移除, 用记忆化搜索处理 然后 dp[i] 代表到 i 为止可以得到的最大value和 if (G[l][r]) dp[r] = max(dp[r], dp[l-1] + sum[r

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)Total Submission(s): 859    Accepted Submission(s): 325 Problem Description Every school has some legends, Northeastern University is the same. Enter

Hdu 5900 QSC and Master

给出n对数keyi,vali表示当前这对数的键值和权值,可以操作将连续的两个数合并,如果满足gcd(a[i],a[i+1])>1,得到 的价值是两个数的权值和,每次合并两个数之后,这两个数就会消失,然后旁边的数会接上 比如1 2 3 4 合并了 2 3 则 剩下1 4也可以合并. 处理出任意区间内的所有数是否可以合并 对于当前的[l,r]区间,如果区间[l+1,r-1]可以合并,且gcd(a[l]+a[r])>1的话,则整个区间[l,r]可以合并,价值也就是前缀和 同理处理出其他的情况  [l

hdu 5900 QSC and Master 区间dp

QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Every school has some legends, Northeastern University is the same. Enter from the north gate of Northeastern University,You are

hdu 5100 Chessboard

http://acm.hdu.edu.cn/showproblem.php?pid=5100 在比赛时没看懂题就没看,结束之后,看了解题报告才知道怎么做. 解题报告: 首先,若n<k,则棋盘连一个1×k的矩形都放不下,输出0. 我们只需要考虑n≥k的情况.将棋盘类似于黑白染色,按(i+j)模k划分等价类,给每个格子标一个号. 标号之后,会注意到每条从左下到右上的斜线数字都是相同的,那么对于s×s的格子,其内部数字有且恰好有2s−1种,所以当s<=k2的时候,内部数字有floor(k2)∗2−1

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5100 题目意思:有一个 n * n 的棋盘,需要用 k * 1 的瓷砖去覆盖,问最大覆盖面积是多少. 比赛时不会做............. hdu 题解: 首先,若n<k,则棋盘连一个1×k的矩形都放不下,输出0. 我们只需要考虑n≥k的情况.将棋盘类似于黑白染色,按(i+j)模k划分等价类,给每个格子标一个号. 标号之后,会注意到每条从左下到右上的斜线数字都是相同的,那么对于s×s的格子,其内部

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;