QSC and Master (区间dp)

QSC and Master

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


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
 
题意:
 
给n对数,如果相邻的一对数的第一个的gcd!=1,那么这两个数就可以一块拿走,获得第二个数的和的收益,求最大的收益;



区间dp的一般思路:

1 枚举区间长度

2 枚举起点

3考虑区间扩展方式递推大区间

本题递推方式为     区间 两边各加一个元素了l,r,如果gcd(l,r) != 1  并且 该区间内·所有元素都已经合并,那么

就可以扩充区间了。做个前缀和就能判断该区间内·所有元素是否都已经合并。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[1005][1005];
int mk[1005][1005];
ll key[1005],val[1005];
ll sum[1005];
ll gcd(ll a, ll b)
{
	return b == 0 ? a : gcd(b, a%b);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
    	int n;
    	scanf("%d",&n);
    	for(int i = 1; i <= n; i++)
    	{
    		scanf("%lld",&key[i]);
		}
		sum[0] = 0;
		for(int i = 1; i <= n; i++)
		{
			scanf("%lld",&val[i]);
			sum[i]=val[i] + sum[i-1];
		}
		memset(dp,0,sizeof(dp));
		for(int i = 2; i <= n; i++)
		for(int j = 1; j <= n-i+1; j++)
		{
			for(int k = 1; k < i; k++)
			{
				dp[j][i+j-1] = max(dp[j][j+k-1]+dp[j+k][i+j-1],dp[j][i+j-1]);
			}
			if(gcd(key[j],key[i+j-1]) != 1 && sum[i+j-2]-sum[j] == dp[j+1][i+j-2])
			{
 				dp[j][i+j-1] = max(dp[j+1][j+i-2]+val[j]+val[i+j-1],dp[j][i+j-1]);
			}
		}
		cout <<dp[1][n]<< endl;
	}
	return 0;
}



猜你喜欢

转载自blog.csdn.net/sunmoonvocano/article/details/79514971