poj 1179 Polygon(化环为直+区间dp)

poj 1179 Polygon(化环为直+区间dp)
总时间限制: 1000ms 内存限制: 65536kB

描述
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.

输入
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].

输出
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.

样例输入

4
t -7 t 4 x 2 x 5

样例输出

33
1 2

来源
IOI 1998

这个题目和我有一些的历史渊源吧,所以看到它第一眼十分亲切一定要拿下这个题目。这是计算概论期末考试最后一题,因为我当时来不及做这个题目了所以计算概论期末考试没有AK,当时已经有思路这个题是区间dp甚至方程式都已经写好了,只是因为没有时间了,这个缺憾也是我计算概论这门课独特的回忆吧。

本题主要是区间dp,一个关键的思路是因为乘法涉及符号问题,所以光dp最大值是不够的,也要dp最小值。另外一个小技巧是为了免去环路取模或旋转的麻烦,可以化环为直,构造一个两倍长的直线来实现环路功能。

因为思路清晰很快AC了,贴代码

Accepted    260kB   10ms    2120 B  G++
#define TEST
#undef TEST

#define MAX_N 50
#define INF 0x1FFFFFFF

#include<stdio.h>
#include<memory.h>

int n;
char op[MAX_N<<1];
int num[MAX_N<<1];

int max[MAX_N<<1][MAX_N<<1],min[MAX_N<<1][MAX_N<<1];
int score[MAX_N],ans;
bool first=true;

int Min(int,int);
int Max(int,int);

int main()
{
    #ifdef TEST
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    scanf("%d\n",&n);
    for (int i=0;i<n;i++)
        scanf("%c %d%*c",&op[i],&num[i]);
    for (int i=n;i<(n<<1);i++)
    {
        op[i]=op[i-n];
        num[i]=num[i-n];
    }
    for (int i=0;i<(n<<1);i++)
        for (int j=i+1;j<(n<<1);j++)
            {
                max[i][j]=-INF;
                min[i][j]=INF;
            }
    for (int i=0;i<(n<<1);i++)
        max[i][i]=min[i][i]=num[i];
    for (int i=0;i<n;i++)
        score[i]=Max(i,i+n-1);
    ans=-INF;
    for (int i=0;i<n;i++)
        if (score[i]>ans)
            ans=score[i];
    printf("%d\n",ans);
    for (int i=0;i<n;i++)
        if (score[i]==ans)
        {
            if (first)
                first=false;
            else
                printf(" ");
            printf("%d",i+1);
        }
    printf("\n");
    #ifdef TEST
    for (int l=0;l<(n<<1);l++)
        for (int r=l;r<(n<<1);r++)
            printf("Max(%d,%d)=%3d,Min(%d,%d)=%3d\n",l,r,Max(l,r),l,r,Min(l,r));
    #endif
    return 0;
}

int Max(int l,int r)
{
    #ifdef TEST
    printf("Max(%d,%d)\n",l,r);
    #endif
    int temp;
    if (max[l][r]!=-INF)
        return max[l][r];
    for (int p=l+1;p<=r;p++)
    {
        if (op[p]=='t')
            temp=Max(l,p-1)+Max(p,r);
        else
        {
            temp=Min(l,p-1)*Min(p,r);
            temp=Min(l,p-1)*Max(p,r)>temp?Min(l,p-1)*Max(p,r):temp;
            temp=Max(l,p-1)*Min(p,r)>temp?Max(l,p-1)*Min(p,r):temp;
            temp=Max(l,p-1)*Max(p,r)>temp?Max(l,p-1)*Max(p,r):temp;
        }
        if (temp>max[l][r])
            max[l][r]=temp;
    }
    return max[l][r];
}

int Min(int l,int r)
{
    #ifdef TEST
    printf("Min(%d,%d)\n",l,r);
    #endif
    int temp;
    if (min[l][r]!=INF)
        return min[l][r];
    for (int p=l+1;p<=r;p++)
    {
        if (op[p]=='t')
            temp=Min(l,p-1)+Min(p,r);
        else
        {
            temp=Min(l,p-1)*Min(p,r);
            temp=Min(l,p-1)*Max(p,r)<temp?Min(l,p-1)*Max(p,r):temp;
            temp=Max(l,p-1)*Min(p,r)<temp?Max(l,p-1)*Min(p,r):temp;
            temp=Max(l,p-1)*Max(p,r)<temp?Max(l,p-1)*Max(p,r):temp;
        }
        if (temp<min[l][r])
            min[l][r]=temp;
    }
    return min[l][r];
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值