HDU 5900 QSC and Master 区间DP

QSC and Master

题目连接:

http://acm.split.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.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

Hint

题意

每个数有两个值,key和value。

就是如果连续的两个数的key不互质的话,那么你就可以删除这两个数,使得答案加上value。

问你最后怎么删除,才能使得value最大。

题解:

从数据范围和题意来看,显然的区间DP,直接莽一波就好了= =

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 305;
int c[maxn][maxn],a[maxn],b[maxn],dp[maxn][maxn],n,m,x;
long long dp2[maxn][maxn];
int vis[maxn][maxn];
long long sum[maxn];
void init()
{
    memset(c,0,sizeof(c));
    memset(vis,0,sizeof(vis));
    memset(dp,0,sizeof(dp));
    memset(dp2,0,sizeof(dp2));
    memset(sum,0,sizeof(sum));
}
int gcd(int aa,int bb)
{
    if(bb==0)return  aa;
    else return gcd(bb,aa%bb);
}
bool check(int x,int y)
{
    if(gcd(a[x],a[y])!=1)return 1;
    return 0;
}
void solvedp()
{
    for(int len=1;len<=n;len++)
    {
        for(int l=1;l<=n;l++)
        {
            int r=l+len;
            if(r>n)break;
            dp[l][r]=max(dp[l+1][r],dp[l][r-1]);
            dp2[l][r]=max(dp2[l+1][r],dp2[l][r-1]);
            if(c[l][r]&&dp[l+1][r-1]==(r-l-1))
            {
                dp[l][r]=max(dp[l][r],dp[l+1][r-1]+2);
                dp2[l][r]=max(dp2[l][r],dp2[l+1][r-1]+b[l]+b[r]);
            }
            for(int i=l;i<r;i++)
            {
                dp[l][r]=max(dp[l][r],dp[l][i]+dp[i+1][r]);
                dp2[l][r]=max(dp2[l][r],dp2[l][i]+dp2[i+1][r]);
            }
            for(int i=l+1;i<r;i++)
            {
                if(c[l][i]&&dp[l+1][i-1]==(i-l-1))
                {
                    dp[l][r]=max(dp[l][r],dp[l+1][i-1]+dp[i+1][r]+2);
                    dp2[l][r]=max(dp2[l][r],dp2[l+1][i-1]+dp2[i+1][r]+b[l]+b[i]);
                }
                if(c[i][r]&&dp[i+1][r-1]==(r-i-1))
                {
                    dp[l][r]=max(dp[l][r],dp[l][i-1]+dp[i+1][r-1]+2);
                    dp2[l][r]=max(dp2[l][r],dp2[l][i-1]+dp2[i+1][r-1]+b[i]+b[r]);
                }
                if(c[l][i]&&c[i][r]&&a[i]-a[l]==a[r]-a[i]&&dp[l+1][i-1]==(i-l-1)&&dp[i+1][r-1]==(r-i-1))
                {
                    dp[l][r]=max(dp[l][r],r-l+1);
                    dp2[l][r]=max(dp2[l][r],sum[r]-sum[l-1]);
                }
            }
        }
    }
}
void solve()
{
    init();
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&b[i]);
    for(int i=1;i<=n;i++)
        sum[i]=sum[i-1]+b[i];
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            if(check(i,j))
                c[i][j]=1;
    solvedp();
    cout<<dp2[1][n]<<endl;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)solve();
    return 0;
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5890963.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞