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. Siddhi CEP Window机制

    https://docs.wso2.com/display/CEP400/SiddhiQL+Guide+3.0#SiddhiQLGuide3.0-Window https://docs.wso2.co ...

  2. 浅析C++的内存管理

    在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/ 静态存储区和常量存储区. 栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区.里面的变量通常是局部变量.函数参 ...

  3. css伪元素before/after和画三角形的搭配应用

    想要实现的效果如下: 第一步:如何用css画出三角形? /* css画三角形 */ .sanjiao{ ; border-top:40px solid red; border-bottom:40px ...

  4. NSURLSession

    参考文章1, apple文档 一.NSURLSessionConfiguration 介绍:分别配置每一个 session 对象.(NSURLConnection 很难做到) 分类: 1) defau ...

  5. 通过runtime替换系统类实现的代码(从github开源库fdstackview中摘录)

    其中部分代码为汇编:由此可见oc的runtime的灵活性和能力.此代码仅供参考 // ---------------------------------------------------- // R ...

  6. angularJS自定义指令模板替换

    <html> <head> <meta charset="utf-8"/> <title></title> </h ...

  7. 解析const

    const在函数前与函数后的区别 一   const基础         如果const关键字不涉及到指针,我们很好理解,下面是涉及到指针的情况:         int   b   =   500; ...

  8. jquery 分页

    最近有点无所事事,无聊之极! 啊啊啊,表示很痛苦! <div id="tablist_01" class="list_tab"> <table ...

  9. 【指标测试】影响IOPS的几个重要因素

    1. 读写方式 顺序读写的IOPS要比随机读写的IOPS高.100%顺序读写来讲,顺序读要高于顺序写.100%随机读写来讲,随机读要高于随机写.小块读写的IOPS要比大块读写高.需要根据实际的应用程序 ...

  10. SQL Server 2008 R2【SET ANSI_PADDING填充属性】插入一条数据后,为何每一列都默认的在字符后多了几个空格

    当加入空格后查出 解决: 导致出现这样的现象的原因就是SET ANSI_PADDING选项. 这个选项只在数据表的字符串字段被更新或者新的数据行插入到表中的时候作用.它控制着SQL Server在遇到 ...