poj1179 區間dp(記憶化搜尋寫法)有巨坑!
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.
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.
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].
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
/**
hdu 1179 區間dp(記憶化搜尋寫法)
題目大意:給定一個n個節點的環,環的每條邊代表+或者*,問最開始把哪條邊去掉,剩下的做運算可以得到最大的表示式的值
解題思路:列舉去掉n條邊的任意一條,然後區間dp來寫。值得一提的是兩個最小的負數相乘就會是最大的值,所以我們不能只維護最大值
同時也需要維護最小值。坑啊,這個陷阱太厲害了
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int num[105],sym[105],n;
int dpmin[155][155],dpmax[155][155];
int vismin[155][155],vismax[155][155];
int MAX(int i,int j);
int MIN(int i,int j);
int MAX(int x,int y)
{
if(vismax[x][y])return dpmax[x][y];
vismax[x][y]=1;
if(y==x)
{
dpmax[x][y]=num[x];
return dpmax[x][y];
}
dpmax[x][y]=-1000000007;
for(int i=x;i<y;i++)
{
int l=MAX(x,i);
int ll=MIN(x,i);
int r=MAX(i+1,y);
int rr=MIN(i+1,y);
if(sym[i+1])
{
dpmax[x][y]=max(dpmax[x][y],l+r);
}
else
{
int ans=l*r;
ans=max(ans,l*rr);
ans=max(ans,ll*r);
ans=max(ans,ll*rr);
dpmax[x][y]=max(dpmax[x][y],ans);
}
}
return dpmax[x][y];
}
int MIN(int x,int y)
{
if(vismin[x][y])return dpmin[x][y];
vismin[x][y]=1;
if(y==x)
{
dpmin[x][y]=num[x];
return dpmin[x][y];
}
dpmin[x][y]=1000000007;
for(int i=x;i<y;i++)
{
int l=MAX(x,i);
int ll=MIN(x,i);
int r=MAX(i+1,y);
int rr=MIN(i+1,y);
if(sym[i+1])
{
dpmin[x][y]=min(dpmin[x][y],ll+rr);
}
else
{
int ans=l*r;
ans=min(ans,l*rr);
ans=min(ans,ll*r);
ans=min(ans,ll*rr);
dpmin[x][y]=min(dpmin[x][y],ans);
}
}
return dpmin[x][y];
}
int main()
{
while(~scanf("%d%*c",&n))
{
for(int i=0;i<n;i++)
{
char ch;
scanf("%c %d%*c",&ch,&num[i]);
num[i+n]=num[i];
sym[i+n]=sym[i]=(ch=='t');
}
memset(dpmin,0,sizeof(dpmin));
memset(dpmax,0,sizeof(dpmax));
memset(vismin,0,sizeof(vismin));
memset(vismax,0,sizeof(vismax));
int maxx=-1000000007;
int sum[55],id;
for(int i=0;i<n;i++)
{
int ans=MAX(i,i+n-1);
/** for(int j=i;j<=i+n-1;j++)
{
printf("%d %c ",num[j],sym[j+1]==0?'*':'+');
}
printf("\n%d\n",ans);*/
if(ans>maxx)
{
maxx=ans;
id=0;
sum[id++]=i+1;
}
else if(ans==maxx)
{
sum[id++]=i+1;
}
}
printf("%d\n",maxx);
for(int i=0;i<id;i++)
{
printf(i==id-1?"%d\n":"%d ",sum[i]);
}
}
return 0;
}
相關文章
- 一類適合記憶化搜尋的區間dp
- C - Digital Path 計蒜客 - 42397(dp記憶化搜尋)Git
- Codeforces 148D Bag of mice:概率dp 記憶化搜尋
- 巨耗記憶體的寫法:記憶體
- 二叉搜尋樹 [四邊形不等式優化區間dp]優化
- 【記憶優化搜尋/dp】HDU - 6415 - 杭電多校第九場 - Rikka with Nash Equilibrium優化UI
- Codeforces 900D Unusual Sequences:記憶化搜尋
- 區間dp
- 【DP】區間DP入門
- POJ 1579-Function Run Fun(記憶化搜尋-遞迴)Function遞迴
- codeforces 505C. Mr. Kitayuta, the Treasure Hunter (記憶化搜尋)
- 組合數的計算(利用楊輝三角/記憶化搜尋)
- AB實驗坑賊多?騰訊搜尋實驗有妙招
- 搜尋引擎語法
- google搜尋語法Go
- ES 筆記十七:結構化搜尋筆記
- WeetCode3 暴力遞迴->記憶化搜尋->動態規劃遞迴動態規劃
- 搜尋巨擎俞某的求職簡歷求職
- poj3252 數位dp(所有比n小的二進位制位0的個數不少於1的個數)記憶化搜尋
- POJ 2311-Cutting Game(Nim博弈-sg函式/記憶化搜尋)GAM函式
- POJ2955 Brackets (區間DP)Racket
- MacOS的Safari搜尋欄無法搜尋怎麼解決?Mac
- python 寫的搜尋引擎Python
- java寫的 聚類搜尋Java聚類
- 演算法學習筆記-暴力搜尋和分治法演算法筆記
- NOIP2014pj子矩陣[搜尋|DP]矩陣
- 搜尋引擎優化(SEO)優化
- Mac 下打包APK的血淚坑(巨坑,巨巨坑,史前巨坑)MacAPK
- 轉:google搜尋聯盟與adsense搜尋廣告的區別Go
- 區間dp 合併石子問題
- 搜尋框帶有搜尋提示點選消失程式碼例項
- Codeforces Round #390 (Div. 2)(A,B,C(記憶化搜尋),D(貪心,優先佇列))佇列
- Android記憶體優化(一):Java記憶體區域Android記憶體優化Java
- Win10系統下點選此電腦搜尋框沒有反應無法搜尋怎麼辦Win10
- 如何解決使用mac聚焦搜尋無法搜尋軟體的情況Mac
- BZOJ 1048: [HAOI2007]分割矩陣 記憶化搜尋,二維字首和矩陣
- solr搜尋分詞優化Solr分詞優化
- 微信全文搜尋優化之路優化