日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hdu-5900 QSC and Master(区间dp)

發布時間:2025/7/14 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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


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[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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。