国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > 2016 ACM/ICPC Asia Regional Shenyang Online 1009 QSC and Master 区间dp

2016 ACM/ICPC Asia Regional Shenyang Online 1009 QSC and Master 区间dp

来源:程序员人生   发布时间:2016-12-03 10:22:56 阅读次数:6409次

QSC and Master

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


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
 


My Solution

     状态定义: dp[i][j][u]  u == 1 时表示 当端点 i, j 进行合并时(取出 val[i] 、 val[j] 时) 

                                                          或 i < k < k+1 < j, key[i] 和 key[k] 可以合并 且key[k+1] 和 key[j]合并的, 区间 [i, j]内的最大价值;

                                     u == 0 时表示 当端点 i, j 不进行合并时(不取出 val[i] 、 val[j] 时) 的 区间 [i, j]内的最大价值;


     初始化:全部初始化为 0;

     边界: 当 j - i == 1 时 如果可以合并 则 dp[i][j][1] = val[i] + val[j];

     状态转移方程:如果 key[i], key[j] 可以组合, 则 如果 dp[i + 1][j - 1][1] > 0 则 dp[i][j][1] = max(dp[i][j][1], dp[i+1][j⑴][1] + val[i] + val[j]);

                                                           然后对 区间划分 k = i ~ j 

                                                                    dp[i][j][0] = max(dp[i][j][0], max(dp[i][k][0], dp[i][k][1]) + max(dp[k+1][j][1] , dp[k+1][j][0]));
                                                                    if(dp[i][k][1] > 0 && dp[k+1][j][1] > 0) dp[i][j][1] = max(dp[i][j][1], dp[i][k][1] + dp[k+1][j][1]);


                                                                   接着是对u == 0时的转移(即keyi keyj 能合并时可以选择不合并) 

                                                                    dp[i][j][0] = max(dp[i][j][0], max(dp[i+1][j⑴][1], dp[i+1][j⑴][0]));


                            如果 key[i], key[j] 不能合并, 则
                                                                        if(dp[i+1][j⑴][1] > 0){
                                                                                   dp[i][j][0] = max(dp[i][j][0], max(dp[i+1][j⑴][1], dp[i+1][j⑴][0]));
                                                                                   for(k = i; k < j; k++){
                                                                                             dp[i][j][0] = max(dp[i][j][0], max(dp[i][k][0], dp[i][k][1]) + max(dp[k+1][j][1] , dp[k+1][j][0]));
                                                                                             if(dp[i][k][1] > 0 && dp[k+1][j][1] > 0) dp[i][j][1] = max(dp[i][j][1], dp[i][k][1] + dp[k+1][j][1]);
                                                                                   }


                                                                       }
                                                                      else{
                                                                                  dp[i][j][0] = max(dp[i][j][0], dp[i+1][j⑴][0]);
                                                                                  for(k = i; k < j; k++){
                                                                                            dp[i][j][0] = max(dp[i][j][0], max(dp[i][k][0], dp[i][k][1]) + max(dp[k+1][j][1] , dp[k+1][j][0]));
                                                                                            if(dp[i][k][1] > 0 && dp[k+1][j][1] > 0) dp[i][j][1] = max(dp[i][j][1], dp[i][k][1] + dp[k+1][j][1]);
                                                                                  }
                                                                     }


     复杂度 O(n^3)


#include <iostream> #include <cstdio> #include <cmath> #include <cstring> using namespace std; typedef long long LL; const int maxn = 3e2 + 8; LL key[maxn], val[maxn], dp[maxn][maxn][2]; inline LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); } inline bool check(LL a, LL b) { return gcd(a, b) != 1; } int main() { #ifdef LOCAL freopen("1009.txt", "r", stdin); //freopen("1009.out", "w", stdout); #endif // LOCAL // ios::sync_with_stdio(false); cin.tie(0); LL T, n; cin >> T; while(T--){ memset(dp, 0, sizeof dp); cin >> n; for(int i = 0; i < n; i++){ cin >> key[i]; } for(int i = 0; i < n; i++){ cin >> val[i]; } LL i, j, k, ans = 0, x, y; for(i = n - 1; i >= 0; i--){ for(j = i + 1; j < n; j++){ if(check(key[i], key[j])){ if(j - i > 2){ if(dp[i+1][j⑴][1] > 0){ dp[i][j][1] = max(dp[i][j][1], dp[i+1][j⑴][1] + val[i] + val[j]); } for(k = i; k < j; k++){ dp[i][j][0] = max(dp[i][j][0], max(dp[i][k][0], dp[i][k][1]) + max(dp[k+1][j][1] , dp[k+1][j][0])); if(dp[i][k][1] > 0 && dp[k+1][j][1] > 0) dp[i][j][1] = max(dp[i][j][1], dp[i][k][1] + dp[k+1][j][1]); } dp[i][j][0] = max(dp[i][j][0], max(dp[i+1][j⑴][1], dp[i+1][j⑴][0])); } else if(j - i == 1) dp[i][j][1] = val[i] + val[j]; } if(j - i > 2){ if(dp[i+1][j⑴][1] > 0){ dp[i][j][0] = max(dp[i][j][0], max(dp[i+1][j⑴][1], dp[i+1][j⑴][0])); for(k = i; k < j; k++){ dp[i][j][0] = max(dp[i][j][0], max(dp[i][k][0], dp[i][k][1]) + max(dp[k+1][j][1] , dp[k+1][j][0])); if(dp[i][k][1] > 0 && dp[k+1][j][1] > 0) dp[i][j][1] = max(dp[i][j][1], dp[i][k][1] + dp[k+1][j][1]); } } else{ dp[i][j][0] = max(dp[i][j][0], dp[i+1][j⑴][0]); for(k = i; k < j; k++){ dp[i][j][0] = max(dp[i][j][0], max(dp[i][k][0], dp[i][k][1]) + max(dp[k+1][j][1] , dp[k+1][j][0])); if(dp[i][k][1] > 0 && dp[k+1][j][1] > 0) dp[i][j][1] = max(dp[i][j][1], dp[i][k][1] + dp[k+1][j][1]); } } } else if(j - i == 1) dp[i][j][0] = 0; //这行可以疏忽 ^_^ //*/ ans = max(ans, max(dp[i][j][0], dp[i][j][1])); } } //cout << dp[0][n⑴][0] << " " << dp[0][n⑴][1]<< endl; if(ans > 0) cout << ans << "\n"; else cout << 0 << "\n"; } return 0; }

  Thank you!

                                                                                                                                               ------from ProLights

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生