zoukankan      html  css  js  c++  java
  • 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('
    ');
    }
      
    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;
    }
    

      

  • 相关阅读:
    JavaScript 正则表达式
    android源代码提示文本框还能输入多少个字符
    js实现鼠标点击input框后里面的内容就消失代码
    使用prompt输入一句英文句子和排序方式(升/降),将所有单词按排序方式排序后在网页上输出
    Javascript输出表格
    flutter 按键监听
    3.声明
    2.基础类型
    1.安装TypeScrpit
    苹果app证书
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5889619.html
Copyright © 2011-2022 走看看