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] - sum[l-1] )

  以及用 dp[i] = max(dp[i], dp[i-1]) 向后保留最大值

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 #define LL long long
 6 const int MAXN = 305;
 7 int t, n;
 8 LL dp[MAXN];
 9 LL key[MAXN], v[MAXN], sum[MAXN];
10 int GCD[MAXN][MAXN];
11 LL gcd(LL a, LL b)
12 {
13     return b == 0 ? a : gcd(b, a%b);
14 }
15 bool check(int l, int r)
16 {
17     if (l > r) return 1;
18     if (GCD[l][r] != -1) return GCD[l][r];
19     if (l == r) return GCD[l][r] = 0;
20     if ((r-l)%2 == 0) return GCD[l][r] = 0;
21     if (l == r-1) return GCD[l][r] = ( gcd(key[l], key[r]) != 1 );
22     if (gcd(key[l], key[r]) != 1 && check(l+1, r-1) ) return GCD[l][r] = 1;
23     for (int i = l+1; i < r-1; i++)
24     {
25         if (check(l,i) && check(i+1, r)) return GCD[l][r] = 1;
26     }
27     return GCD[l][r] = 0;
28 }
29 int main()
30 {
31     scanf("%d", &t);
32     while (t--)
33     {
34         scanf("%d", &n);
35         for (int i = 1; i <= n; i++)
36             scanf("%lld", &key[i]);
37         sum[0] = 0;
38         for (int i = 1; i <= n; i++)
39             scanf("%lld", &v[i]) , sum[i] = sum[i-1] + v[i];
40         memset(GCD, -1, sizeof(GCD));
41         for (int i = 1; i <= n; i++)
42             for (int j = i; j <= n; j++)
43                 check(i, j);
44         memset(dp, 0, sizeof(dp));
45         for (int i = 1; i <= n; i++)
46         {
47             for (int j = 2; j <= i; j += 2)
48             {
49                 int l = i - j + 1, r = i;
50                 if (check(l, r))
51                     dp[i] = max(dp[i], dp[l-1] + sum[r] - sum[l-1]);
52             }
53             dp[i] = max(dp[i], dp[i-1]);
54         }
55         printf("%lld\n", dp[n]);
56     }
57 } 
时间: 2025-01-25 03:52:52

HDU 5900 - QSC and Master [ DP ]的相关文章

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 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

给出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(区间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 <

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

2016沈阳网络赛 QSC and Master

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 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

hdu 3555 Bomb(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:就是给你一个数n,判断从0到n有多少个数含有数字49...... 是不是觉得跟hdu2089很相似呀... 思路:跟hdu2089一样的,注意给出的数比较大,所以这儿用__int64  .... code: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm&

HDU 1231 最大连续子序列 DP题解

典型的DP题目,增加一个额外要求,输出子序列的开始和结尾的数值. 增加一个记录方法,nothing special. 记录最终ans的时候,同时记录开始和结尾下标: 更新当前最大值sum的时候,更新开始节点. const int MAX_N = 10001; long long arr[MAX_N]; int N, sta, end; long long getMaxSubs() { long long sum = 0, ans = LLONG_MIN; int ts = 0; for (int