zoukankan      html  css  js  c++  java
  • [IOI1998] Polygon (区间dp)

    Description

    Polygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an integer and each edge is labelled with either the symbol + (addition) or the symbol * (product). The edges are numbered from 1 to N.

    On the first move, one of the edges is removed. Subsequent moves involve the following steps:
    �pick an edge E and the two vertices V1 and V2 that are linked by E; and
    �replace them by a new vertex, labelled with the result of performing the operation indicated in E on the labels of V1 and V2.
    The game ends when there are no more edges, and its score is the label of the single vertex remaining.

    Consider the polygon of Figure 1. The player started by removing edge 3. After that, the player picked edge 1, then edge 4, and, finally, edge 2. The score is 0.

    Write a program that, given a polygon, computes the highest possible score and lists all the edges that, if removed on the first move, can lead to a game with that score.
    Input

    Your program is to read from standard input. The input describes a polygon with N vertices. It contains two lines. On the first line is the number N. The second line contains the labels of edges 1, …, N, interleaved with the vertices’ labels (first that of the vertex between edges 1 and 2, then that of the vertex between edges 2 and 3, and so on, until that of the vertex between edges N and 1), all separated by one space. An edge label is either the letter t (representing +) or the letter x (representing *).

    3 <= N <= 50
    For any sequence of moves, vertex labels are in the range [-32768,32767].
    Output

    Your program is to write to standard output. On the first line your program must write the highest score one can get for the input polygon. On the second line it must write the list of all edges that, if removed on the first move, can lead to a game with that score. Edges must be written in increasing order, separated by one space.
    Sample Input

    4
    t -7 t 4 x 2 x 5
    Sample Output

    33
    1 2

    基本思想和普通区间dp一样,不过由于有负数 此时求最大值需同时考虑最大值和最小值,也就是保留两个信息
    分情况讨论一下就行(不要缺情况就行qwq)

    code:(评测:luogu P4342)

    //By Menteur_Hxy
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    
    int rd() {
        int x=0,fla=1; char c=' ';
        while(c>'9'|| c<'0') {if(c=='-') fla=-fla; c=getchar();}
        while(c<='9'&&c>='0') x=x*10+c-'0',c=getchar();
        return x*fla;
    }
    
    const int MAX=210;
    const int INF=0x7fffffff;
    int n,maxn=-INF;
    int a[MAX],c[MAX],f[MAX][MAX][2];// 1最大值 0最小值 
    
    int main() {
        n=rd();
        getchar();//一开始忘了吞换行 WA了两次QAQ
        for(int i=1;i<=n;i++) c[i]=c[i+n]=(char(getchar())=='t'),a[i]=a[i+n]=rd();
    //  for(int i=1;i<=n<<1;i++) cout<<a[i]<<" ";
    //  cout<<endl;
        for(int i=1;i<=n<<1;i++)
            for(int j=1;j<=n<<1;j++) 
                f[i][j][1]=-INF,f[i][j][0]=INF;
        for(int i=1;i<=n<<1;i++) f[i][i][1]=f[i][i][0]=a[i];
        for(int len=1;len<=n;len++) {
            for(int l=1;l<=n<<1&&len+l-1<=n<<1;l++) {
                int r=len+l-1;
    //          cout<<l<<" "<<r<<" "<<f[l][r][0]<<" "<<f[l][r][1]<<endl;
                for(int k=l;k<r;k++)  {
                    if(c[k+1]) {//如果是加法
                        f[l][r][0]=min(f[l][r][0],f[l][k][0]+f[k+1][r][0]);
                        f[l][r][1]=max(f[l][r][1],f[l][k][1]+f[k+1][r][1]);
                        continue;
                    }//不然就是乘法
                    f[l][r][0]=min(f[l][r][0],f[l][k][0]*f[k+1][r][0]);
                    f[l][r][0]=min(f[l][r][0],f[l][k][1]*f[k+1][r][0]);
                    f[l][r][0]=min(f[l][r][0],f[l][k][0]*f[k+1][r][1]);
                    f[l][r][1]=max(f[l][r][1],f[l][k][1]*f[k+1][r][1]);
                    f[l][r][1]=max(f[l][r][1],f[l][k][0]*f[k+1][r][0]);
                }
    //          cout<<l<<" "<<r<<" "<<f[l][r][0]<<" "<<f[l][r][1]<<endl;
            }   
        }
        for(int l=1,r=l+n-1;l<=n;l++,r++) 
            if(maxn<f[l][r][1]) maxn=f[l][r][1];
        printf("%d
    ",maxn);
        for(int l=1,r=l+n-1;l<=n;l++,r++) 
            if(f[l][r][1]==maxn) printf("%d ",l);
        return 0;
    }

    另外附上数据点(5个) :

    5
    x 2 x 3 t 1 t 7 x 4
    
    224
    4
    
    
    5
    x -3 t -1 t -7 t -4 x -2
    
    30
    1 5
    
    
    3
    t 0 x 1 t -2
    
    0
    1
    
    
    30
    x 1 t 1 x 1 t 1 t 1 x 1 x 1 x 1 x 1 x 1 x 1 t 1 t 1 x 1 t 1 x 1 x 1 t 1 x 1 x 1 t 1 x 1 x 1 x 1 x 1 x 1 t 1 x 1 x 1 x 1
    
    288
    1 3 6 7 8 9 10 11 14 16 17 19 20 22 23 24 25 26 28 29 30
    
    
    48
    x 1 x 2 x 1 x -1 t 1 x -1 x -1 x 1 t 1 t -1 x 1 t 2 x 1 x 2 t 1 x 1 x -1 x -2 x 1 x 1 t 1 x 1 t 1 x 1 x 1 x 1 t 1 x 1 x 1 x 1 x 1 x 1 x 1 x -1 t 1 x 1 x -1 x -1 t 1 x 1 t 1 x 1 x 1 x -1 t 1 t -1 t -1 x 1
    
    23328
    45
    版权声明:本文为博主原创文章,未经博主允许不得转载。 博主:https://www.cnblogs.com/Menteur-Hxy/
  • 相关阅读:
    [Sdoi2016]征途
    何时会发生db file sequential read等待事件?
    oracle分布式事务总结
    db file scattered read 等待事件
    db file sequential read (数据文件顺序读取)
    INDEX_JOIN
    Oracle优化器介绍
    GC Buffer Busy Waits处理(转载)
    Oracle V$SESSION详解
    Oracle锁表(转载)
  • 原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9247984.html
Copyright © 2011-2022 走看看