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
- HDU 6415(dp/記憶化搜尋)
- 記憶化搜尋
- C - Digital Path 計蒜客 - 42397(dp記憶化搜尋)Git
- C++記憶化搜尋C++
- 二叉搜尋樹 [四邊形不等式優化區間dp]優化
- 【記憶優化搜尋/dp】HDU - 6415 - 杭電多校第九場 - Rikka with Nash Equilibrium優化UI
- 【leetcode 1510 石子游戲】【記憶化搜尋】LeetCode
- 記憶搜尋解救滑雪問題
- 區間dp
- 【leetcode 3149. 找出分數最低的排列】記憶化搜尋LeetCode
- 【DP】區間DP入門
- WeetCode3 暴力遞迴->記憶化搜尋->動態規劃遞迴動態規劃
- ACM日常訓練日記——8.1(區間dp)ACM
- Eat Walnuts(區間dp)
- 組合數的計算(利用楊輝三角/記憶化搜尋)
- Mac 下打包APK的血淚坑(巨坑,巨巨坑,史前巨坑)MacAPK
- ES 筆記十七:結構化搜尋筆記
- 【區間dp】石子合併
- AB實驗坑賊多?騰訊搜尋實驗有妙招
- python 寫的搜尋引擎Python
- [動態規劃] 區間 dp動態規劃
- POJ1390 Blocks (區間DP)BloC
- MacOS的Safari搜尋欄無法搜尋怎麼解決?Mac
- 演算法學習筆記-暴力搜尋和分治法演算法筆記
- 【Leetcode】1340. Jump Game V 【動態規劃/記憶性搜尋】LeetCodeGAM動態規劃
- Codeforces Round #689 (Div. 2, based on Zed Code Competition)-B. Find the Spruce(DFS+記憶化搜尋)Zed
- 區間dp 合併石子問題
- AtCoder Regular Contest 104——C區間dp
- 匹配搜尋關鍵高亮 new RegEXP 填坑
- 搜尋引擎優化(SEO)優化
- python 寫的搜尋引擎 - 原始碼Python原始碼
- cnetos 9 安裝巨坑!!! ssh無法登入
- 如何解決使用mac聚焦搜尋無法搜尋軟體的情況Mac
- Java記憶體區域有哪些構成?Java記憶體
- 折半搜尋學習筆記筆記
- dp套dp 隨寫
- 各大網際網路巨頭的搜尋引擎交鋒PGB
- solr搜尋分詞優化Solr分詞優化