zoukankan      html  css  js  c++  java
  • hdu5900_区间dp

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

    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
     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <cstdio>
     6 #include <vector>
     7 #include <ctime>
     8 #include <queue>
     9 #include <list>
    10 #include <set>
    11 #include <map>
    12 using namespace std;
    13 #define INF 0x3f3f3f3f
    14 typedef long long LL;
    15 
    16 LL a[330], b[330], dp[330][330], sum[330];
    17 int Gcd(int x, int y)
    18 {
    19     return y==0 ? x : Gcd(y, x%y);
    20 }
    21 int main()
    22 {
    23     int t, n;
    24     scanf("%d", &t);
    25     while(t--)
    26     {
    27         scanf("%d", &n);
    28         for(int i = 1; i <= n; i++)
    29             scanf("%lld", &a[i]);
    30         sum[0] = 0;
    31         for(int i = 1; i <= n; i++){
    32             scanf("%lld", &b[i]);
    33             sum[i] = sum[i-1] + b[i];
    34         }
    35         memset(dp, 0, sizeof(dp));
    36         for(int len = 1; len < n; len++)
    37         {
    38             for(int i = 1; i <= n && len+i <= n; i++)
    39             {
    40                 int j = i + len;
    41                 if((dp[i+1][j-1] == sum[j-1]-sum[i] || i == j-1) && (Gcd(a[i], a[j]) != 1))//刚开始写成dp[i+1][j-1] != 0,原来是dp[i+1][j-1] == sum[j-1]-sum[i]
    42                     dp[i][j] = max(dp[i][j], dp[i+1][j-1]+b[i]+b[j]);
    43                 for(int k = i; k <= j; k++)
    44                     dp[i][j] = max(dp[i][k]+dp[k+1][j], dp[i][j]);
    45             }
    46         }
    47         printf("%lld
    ", dp[1][n]);
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    HTML5本地存储应用sessionStorage和localStorage
    [js高手之路] 设计模式系列课程
    [js高手之路] vue系列教程
    [js高手之路] vue系列教程
    [js高手之路] vue系列教程
    [js高手之路] vue系列教程
    [js高手之路] vue系列教程
    [js高手之路] vue系列教程
    js单页hash路由原理与应用实战
    javascript面向对象的常见写法与优缺点
  • 原文地址:https://www.cnblogs.com/luomi/p/5971147.html
Copyright © 2011-2022 走看看