zoukankan      html  css  js  c++  java
  • poj 1179 Polygon

    Polygon
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 4464   Accepted: 1859

    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

    Source


    // 区间 dp , 本质是石子合并
    #include <iostream> #include <algorithm> #include <queue> #include <math.h> #include <stdio.h> #include <string.h> using namespace std; #define MOD 1000000007 int dp_min[110][110],dp_max[110][110]; // dp[i][j]代表 第 i 和 j 个数运算到成一个数时的 最大/小值 int v[110]; char op[110]; int main() { int n; int i,j,k; while(scanf("%d",&n)!=EOF){ getchar(); for(i=1;i<=n;i++){ scanf("%c %d",&op[i],&v[i]); getchar(); // printf("%c %d",op[i],v[i]); op[i+n]=op[i]; v[i+n]=v[i]; } //printf("??"); for(i=1;i<=2*n;i++) { dp_max[i][i]=dp_min[i][i]=v[i]; if(i<2*n){ if(op[i+1]=='t') dp_max[i][i+1]=dp_min[i][i+1]=v[i]+v[i+1]; else dp_max[i][i+1]=dp_min[i][i+1]=v[i]*v[i+1]; } } for(k=3;k<=n;k++) // k 代表求解区间的长度 for(i=1;i+k-1<=2*n;i++){ dp_max[i][i+k-1]=-MOD; dp_min[i][i+k-1]=MOD; for(j=i;j<i+k-1;j++){ if(op[j+1]=='t'){ dp_max[i][i+k-1]=max(dp_max[i][i+k-1],dp_max[i][j]+dp_max[j+1][i+k-1]); dp_min[i][i+k-1]=min(dp_min[i][i+k-1],dp_min[i][j]+dp_min[j+1][i+k-1]); } else{ dp_max[i][i+k-1]=max(dp_max[i][i+k-1],dp_max[i][j]*dp_max[j+1][i+k-1]); dp_max[i][i+k-1]=max(dp_max[i][i+k-1],dp_min[i][j]*dp_min[j+1][i+k-1]); dp_max[i][i+k-1]=max(dp_max[i][i+k-1],dp_max[i][j]*dp_min[j+1][i+k-1]); dp_max[i][i+k-1]=max(dp_max[i][i+k-1],dp_min[i][j]*dp_max[j+1][i+k-1]); dp_min[i][i+k-1]=min(dp_min[i][i+k-1],dp_max[i][j]*dp_max[j+1][i+k-1]); dp_min[i][i+k-1]=min(dp_min[i][i+k-1],dp_max[i][j]*dp_min[j+1][i+k-1]); dp_min[i][i+k-1]=min(dp_min[i][i+k-1],dp_min[i][j]*dp_max[j+1][i+k-1]); dp_min[i][i+k-1]=min(dp_min[i][i+k-1],dp_min[i][j]*dp_min[j+1][i+k-1]); } } } int out[55],cnt=0; int ans=-MOD; for(i=1;i<=n;i++){ if(dp_max[i][i+n-1]>ans){ ans=dp_max[i][i+n-1]; cnt=0; out[cnt]=i; }else if(dp_max[i][i+n-1]==ans){ out[++cnt]=i; } } printf("%d ",ans); for(i=0;i<cnt;i++) printf("%d ",out[i]); printf("%d ",out[i]); } return 0; }
  • 相关阅读:
    Making a CocoaPod
    关于Http
    The podfile
    iOS 8个实用小技巧(总有你不知道的和你会用到的)
    关于深拷贝浅拷贝
    适配ios10(iTunes找不到构建版本)
    iOS 10 推送的简单使用
    __block 和 __weak的区别
    Masonry使用注意事项
    iOS数字键盘自定义按键
  • 原文地址:https://www.cnblogs.com/372465774y/p/3183431.html
Copyright © 2011-2022 走看看