HDU5900(区间DP)

た 入场券 2022-05-30 11:41 339阅读 0赞

QSC and Master

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

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

2016 ACM/ICPC Asia Regional Shenyang Online

Recommend

wange2014

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<queue>
  6. using namespace std;
  7. const int maxn=310;
  8. const int INF=0x3f3f3f3f;
  9. typedef long long ll;
  10. ll dp[maxn][maxn], sum[maxn], val[maxn];
  11. int id[maxn];
  12. int gcd(int x, int y){
  13. return y==0 ? x : gcd(y, x%y);
  14. }
  15. int main(){
  16. int T, n;
  17. scanf("%d", &T);
  18. while(T--){
  19. memset(dp, 0, sizeof(dp));
  20. memset(sum, 0, sizeof(sum));
  21. scanf("%d", &n);
  22. for(int i=1; i<=n; i++)
  23. scanf("%d", &id[i]);
  24. for(int j=1; j<=n; j++)
  25. {
  26. scanf("%lld", &val[j]);
  27. sum[j]=sum[j-1]+val[j];
  28. }
  29. for(int l=1; l<n; l++)
  30. {
  31. for(int i=1; i+l<=n; i++)//枚举区间
  32. {
  33. int j=i+l;
  34. for(int k=i; k<j; k++)
  35. dp[i][j]=max(dp[i][j], dp[i][k]+dp[k+1][j]);//更新此区间
  36. if(gcd(id[i], id[j])>1)//将满足条件的区间计算出来
  37. {
  38. if(i+1==j)
  39. dp[i][j]=sum[j]-sum[i-1];
  40. else if(dp[i+1][j-1]==sum[j-1]-sum[i])
  41. dp[i][j]=sum[j]-sum[i-1];
  42. }
  43. }
  44. }
  45. printf("%lld\n", dp[1][n]);
  46. }
  47. return 0;
  48. }

发表评论

表情:
评论列表 (有 0 条评论,339人围观)

还没有评论,来说两句吧...

相关阅读

    相关 区间dp

    概念 区间dp就是在区间上进行动态规划,求解一段区间上的最优解。主要是通过合并小区间的 最优解进而得出整个大区间上最优解的dp算法。 借鉴大佬总结:[猛戳][Link

    相关 区间dp

    让我求解在一个区间上的最优解,那么我把这个区间分割成一个个小区间,求解每个小区间的最优解,再合并小区间得到大区间即可。所以在代码实现上,我可以枚举区间长度len为每次分割成的小