程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 2016 年沈陽網絡賽---QSC and Master(區間DP),---qscdp

2016 年沈陽網絡賽---QSC and Master(區間DP),---qscdp

編輯:C++入門知識

2016 年沈陽網絡賽---QSC and Master(區間DP),---qscdp


題目鏈接

http://acm.hdu.edu.cn/showproblem.php?pid=5900

 

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  

 

Recommend wange2014   |   We have carefully selected several similar problems for you:  5901 5899 5898 5897 5896      題意:n 個pair<int , int>,每次可以選相鄰兩個pair。如果他們的first不互質就可以把它們都刪掉,並且獲得second之和的分數,問最大得分。   思路:區間DP,類似於括號匹配的題(一個括號序列,要求添加最少的括號使這個括號序列匹配) 定義dp[i][j] 表示i~j的區間能得的最大分,那麼有狀態轉移方程:dp[i][j]=dp[i][k]+dp[k+1][j]   另外注意特判取兩邊時的情形,即區間 i+1~j-1 都刪除了(判斷條件dp[i+1][j-1]==sum(b[i+1]+....+b[j-1])),如果gcd(a[i],a[j])>1 dp[i][j]=dp[i+1][j-1]+b[i]+b[j];   代碼如下:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cmath>
#include <string.h>
using namespace std;
long long a[305],b[305];
long long dp[305][305];
long long sum[305];
long long GCD(long long a,long long b)
{
    return  (b==0)?a:GCD(b,a%b);
}
int main()
{
    int T,N;
    cin>>T;
    while(T--)
    {
        scanf("%d",&N);
        for(int i=1;i<=N;i++)
            scanf("%lld",&a[i]);
        sum[0]=0;
        for(int i=1;i<=N;i++)
        {
            scanf("%lld",&b[i]);
            sum[i]=sum[i-1]+b[i];
        }
        memset(dp,0,sizeof(dp));
        for(int len=1;len<N;len++)
        {
            for(int i=1;i+len<=N;i++)
            {
                if(sum[i+len-1]-sum[i]==dp[i+1][i+len-1])
                {
                    dp[i][i+len]=dp[i+1][i+len-1];
                    if(GCD(a[i],a[i+len])>1) dp[i][i+len]+=b[i]+b[i+len];
                }
                for(int k=i;k<i+len;k++)
                {
                    dp[i][i+len]=max(dp[i][i+len],dp[i][k]+dp[k+1][i+len]);
                }
            }
        }
        printf("%lld\n",dp[1][N]);
    }
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved