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

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=1e3+10,M=1e6+1010,inf=1e9+10,mod=1e9+7;
const ll INF=1e18+10;
ll dp[N][N];
ll a[N];
ll b[N];
ll sum[N];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));
        memset(sum,0,sizeof(sum));
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
        for(int i=1;i<=n;i++)
        scanf("%lld",&b[i]),sum[i]=sum[i-1]+b[i];
        for(int i=2;i<=n;i++)
        {
            for(int t=1;t+i-1<=n;t++)
            {
                if(__gcd(a[t],a[t+i-1])!=1&&sum[t+i-2]-sum[t]==dp[t+1][t+i-2])
                {
                    dp[t][t+i-1]=sum[t+i-1]-sum[t-1];
                    continue;
                }
                for(int j=t;j<=t+i-2;j++)
                {
                    dp[t][t+i-1]=max(dp[t][t+i-1],dp[t][j]+dp[j+1][t+i-1]);
                }
            }
        }
        printf("%lld\n",dp[1][n]);
    }
    return 0;
}
时间: 2024-08-04 23:06:29

hdu 5900 QSC and Master 区间dp的相关文章

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,但是我比赛中如何也

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

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

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 4960 Another OCD Patient 区间dp

区间dp.. T^T一直感觉是n^3,看了题解看来是数据水了么.. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string.h> #define ll long long #define inf 1e8 inline int min(int a, int b){return a<b?a:b;} inline void rdl(ll

hdu 5151 Sit sit sit(区间dp+排列组合)

题目链接:hdu 5151 Sit sit sit 题意: 一共有并排N个椅子, N个学生依次去坐,同时满足3个条件就不能坐下去: 1,该椅子不在最左,不在最右. 2,该椅子左右都有人坐了. 3,左右的椅子不同颜色.求最后N个人都能坐下去,有多少不同的情况. 题解: 考虑区间dp,dp[i][j] = sum(dp[i][k-1] * dp[k+1][j] * c[j - i][k - i])其中满足(v[k-1]==v[k+1]) 表示i到j区间最后来坐k位置,乘组合是因为合并这两段区间的时候

HDU 2476 String painter(区间DP啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 Problem Description There are two strings A and B with equal length. Both strings are made up of lower case letters. Now you have a powerful string painter. With the help of the painter, you can cha

HDU 2476 String painter (区间DP)

题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成b的次数 ans[i]:a从(0,i)变成b(0,i)所需的最小次数 初始化ans[i]=dp[0][i] 如果a[i]==b[i],则ans[i]=ans[i-1]; 由小区间更新到大区间 1 //#pragma comment(linker, "/STACK:167772160")//

hdu 5115 Dire Wolf(区间dp)

Problem Description Dire wolves, also known as Dark wolves, are extraordinarily large and powerful wolves. Many, if not all, Dire Wolves appear to originate from Draenor. Dire wolves look like normal wolves, but these creatures are of nearly twice th