http://poj.org/problem?id=1179

Polygon
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5078   Accepted: 2139

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

 
 
分析:

  多边形游戏,有N个顶点的多边形,3 <= N <= 50 ,多边形有N条边,每个顶点中有一个数字(可正可负),每条边上或者是“+”号,或者是“*”号。边从1到N编号,首先选择一条边移去,然后进行如下操作:

1 选择一条边E和边E连接着的两个顶点V1,V2。

2 用一个新的顶点代替边E和V1、V2,新顶点的值为V1、V2中的值进行边上代表的操作得来(相加或相乘)

当最后只剩一个顶点,没有边时,游戏结束。现在的任务是编程求出最后的顶点能获得的最大值,以及输出取该最大值时,第一步需移去的边,如果有多条符合条件的边,按编号从小到大输出。

其实结题思路还是比较好想到的,枚举(枚举去掉的符号)+DP(记忆化搜索)就可以做到。但这里有一个BUG,就是负负得正,所以不能单一的枚举最大值,而要同时DP最小值。    

计算最大值:
加法 max(i,j) = max(i,k)+max(k,j);
乘法 max(i,j) = MAX(max(i,k)*max(k,j),max(i,k)*min(k,j),max(k,j)*min(i,k),min(i,k)*min(k,j));(i=<k<=j) 计算最小值:
   加法  min(i,j) = min(i,k)+min(k,j);
乘法 min(i,j) = MIN(max(i,k)*max(k,j),min(i,k)*min(k,j),max(k,j)*min(i,k),min(i,k)*min(k,j));(i=<k<=j)
 
AC代码:
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
#include <set>
#include <queue>
#define MAX(a,b) (a) > (b)? (a):(b)
#define MIN(a,b) (a) < (b)? (a):(b)
#define mem(a) memset(a,0,sizeof(a))
#define INF 1000000007
#define MAXN 20005
using namespace std; bool op[];
int num[],dp_max[], dp_min[], n;
bool vis_max[],vis_min[];
int DP_MIN(int i,int j);
int DP_MAX(int i,int j); int DP_MAX(int i,int j)//DP求区间最大值
{
int u = i*+j;
if(vis_max[u])return dp_max[u];
vis_max[u]=;
if(j-i <= )
{
if(j==i)return dp_max[u]=num[i-];
if(!op[i])return dp_max[u]=num[i-]+num[i];
else return dp_max[u]=num[i-]*num[i];
}
dp_max[u] = -INF;
for(int k=i;k<j;k++)
{
int l=DP_MIN(i,k);
int r=DP_MIN(k+,j);
int ll=DP_MAX(i,k);
int rr=DP_MAX(k+,j);
if(!op[k])dp_max[u] = MAX(dp_max[u], ll+rr);
else dp_max[u] = MAX(dp_max[u], MAX(ll*rr,MAX(l*r,MAX(l*rr,r*ll))));
}
return dp_max[u];
} int DP_MIN(int i,int j)//DP求区间最小值
{
int u = i*+j;
if(vis_min[u])return dp_min[u];
vis_min[u]=;
if(j-i <= )
{
if(j==i)return dp_min[u]=num[i-];
if(!op[i])return dp_min[u]=num[i-]+num[i];
else return dp_min[u]=num[i-]*num[i];
}
dp_min[u] = INF;
for(int k=i;k<j;k++)
{
int l=DP_MIN(i,k);
int r=DP_MIN(k+,j);
int ll=DP_MAX(i,k);
int rr=DP_MAX(k+,j);
if(!op[k])dp_min[u] = MIN(dp_min[u], l+r);
else dp_min[u] = MIN(dp_min[u], MIN(ll*rr,MIN(l*r,MIN(l*rr,r*ll))));
}
return dp_min[u];
} int main()
{
while(~scanf("%d%*c",&n))
{
mem(op);mem(dp_max);
mem(num);mem(vis_min);
mem(vis_max);
int max=-INF,i;
char ch;
for(i=;i<n;i++)
{
scanf("%c %d%*c",&ch,&num[i]);
op[i]=op[i+n]=(ch=='x');
num[i+n]=num[i];
}
for(i=;i<n;i++)
{
max=MAX(max,DP_MAX(i+,i+n));
}
printf("%d\n",max);
int ok=;
for(i=;i<n;i++)
{
if(DP_MAX(i+,i+n) == max)
{
if(ok){printf("%d",i+);ok=;}
else printf(" %d",i+);
}
}
printf("\n");
}
return ;
}
 #include<iostream>
using namespace std;
int v[]; //存放点中的数据,下标从1开始
char op[]; //存放边,下标从1开始
int max_score[][]; //max_score[i][j]表示从边i到边j所能取到的最大值
int min_score[][]; //min_score[i][j]表示从边i到边j所能取到的最小值
int n; //边、点的数量 //计算最大最小值
void calculate(int max_x, int min_x, char op, int max_y, int min_y, int &max, int &min)
{
if(op=='t')
{
max = max_x + max_y;
min = min_x + min_y;
}
else //乘法
{
int temp;
max = min = max_x * max_y; temp = max_x * min_y;
if(temp > max) max=temp;
if(temp < min) min=temp; temp = min_x * max_y;
if(temp > max) max=temp;
if(temp < min) min=temp; temp = min_x * min_y;
if(temp > max) max=temp;
if(temp < min) min=temp;
}
} int getNum(int i) //处理可能的越界问题
{
if(i>n) i-=n;
else if(i<) i += n;
return i;
} //使用动态规划找最大值并返回
int dp()
{
int i,j,k,len, max_temp, min_temp, max_value, min_value, temp,ans;
//初始化数组的边界值
for(i=; i<=n; i++)
{
j=i-; //j代表i左边的边
if(j==) j=n; if(op[i]=='t')
max_score[i][i] = min_score[i][i] = v[j] + v[i];
else
max_score[i][i] = min_score[i][i] = v[j] * v[i]; max_score[i][j] = min_score[i][j] = v[j];
} ans=<<;
for(len=; len<n; len++) //len代表从边i到边j包含多少条边
for(i=; i<=n; i++) //i代表开始边
{
j=i+len-; //j代表结束边
if(j>n) j-=n;
//计算max[i][j] min[i][j]的值, max[i][j]=max{d[i][k-1] op[k] d[k+1][j]} i<=k<=j
k=i;
calculate(max_score[i][getNum(k-)], min_score[i][getNum(k-)], op[k], max_score[getNum(k+)][j], min_score[getNum(k+)][j], max_temp, min_temp);
max_value=max_temp;
min_value=min_temp;
while(true)
{
k++;
if(k>n) k-=n;
calculate(max_score[i][getNum(k-)], min_score[i][getNum(k-)], op[k], max_score[getNum(k+)][j], min_score[getNum(k+)][j], max_temp, min_temp);
if(max_temp > max_value) max_value=max_temp;
if(min_temp < min_value) min_value=min_temp;
if(k==j) break;
}
max_score[i][j]=max_value;
min_score[i][j]=min_value;
if(len==n- && max_value>ans) ans=max_value;
} return ans;
} int main()
{
int i,j,ans;
bool first=true;; //读入数据
cin>>n;
for(i=; i<=n; i++)
{
cin>>op[i]>>v[i];
} ans=dp();
cout<<ans<<endl; //输出最大值
//输出取最大值时,第一次移去的边
for(i=; i<=n; i++)
{
j=i+n-;
if(j>n) j -= n; if(max_score[i][j] == ans)
{
if(!first) cout<<" ";
cout<<i-;
first = false;
}
}
if(max_score[][n-]==ans)
{
if(!first) cout<<" ";
cout<<n;
}
cout<<endl; return ;
}

poj 1179 Polygon的更多相关文章

  1. POJ 1179 - Polygon - [区间DP]

    题目链接:http://poj.org/problem?id=1179 Time Limit: 1000MS Memory Limit: 10000K Description Polygon is a ...

  2. poj 1179 $Polygon$(断环成链)

    Polygon \(solution:\) upd:还是多讲一下,这道题基本上可以说是一道思维题.一道结论题.一道考验你动态规划基本功是否扎实的题目.因为这道题的数据范围很小,思考一下总能想到断环成链 ...

  3. IOI 98 (POJ 1179)Polygon(区间DP)

    很容易想到枚举第一步切掉的边,然后再计算能够产生的最大值. 联想到区间DP,令dp[i][l][r]为第一步切掉第i条边后从第i个顶点起区间[l,r]能够生成的最大值是多少. 但是状态不好转移,因为操 ...

  4. DP中环形处理 +(POJ 1179 题解)

    DP中环形处理 对于DP中存在环的情况,大致有两种处理的方法: 对于很多的区间DP来说,很常见的方法就是把原来的环从任意两点断开(注意并不是直接删掉这条边),在复制一条一模一样的链在这条链的后方,当做 ...

  5. Mark一下, dp状态转移方程写对,可是写代码都错,poj 1651 poj 1179

    dp题: 1.写状态转移方程; 2.考虑初始化边界,有意义的赋定值.还没计算的赋边界值: 3.怎么写代码自底向上计算最优值 今天做了几个基础dp,所有是dp方程写对可是初始化以及计算写错 先是poj ...

  6. POJ 1179 IOI1998 Polygon

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

  7. 【POJ 1179】Polygon

    [原题链接]传送门 [题解思路] 1.第一感觉没有其他做法,想到动态规划,去环,区间dp 2.f[l,r]表示[l,r]内的最大值,考虑转移 3.最大值分加法和乘法,其中乘法不一定由两个要求合并的区间 ...

  8. POJ 3597 Polygon Division 多边形剖分

    题目链接: http://poj.org/problem?id=3597 Polygon Division Time Limit: 2000MSMemory Limit: 131072K 问题描述 G ...

  9. POJ 2007--Scrambled Polygon(计算凸包,点集顺序)

    Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10094   Accepted: 476 ...

随机推荐

  1. 【转】 Update和FixedUpdate的区别

    MonoBehaviour.Update 更新 当MonoBehaviour启用时,其Update在每一帧被调用. MonoBehaviour.FixedUpdate 固定更新 当MonoBehavi ...

  2. vbox共享文件 挂载

    环境:主机操作系统是Windows 7,虚拟机是open suse 12.0,虚拟机是VirtualBox 4.2.1. 1. 安装增强功能包(Guest Additions) 安装好open sus ...

  3. Best practice: escape, or encodeURI / encodeURIComponent

    escape() Don't use it, as it has been deprecated since ECMAScript v3. encodeURI() Use encodeURI when ...

  4. Java进程间通信

    传统的进程间通信的方式有大致如下几种: (1)   管道(PIPE) (2)   命名管道(FIFO) (3)   信号量(Semphore) (4)   消息队列(MessageQueue) (5) ...

  5. MongoDB创建用户

    1.在创建用户之前,我们首先应该启动mongodb的用户验证功能,否则建立用户是没有意义的! 2.使用 //这个123用户拥有test1数据库的数据库管理员权限,拥有test2数据库的读取权限 db. ...

  6. SQLAlchemy 一对多

    下述範例描述了電影同導演的多對一關係.範例中說明了從用戶定義的Python類建立數據表的方法,雙方關係例項的建立方法,以及最終查詢數據的方法:包括延遲載入和預先載入兩種自動生成的SQL查詢. 結構定義 ...

  7. block iOS 块

    block 是个很陌生的东西啊.以前没有学会,现在再看它,还是觉得很稀奇古怪. 无奈,之后硬着头皮学了.. //有参返回值 格式: 返回值类型 (^变量名)(参数类型及个数) = ^(形参列表){ 代 ...

  8. Fiddler-006-修改HTTP请求参数

    在进行 App 测试时,经常需要修改请求参数,以获得不同的显示效果,以查看相应的页面显示处理.例如:可以通过修改 HTTP请求 的参数,来获取不同的响应结果. 下面以修改 HTTP请求的商品系统编号为 ...

  9. 开发报表时将已有User做成下拉列表,第一项为label为ALL,value为null

    SELECT 'All' AS LABLE_NAME, NULL AS USER_NAMEUNION ALLSELECT USER_NAME AS LABLE_NAME, USER_NAME from ...

  10. meta的学习记录

    meta的学习记录 最近再学响应式的布局,就先来总结一下我们的viewport. 什么是viewport呢? 手机浏览器是把页面放在一个虚拟的“窗口”(viewport)中,通常这个虚拟的“窗口”(v ...