负数的处理POJ1179Polygon
Posted chanceyu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了负数的处理POJ1179Polygon相关的知识,希望对你有一定的参考价值。
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.
Instruction
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
Analysis
最大值需要分6种情况:++,--,+-,(+-)+,(+-)-
相乘有maxmax,minmin,maxmin三种
相加只有maxmax
Code
#include<algorithm>
#include<set>
using namespace std;
int dp[100][100],xp[100][100],c[100];
set<int>v;
int main(){
int n,i,j,x,sum=0;scanf("%d
",&n);
for(i=n-1;i>=0;--i){
scanf("%c %d",&c[i],&dp[i][i]);
c[i+n]=c[i];xp[i][i]=xp[i+n][i+n]=dp[i+n][i+n]=dp[i][i];
if(i!=0)getchar();
}
for(int z=1;z<n;++z)for(i=0;(j=i+z)<2*n;++i){
xp[i][j]=5000000;dp[i][j]=-5000000;
for(int k=i;k<j;++k){
if(c[k]=='t'){
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
xp[i][j]=min(xp[i][j],xp[i][k]+xp[k+1][j]);
}
else{
int y[5]={dp[i][j],dp[i][k]*dp[k+1][j],xp[i][k]*xp[k+1][j],dp[i][k]*xp[k+1][j],xp[i][k]*dp[k+1][j]};
dp[i][j]=*max_element(y,y+5);y[0]=xp[i][j];
xp[i][j]=*min_element(y,y+5);
}
}
}
for(i=0;i<n;++i){
x=(i==0)?1:(n+1-i);
if(dp[i][i+n-1]>sum){
v.clear();v.insert(x);sum=dp[i][i+n-1];
}
else if(dp[i][i+n-1]==sum)v.insert(x);
}
printf("%d
",sum);
for(set<int>::iterator it=v.begin();it!=v.end();++it){
if(it!=v.begin())printf(" ");
printf("%d",*it);
}
return 0;
}
以上是关于负数的处理POJ1179Polygon的主要内容,如果未能解决你的问题,请参考以下文章