zoukankan      html  css  js  c++  java
  • POJ-1179 Polygon (动态规划)

    Polygon
    Time Limit: 1000MS Memory Limit: 10000K
    Total Submissions: 5293 Accepted: 2238
    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
    题意应该不难读懂吧,这里我就不赘述了。我只想谈谈这个题目给我带来的收获,和这个题目的状态转移方程。首先这个是一道什么样的DP题目呢?在写这道题目之前,应该知道一个概念就是最优矩阵链乘,就是连续几个矩阵相乘,由于矩阵相乘满足结合率,所以可以选取不同的想乘顺序,但是不同的顺序会带来不同的计算次数,因为矩阵相乘的特性,行数乘以列数什么乱七八糟的,可以百度一下。这个我都忘了,以前线代课的时候记得很清楚。然后这道题目差不多同一个道理,就是怎么样的顺序安排,使得最后得到的值最大。利用动态规划的思想,如果在去掉一条边的情况下,那么最终的情况肯定是这条边的一个点到通过其他点到另一个点,这个记做DP[d%n][(d+n)%n],无论你先选择操作哪条边,最终的结果肯定是从d到d+n,这个区间,计算的结果。由于你可以选择顺序,所以d到d+n,你就可以分开进行,比如先算k到d+n,再算d到k,那么k就是分割点
    dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]);
    我知道我说了一大堆,可能你什么都不懂,因为动态规划是很难用语言描述准确的,只知道它的意思。

    这里还有一点就是,dp维护的不仅是最大值,还要最小值,因为负负得正啊,所以最小值也要考虑!!!

    #include <iostream>
    #include <string.h>
    #include <math.h>
    #include <algorithm>
    #include <stdlib.h>
    
    using namespace std;
    #define MAX 999999
    int dmax[55][55];
    int dmin[55][55];
    int n;
    char c[55];
    int  a[55];
    int res[55];
    int main()
    {
        int tot;
        int ans;
        int ans1,ans2;
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                   dmax[i][j]=-MAX;
                   dmin[i][j]=MAX;
                }
            }
             tot=0;
             ans=-MAX;
             getchar();
             for(int i=0;i<n;i++)
             { 
                cin>>c[i]>>a[i];
                dmax[i][i]=dmin[i][i]=a[i];
    
             }
    
    
    
             for(int d=0;d<n;d++)
             {
                 for(int i=1;i<n;i++)
                 {
    
                     for(int  j=d;j+i<d+n;j++)
                     {
    
                        // if(dmax[j%n][(j+i)%n]!=-MAX)
                            // continue;
    
                          for(int k=j;k<j+i;k++)
                          {
                              if(c[(k+1)%n]=='t')
                              {
                                 dmax[j%n][(j+i)%n]=max(dmax[j%n][(j+i)%n],dmax[j%n][k%n]+dmax[(k+1)%n][(j+i)%n]);
                                 dmin[j%n][(j+i)%n]=min(dmin[j%n][(j+i)%n],dmin[j%n][k%n]+dmin[(k+1)%n][(j+i)%n]);
                              }
                              else
                              {
                                  dmax[j%n][(j+i)%n]=max(dmax[j%n][(j+i)%n],dmax[j%n][k%n]*dmax[(k+1)%n][(j+i)%n]);
                                  dmax[j%n][(j+i)%n]=max(dmax[j%n][(j+i)%n],dmin[j%n][k%n]*dmin[(k+1)%n][(j+i)%n]);
                                  dmin[j%n][(j+i)%n]=min(dmin[j%n][(j+i)%n],dmax[j%n][k%n]*dmin[(k+1)%n][(j+i)%n]);
                                  dmin[j%n][(j+i)%n]=min(dmin[j%n][(j+i)%n],dmin[j%n][k%n]*dmax[(k+1)%n][(j+i)%n]);
                              }
                          }
    
    
                     }
    
    
    
                 }
                 if(dmax[d][(d+n-1)%n]>ans)
                 {
                       tot=0;
                       res[tot++]=d+1;
                       ans=dmax[d][(d+n-1)%n];
                 }
                 else if(dmax[d][(d+n-1)%n]==ans)
                 {
                     res[tot++]=d+1;
                 }
    
             }
             printf("%d
    ",ans);
             for(int i=0;i<tot;i++)
             {
                 if(i!=tot-1)
                 printf("%d ",res[i]);
                 else
                     printf("%d
    ",res[i]);
             }
    
        }
        return 0;
    
    }
  • 相关阅读:
    Vim配置IDE开发环境
    Win7任务计划自由预设系统定时自动关机
    awk中文手册
    awk简明教程
    Linux之mount命令详解
    VirtualBox内Linux系统与Windows共享文件夹
    堆排序详解
    int main(int argc,char* argv[])参数详解
    GDB调试详解
    VirtualBox中虚拟Ubuntu添加新的虚拟硬盘
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228851.html
Copyright © 2011-2022 走看看