Polygon (p1179)

来源:互联网 发布:网络推广的综合方案 编辑:程序博客网 时间:2024/04/18 18:30

这个题自己看了题还是知道些思路的,但就是感觉自己比较懒,,不好。还是参考了一下人家的代码。自己的代码能力还是很弱啊。

题意: 把一个圈(点为数字,边为乘或是加的去处操作) 先是去一条边,,然后就依次去边把边两端点的数进行运算,直到得到一个最大的数。

    输出最后最大运算数,并输出得到这个最大数时第一条边去掉的是哪些边可以得到。


思路:把该圈打开并使其长度为原来的两倍,这样一个一个开口进行计算出其最大值,dp[i][j][0]代表从点 i 到点 j 的最小值,dp[i][j][1]则为最大值(其中负负相乘得正,

   所以要有最小值也会记录)


#include<iostream>#include<cstdio>#include<algorithm>#include<queue>#include<vector>#include<cmath>#include<set>#include<cstdlib>#include<cstring>#include<stack>#include<string>using namespace std;//freopen("C://i.txt","r",stdin);#define N 111#define inf 10000000;int a[N];int dp[N][N][2];int ans[N];char c[N],cn[N];int an[N];int n;void init(){int i,j;for (i=1;i<=n;i++){for (j=1;j<=n;j++){dp[i][j][0]=inf;dp[i][j][1]=-inf;}}}int cal(int a,int b,char c){if (c=='t')return a+b;elsereturn a*b;}int main(){freopen("C://i.txt","r",stdin);int i,j,k;while (cin>>n){for (i=1;i<=n;i++){cin>>c[i];c[i+n]=c[i];cin>>a[i];a[i+n]=a[i];}int big=-inf;for (int d=1;d<=n;d++){int s=d;for (i=1;i<=n;i++){an[i]=a[s++];cn[i]=c[s];}init();for (int len=1;len<=n;len++){for (i=1;i+len-1<=n;i++){if (len==1){dp[i][i][0]=min(dp[i][i][0],an[i]);dp[i][i][1]=max(dp[i][i][1],an[i]);}j=i+len-1;for (k=i;k<j;k++){dp[i][j][0]=min(dp[i][j][0],cal(dp[i][k][0],dp[k+1][j][0],cn[k]));dp[i][j][0]=min(dp[i][j][0],cal(dp[i][k][1],dp[k+1][j][0],cn[k]));dp[i][j][0]=min(dp[i][j][0],cal(dp[i][k][1],dp[k+1][j][1],cn[k]));dp[i][j][0]=min(dp[i][j][0],cal(dp[i][k][0],dp[k+1][j][1],cn[k]));dp[i][j][1]=max(dp[i][j][1],cal(dp[i][k][0],dp[k+1][j][0],cn[k]));dp[i][j][1]=max(dp[i][j][1],cal(dp[i][k][1],dp[k+1][j][0],cn[k]));dp[i][j][1]=max(dp[i][j][1],cal(dp[i][k][1],dp[k+1][j][1],cn[k]));dp[i][j][1]=max(dp[i][j][1],cal(dp[i][k][0],dp[k+1][j][1],cn[k]));}}}ans[d]=dp[1][n][1];}for (i=1;i<=n;i++){big=max(big,ans[i]);}cout<<big<<endl;for (i=1;i<=n;i++){if (big==ans[i])cout<<i<<' ';}cout<<endl;}}



Polygon
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4058 Accepted: 1675

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

4t -7 t 4 x 2 x 5

Sample Output

331 2

Source




原创粉丝点击