Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5472   Accepted: 2334

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 <cstring>
#include <cstdio>
const int N = + ;
using namespace std ;
int n,a[N];
char ss[N];
long long f[N][N],g[N][N],ans; void Init()
{
scanf("%d",&n);
for(int i = ; i <= n ; ++i)
{
cin>>ss[i]>>a[i];
a[n+i]=a[i],ss[n+i]=ss[i];
}
} void Solve( )
{
for(int i = ; i <= (n<<) ; ++i) for(int j = ; j <= (n<<) ; ++j)f[i][j] = -0x3f3f3f3f ;
for(int i = ; i <= (n<<) ; ++i) for(int j = ; j <= (n<<) ; ++j)g[i][j] = 0x3f3f3f3f ;
for(int i = ; i <= (n<<) ; ++i)
{
if(ss[i+]=='t')
f[i][i+] = g[i][i+] = a[i] + a[i+];
else
f[i][i+] = g[i][i+] = a[i] * a[i+];
f[i][i] = g[i][i] = a[i];
}
long long ans = -0x3f3f3f3f ;
for(int i = (n<<);--i;)
for(int j = i + ; j <=(n<<);++j)
for(int k = i;k<j;++k)
if(ss[k+] == 't')
{
f[i][j] = max(f[i][j],f[i][k]+f[k+][j]);
g[i][j] = min(g[i][j],g[i][k]+g[k+][j]);
}
else
{
long long int a = f[i][k]*g[k+][j],b = f[i][k]*f[k+][j];
long long int c = g[i][k]*f[k+][j],d = g[i][k]*g[k+][j];
f[i][j] = max(f[i][j],max(max(a,b),max(c,d)));
g[i][j] = min(g[i][j],min(min(a,b),min(c,d)));
}
for(int i = ; i<= n ;++i)
ans = max(ans,f[i][i+n-]);
printf("%lld\n",ans);
for(int i=;i<=n;++i)
if(f[i][i+n-] == ans)
printf("%d ",i);
puts("");
} int main( )
{
// freopen("polygon.in","r",stdin);
// freopen("polygon.out","w",stdout);
Init();
Solve();
fclose(stdin);
fclose(stdout);
return ;
}

最新文章

  1. 在win8.1中安装apache+php+mysql
  2. JAVA 调用matlab 出错总结
  3. C++例题练习(2)
  4. building Utils {{ant+ivy}、{maven}}怎么样手动将下载下来的 JAR 包添加到 Maven、ivy 的本地仓库
  5. Activiti安装
  6. Spring再学习
  7. 用纯jsp实现用户的登录、注册与退出
  8. JS拖动浮动DIV
  9. MVC5 Entity Framework学习之创建复杂的数据模型
  10. Python使用心得之魔法参数**kw
  11. 【转】java jvm 线程 与操作系统线程
  12. 什么是TNB?如何买TNB?
  13. android sdk里的各目录作用
  14. Linux-基础学习(四)-部署图书管理系统项目
  15. mysql Navicat 导入导出
  16. VersionControl:git
  17. Mishka and Divisors CodeForces - 703E
  18. oracle11g 导出空表
  19. Laya LoaderManager小记
  20. Linux 设置 LD_LIBRARY_PATH

热门文章

  1. Interface和Abstract class区别
  2. ORACLE VS MYSQL
  3. unity 引用 移动mm 支付sdk
  4. php和.net的DES加密解密方法
  5. Document字段发生变化后,报的错
  6. Android游戏快速入门(一):基础储备
  7. 【POJ】1505 Copying Books
  8. 应付分配集 Distribution Sets
  9. 国内的一些开源镜像站汇总,EPEL源
  10. Colored Sticks (字典树哈希+并查集+欧拉路)