国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ 1179 Polygon(區間DP)

2019-11-08 02:15:25
字體:
來源:轉載
供稿:網友

題目鏈接:http://poj.org/PRoblem?id=1179

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. Figure 1

On the first move, one of the edges is removed. Subsequent moves involve the following steps: 1. pick an edge E and the two vertices V1 and V2 that are linked by E; and 2. 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. Figure 2

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

IOI 1998

解題思路

這道題的題意大致是對于n個點的環先刪除一個點后,根據連線的運算符逐漸合并各個點,最后求最大的結果可能是多少。一道經典的區間DP題,有幾個點需要注意一下:

需要枚舉最開始刪除的是哪條邊 由于一開始是個環,刪邊后可能從中間斷開,導致區間不連續,無法使用區間DP。解決方法是將原始數組復制一倍,這樣就能保證能夠使用區間DP。 由于元素可能存在負值,負負相乘得正后可能得到的結果更大。所以在維護結果的時候,既要維護最大值,也要維護最小值。

AC代碼

#include<bits/stdc++.h>//#define TESTusing namespace std;#define INF 400000int n,ans;char op[105];int num[105],ma[105][105],mi[105][105];vector<int> lt;int mmax(int s,int k,int t,char z){ if(z=='t') return ma[s][k-1]+ma[k][t]; else return max(ma[s][k-1]*ma[k][t],mi[s][k-1]*mi[k][t]);}int mmin(int s,int k,int t,char z){ if(z=='t') return mi[s][k-1]+mi[k][t]; else return min(ma[s][k-1]*ma[k][t],mi[s][k-1]*mi[k][t]);}int main(){ scanf("%d/n",&n); for(int i=0;i<n;i++){ scanf("%c %d%*c",&op[i],&num[i]); op[i+n]=op[i]; num[i+n]=num[i]; // 增加一倍長度 } for(int i=0;i<2*n;i++){ for(int j=0;j<2*n;j++){ ma[i][j]=-INF; mi[i][j]=INF; } } for(int i=0;i<(n<<1);i++){ ma[i][i]=mi[i][i]=num[i]; } #ifdef TEST for(int i=0;i<2*n;i++){ for(int j=0;j<2*n;j++){ cout<<ma[i][j]<<' '; } cout<<endl; } #endif // TEST for(int d=1;d<=n;d++){ for(int s=0;s<2*n-d+1;s++){ int t=s+d-1; for(int k=s+1;k<=t;k++){ ma[s][t]=max(ma[s][t],mmax(s,k,t,op[k])); mi[s][t]=min(mi[s][t],mmin(s,k,t,op[k])); } } } #ifdef TEST for(int i=0;i<2*n;i++){ for(int j=0;j<2*n;j++){ cout<<ma[i][j]<<' '; } cout<<endl; } #endif // TEST ans=-INF; lt.clear(); for(int i=0;i<n;i++){ if(ma[i][i+n-1]>ans){ ans=ma[i][i+n-1]; lt.clear(); lt.push_back(i+1); } else if(ma[i][i+n-1]==ans){ lt.push_back(i+1); } } printf("%d/n",ans); printf("%d",lt[0]); for(int i=1;i<lt.size();i++){ printf(" %d",lt[i]); } printf("/n"); return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鲁山县| 仁化县| 仙桃市| 平邑县| 高要市| 通江县| 武穴市| 蓬安县| 滁州市| 虞城县| 博乐市| 泰兴市| 达拉特旗| 阳江市| 沙田区| 宜黄县| 萨嘎县| 彭阳县| 江川县| 册亨县| 突泉县| 阳新县| 澄城县| 兴和县| 沙坪坝区| 万年县| 定远县| 镶黄旗| 商洛市| 浦北县| 澄迈县| 乾安县| 天津市| 宜良县| 乳山市| 溆浦县| 鹤山市| 嘉禾县| 且末县| 镇原县| 昔阳县|