zoukankan      html  css  js  c++  java
  • 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 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
    题意:npair<int , int>,每次可以选相邻两个pair。如果他们的first不互质就可以把它们都删掉,并且获得second之和的分数,问最大得分
    题解:dp[i][j]表示区间[i,j]内的最大得分 还是区间dp区间合并的老套路 但是对于题目要求的 有很巧的姿势处理
    先预处理出value值的前缀和
    对于当前的区间[i,j]当key[i],key[j]不互质时进行转移 若j==i+1 则说明当前的这个区间只有两个数 相邻且不互质 更新dp[i][j]=value[i]+value[j]
    若当前区间的长度大于>2 只有dp[i+1][j-1]==sum[j-1]-sum[i+1-1]时 也就是中间的数全部提供贡献被移除 i,j才能够相邻
    dp[i][j]=max(dp[i][j],dp[i+1][j-1]+value[i]+value[j]);
     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 //#include<bits/stdc++.h>
     8 #include<iostream>
     9 #include<cstring>
    10 #include<cmath>
    11 #include<cstdio>
    12 #define ll long long
    13 #define mod 1000000007
    14 #define PI acos(-1.0)
    15 using namespace std;
    16 int t;
    17 int n;
    18 ll key[305];
    19 ll value[305];
    20 ll dp[305][305];
    21 ll sum[305];
    22 ll gcd(ll a,ll b)
    23 {
    24     if(b==0)
    25         return a;
    26     else
    27         return gcd(b,a%b);
    28 }
    29 int main()
    30 {
    31     while(scanf("%d",&t)!=EOF)
    32     {
    33         for(int i=1; i<=t; i++)
    34         {
    35             memset(dp,0,sizeof(dp));
    36             scanf("%d",&n);
    37             for(int j=1; j<=n; j++)
    38                 scanf("%d",&key[j]);
    39             sum[0]=0;
    40             for(int j=1; j<=n; j++)
    41             {
    42                 scanf("%d",&value[j]);
    43                 sum[j]=sum[j-1]+value[j];
    44             }
    45             for(int j=n; j>=1; j--)
    46             {
    47                 for(int d=j+1; d<=n; d++)
    48                 {
    49                     for(int zhong=j; zhong<d; zhong++)
    50  dp[j][d]=max(dp[j][d],dp[j][zhong]+dp[zhong+1][d]);
    51                     if(gcd(key[j],key[d])>1)
    52                     {
    53                         if(d==j+1)
    54                             dp[j][d]=value[j]+value[d];
    55                         else
    56                         {
    57                             if(dp[j+1][d-1]==sum[d-1]-sum[j])
    58   dp[j][d]=max(dp[j][d],dp[j+1][d-1]+value[j]+value[d]);
    59                         }
    60                     }
    61                 }
    62             }
    63             printf("%I64d
    ",dp[1][n]);
    64         }
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    一篇图看清Java中的各种Queue
    使用尾递归计算阶乘
    使用 Sonar 检测代码质量
    jsessionid 导致重定向404的问题
    Java8之——简洁优雅的Lambda表达式
    支付宝手机网站支付开发指引
    Intellij Idea 编辑器使用之 安装、破解 版本15.0.1
    虚拟机启动linux系统报错,此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态
    META-INF文件夹是干啥的,META-INF文件夹的作用, META-INF文件夹能删吗
    一道Integer面试题引发的对Integer的探究
  • 原文地址:https://www.cnblogs.com/hsd-/p/5890262.html
Copyright © 2011-2022 走看看