HDU5900-QSC and Master

QSC and Master

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

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


题意:给出n个pair<int,int>,每次可以选择相邻的两个pair,如果他们的first不互质就可以把他们取出来,并且获得second的分数之和,问最大的得分

解题思路:设dp[i][j]为区间[i,j]所能获得的最大得分,转移方程就是断开拼起来(包括不取左边和不取右边)和取两边,其中取两边需要中间取完了才能取

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>

using namespace std;

#define ll long long

int n;
ll a[305],b[305];
struct node
{
    ll value;
    int flag;
} dp[305][305];

int gcd(int c,int d)
{
    if(d<c) return gcd(d,c);
    while(d%c)
    {
        int t=d%c;
        d=c;
        c=t;
    }
    return c;
}

int check(int c,int d)
{
    if(gcd(c,d)==1) return 0;
    else return 1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%lld",&a[i]);
        for(int i=0; i<n; i++)
            scanf("%lld",&b[i]);
        memset(dp,0,sizeof dp);
        for(int k=1; k<=n-1; k++)
        {
            for(int i=0; i<n-k; i++)
            {
                if(k==1)
                {
                    if(check(a[i],a[i+k]))
                        dp[i][i+k].value=b[i]+b[i+k],dp[i][i+k].flag=1;
                }
                else
                {
                    if(check(a[i],a[i+k])&&dp[i+1][i+k-1].flag)
                    {
                        if(dp[i][i+k].value<=dp[i+1][i+k-1].value+b[i]+b[i+k])
                        {
                            dp[i][i+k].value=dp[i+1][i+k-1].value+b[i]+b[i+k];
                            dp[i][i+k].flag=1;
                        }
                    }
                    for(int j=i; j<i+k; j++)
                    {
                        if(dp[i][i+k].value<=dp[i][j].value+dp[j+1][i+k].value)
                        {
                            dp[i][i+k].value=dp[i][j].value+dp[j+1][i+k].value;
                            if(dp[i][j].flag&&dp[j+1][i+k].flag)
                            {
                                dp[i][k+i].flag=1;
                            }
                        }
                    }
                }
            }
        }
        printf("%lld\n",dp[0][n-1].value);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值