1. 程式人生 > >【HDU - 5900】QSC and Master(區間dp)

【HDU - 5900】QSC and Master(區間dp)

題幹:

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

題目大意:

   給一串數的key和value,如果相鄰兩元素key不是互質的就可以將這倆移除並獲得這倆的value值,移除後兩側的元素便是相鄰了,問最終最大能獲得多少value值。

每次合併兩個數之後,這兩個數就會消失,然後旁邊的數會接上。比如3 2 4 6 合併了 2 4 則 剩下3 6也可以合併

解題報告:

   是 石子合併 那道題的晉升版,,稍微改一改就可以了、、採用 先繼承 後擴充套件 的 方式寫、、不難想出來(話說還是對區間dp不是很熟啊剛開始就亂搞一通、、想明白了其實就很簡單了)

AC程式碼:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 500 + 5;
int n;
ll dp[MAX][MAX];
bool bk[MAX][MAX];
ll a[MAX],val[MAX];
ll sum[MAX];
int main() {
	int t;
	cin>>t;
	while(t--) {
		memset(dp,0,sizeof dp);
		memset(sum,0,sizeof sum);
		scanf("%d",&n);
		for(int i = 1; i<=n; i++) scanf("%lld",a+i);
		for(int i = 1; i<=n; i++) scanf("%lld",val+i),sum[i] = sum[i-1]+val[i];
		for(int i = 1; i<n; i++) {
			if(__gcd(a[i],a[i+1])>1) {
				bk[i][i+1]=1,dp[i][i+1]=val[i]+val[i+1];
			}
			bk[i][i]=1;//dp[i][i]=val[i];
		}
		for(int len=3; len<=n; len++) {
			for(int l=1; l<=n-len+1; l++) {
				int r=l+len-1;
				for(int k = l; k<r; k++) {
					dp[l][r] = max(dp[l][r],dp[l][k]+dp[k+1][r]);
					
				}
				if(dp[l+1][r-1] == sum[r-1]-sum[l] && __gcd(a[l],a[r])>1) {
					dp[l][r] = max(dp[l][r],dp[l+1][r-1] + val[l] + val[r]);
				}
			}
		}
		printf("%lld\n",dp[1][n]);
	}
	return 0 ;
}

附另一種寫法

區間DP,區間長度為1時dp[i][i]為0,新增一個標記陣列vis[i][j],記錄區間內是否所有數字都為可以移除,每次處理需要討論最後留下區間兩側的情況。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
int key[505],val[505],n;
bool vis[505][505];//1表示區間內全部用到,0表示區間內有沒用到的。
LL dp[505][505];
int gcd(int x,int y){
    return y==0?x:gcd(y,x%y);
}
int main(){
    int t; cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>key[i];
        for(int i=1;i<=n;i++)
            cin>>val[i];
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<n;i++){
            if(gcd(key[i],key[i+1])!=1){
                dp[i][i+1]=val[i]+val[i+1];
                vis[i][i+1]=1;
            }
        }
        for(int l=3;l<=n;l++)
        for(int i=1;i<=n-l+1;i++){
            int j=i+l-1;
            for(int k=i;k<j;k++){
                if(vis[i][k]&&vis[k+1][j])
                    vis[i][j]=1;
                dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
            }
            if(vis[i+1][j-1]&&gcd(key[i],key[j])!=1){
                vis[i][j]=1;
                dp[i][j]=dp[i+1][j-1]+val[i]+val[j];
            }
        }
        cout<<dp[1][n]<<endl;
    }

    return 0;
}

其實和我的那個寫法差不多,,只不過判斷i+1 j-1的方法不太一樣,,我是維護了一個字首和,,他是用vis標記搞的。