HDU 5900 QSC and Master (区间DP 记忆化搜索)

来源:互联网 发布:mac vps ss 编辑:程序博客网 时间:2024/05/16 00:27

大体题意:

给你n 个数,你可以删除两个数,这两个数必须相邻,而且他们gcd 不是1,删除之后你的sum +=  他们的权值,求最大权值!

思路:

比赛队友做的,现在补一补:

区间DP记忆化搜索:

如果一个区间[x,y]  x >= y 则肯定是0。

如果x + 1 == y  这是边界情况,则要看这两个数是否互质,不互质的话就返回权值之和,否则 返回0。

否则   如果两个端点 不互质, 我们就要枚举每一个数之间的权值之和等于缩区间后的dfs,那么就是合理的。

否则  端点不互质与互质一并处理,求一个最大的子区间!

注意 要先预处理前缀和 否则会超时

详细见代码:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 300 + 10;typedef long long ll;ll id[maxn], key[maxn], dp[maxn][maxn];ll sumid[maxn],sumkey[maxn];ll gcd(ll a,ll b){return !b ? a : gcd(b,a%b);}ll dfs(ll x,ll y){ll& ans = dp[x][y];if (x >= y)return ans = 0;if (~ans) return ans;ll t = gcd(id[x],id[y]);if (x + 1 == y){if (t != 1)return ans = key[x] + key[y];return ans = 0;}if (t != 1){if (sumkey[y-1] - sumkey[x] == dfs(x+1,y-1)){ans = sumkey[y] - sumkey[x-1];}}for (int i = x; i <= y; ++i){ans = max(ans,dfs(x,i)+dfs(i+1,y));}return ans;}int main(){int T;scanf("%d",&T);while(T--){int n;scanf("%d", &n);for (int i = 1; i <= n; ++i)scanf("%I64d",&id[i]),sumid[i] = sumid[i-1] + id[i];for (int i = 1; i <= n; ++i)scanf("%I64d",&key[i]),sumkey[i] = sumkey[i-1] + key[i];memset(dp,-1,sizeof dp);ll ans = dfs(1,n);printf("%I64d\n",ans);}return 0;}


QSC and Master
Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %I64d & %I64u

Submit Status

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

331 2 31 1 131 2 41 1 141 3 4 31 1 1 1
 

Sample Output

020
 

Source

2016 ACM/ICPC Asia Regional Shenyang Online

0 0