hdu-5900 QSC and Master(区间dp)
生活随笔
收集整理的這篇文章主要介紹了
hdu-5900 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
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[l][r]表示區間[l,r]的最大收益,轉移的時候可以發現要么是分成兩段dp[l][r]=max(dp[l][k],dp[k+1][r]); 要么是把中間的都取完,然后讓a[l]和a[r]一塊取,那么就要判斷中間的[l+1,r-1]是否能取完, 用前綴和sum[r]-sum[l-1]==dp[l][r]就可以說區間都被取完了,因為這些數都是正數; AC代碼: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map>using namespace std;#define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss));typedef long long LL;template<class T> void read(T&num) {char CH; bool F=false;for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) {if(!p) { puts("0"); return; }while(p) stk[++ tp] = p%10, p/=10;while(tp) putchar(stk[tp--] + '0');putchar('\n'); }const LL mod=1e9+7; const double PI=acos(-1.0); const LL inf=1e18; const int N=(1<<20)+10; const int maxn=300+10; const double eps=1e-12;LL a[maxn],b[maxn],sum[maxn],dp[maxn][maxn]; LL gcd(LL x,LL y) {if(y==0)return x;return gcd(y,x%y); } int main() {int t,n;read(t);while(t--){read(n);For(i,1,n)read(a[i]);For(i,1,n)read(b[i]),sum[i]=sum[i-1]+b[i];mst(dp,0);for(int r=1;r<=n;r++){for(int l=r-1;l>0;l--){for(int k=l;k<=r;k++)dp[l][r]=max(dp[l][r],dp[l][k]+dp[k+1][r]);if(gcd(a[l],a[r])!=1&&dp[l+1][r-1]==sum[r-1]-sum[l])dp[l][r]=max(dp[l+1][r-1]+b[l]+b[r],dp[l][r]);}}print(dp[1][n]);}return 0; }
轉載于:https://www.cnblogs.com/zhangchengc919/p/5889619.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的hdu-5900 QSC and Master(区间dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hihocoder 网络流二·最大流最小
- 下一篇: activeMQ 安装于使用