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,r-2] [l+1,r]

区间dp,对于当前的l,r区间,如果可以合并,则直接加上区间和

反之,则枚举一个中间值k,找出区间内最大满足情况的值

所以是2次dp

#include <bits/stdc++.h>

typedef long long LL;

using namespace std;

int n;
LL  sum[1000],a[1000],b[1000];
LL  g[302][302],dp[302][302];

LL gcd(LL a,LL b){
    return b==0?a:gcd(b,a%b);
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        memset(a,0,sizeof a);
        memset(b,0,sizeof b);
        memset(sum,0,sizeof sum);
        memset(g,0,sizeof g);
        memset(dp,0,sizeof dp);

        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
        for(int i=1;i<=n;i++) scanf("%I64d",&b[i]),sum[i]=sum[i-1]+b[i];

        for(int i=1;i<=n-1;i++) if(gcd(a[i],a[i+1])>1) g[i][i+1]=1;

        for(int len=2;len<=n;len+=2){
            for(int j=1;j+len-1<=n;j++){
                int l=j,r=j+len-1;
                if(gcd(a[l],a[r])>1 && g[l+1][r-1]) g[l][r]=1;
                if(gcd(a[l],a[l+1])>1 && g[l+2][r]) g[l][r]=1;
                if(gcd(a[r-1],a[r])>1 && g[l][r-2]) g[l][r]=1;
            }
        }

        for(int len=2;len<=n;len++)
            for(int j=1;j+len-1<=n;j++){
                int l=j,r=j+len-1;
                if(g[l][r]) dp[l][r]=sum[r]-sum[l-1];
                else  for(int k=l;k<r;k++)
                         dp[l][r]=max(dp[l][r],dp[l][k]+dp[k+1][r]);
            }

        printf("%I64d\n",dp[1][n]);
    }
    return 0;
}
时间: 2024-10-14 19:43:50

Hdu 5900 QSC and Master的相关文章

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

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

HDU5900 QSC and Master(区间DP + 最小费用最大流)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 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.

2016 年沈阳网络赛---QSC and Master(区间DP)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 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 Universi