• 「IOI1998」「LuoguP4342」Polygon(区间dp


    P4342 [IOI1998]Polygon - 洛谷

    题意翻译

    题目可能有些许修改,但大意一致

    多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4。每个顶点用整数标记,每个边用符号+(加)或符号*(乘积)标记。

    第一步,删除其中一条边。随后每一步:

    选择一条边连接的两个顶点V1和V2,用边上的运算符计算V1和V2得到的结果来替换这两个顶点。

    游戏结束时,只有一个顶点,没有多余的边。

    如图所示,玩家先移除编号为3的边。之后,玩家选择计算编号为1的边,然后计算编号为4的边,最后,计算编号为2的边。结果是0。

    (翻译者友情提示:这里每条边的运算符旁边的数字为边的编号,不拿来计算)

    编写一个程序,给定一个多边形,计算最高可能的分数。

    输入格式

    输入描述一个有n个顶点的多边形,它包含两行。第一行是数字n,为总边数。

    第二行描述这个多边形,一共有2n个读入,每两个读入中第一个是字符,第二个是数字。

    第一个字符为第一条边的计算符号(t代表相加,x代表相乘),第二个代表顶点上的数字。首尾相连。

    3 < = n < = 50

    对于任何一系列的操作,顶点数字都在[-32768,32767]的范围内。

    输出格式

    第一行,输出最高的分数。在第二行,它必须写出所有可能的被清除后的边仍能得到最高得分的列表,必须严格递增。

    感谢@2016c01 提供的翻译

    题目描述

    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.

    输入输出样例

    输入样例#1: 复制
    4
    t -7 t 4 x 2 x 5
    
    输出样例#1: 复制
    33
    1 2

     题解

    首先是对于环形问题的惯用套路——断环为链,再复制一遍。

    对于链上的每个点,我们记录该点的初始值$num[i]$ 和该点左边的边的符号$fh[i]$

    然后在状态设置上,不仅要设区间$[L,R]$合并得到的最大值$f[L][R]$,还要设一个区间最小值$g[L][R]$。

    因为乘法的存在和负负得正的原理,两个为负的值会对结果产生正的贡献。

    然后就是很常规的区间dp了。

     1 /*
     2     qwerta
     3     P4342 [IOI1998]Polygon
     4     Accepted
     5     100
     6     代码 C++,1.46KB
     7     提交时间 2018-09-24 15:19:58
     8     耗时/内存
     9     18ms, 1044KB
    10 */
    11 #include<iostream>
    12 #include<cstring>
    13 #include<cstdio>
    14 #include<cmath>
    15 using namespace std;
    16 int num[107];
    17 bool fh[107];
    18 int f[107][107];
    19 int g[107][107];
    20 int stack[107];//用来输出答案
    21 int main()
    22 {
    23     //freopen("a.in","r",stdin);
    24     ios::sync_with_stdio(false);
    25     cin.tie(false),cout.tie(false);//关闭同步流
    26     int n;
    27     cin>>n;
    28     for(int i=1;i<=n;++i)
    29     {
    30         char ch;
    31         cin>>ch;
    32         fh[i+n]=fh[i]=(ch=='t'?0:1);//记录符号 存的时候要存两截
    33         int x;
    34         cin>>x;
    35         num[i+n]=num[i]=x;
    36     }
    37     int n2=n*2;
    38     memset(g,127,sizeof(g));//给g赋INF
    39     memset(f,128,sizeof(f));//给f赋-INF
    40     for(int i=1;i<=n2;++i)
    41     {
    42         f[i][i]=g[i][i]=num[i];//赋初值
    43     }
    44     for(int len=1;len<n2;++len)
    45     for(int l=1,r=len;r<=n2;++l,++r)
    46     {
    47         for(int k=l+1;k<=r;++k)
    48         if(!fh[k])//如果这里是一个加号
    49         {
    50             f[l][r]=max(f[l][r],f[l][k-1]+f[k][r]);
    51             g[l][r]=min(g[l][r],g[l][k-1]+g[k][r]);
    52         }
    53         else//如果是个乘号
    54         {
    55             f[l][r]=max(f[l][r],max(f[l][k-1]*f[k][r],g[l][k-1]*g[k][r]));
    56             g[l][r]=min(min(g[l][r],min(f[l][k-1]*f[k][r],f[l][k-1]*g[k][r])),
    57                             min(g[l][k-1]*g[k][r],g[l][k-1]*f[k][r]));
    58         }
    59     }
    60     int ans=-999999999,tos=0;
    61     for(int i=1;i<=n;++i)
    62     {
    63         if(ans<f[i][i+n-1])
    64         {
    65             ans=f[i][i+n-1];
    66             tos=1;
    67             stack[tos]=i;
    68         }
    69         else if(ans==f[i][i+n-1])
    70         {
    71             stack[++tos]=i;
    72         }
    73     }//找答案
    74     cout<<ans<<endl;
    75     for(int i=1;i<=tos;++i)
    76     cout<<stack[i]<<" ";
    77     return 0;
    78 }
  • 相关阅读:
    智能安全实验室-全能优化(Guardio) 3.8.0.460:紧急修复功能列表点击“导出(/列表)”出现错误的问题
    Guardio全能优化3.2.0.400
    智能安全实验室-全能优化(Guardio) 3.8.0.482:修正部分错误
    智能安全实验室-Defendio杀马2.4.0.420-实时防护-内存防护、新浏览器导航界面
    智能安全实验室-杀马(Defendio) 2.3.0.409 :任务计划,用户可以定时对指定目标进行扫描、智能更新等
    微软同步框架入门之五使用WCF同步远程数据
    微软同步框架入门之四冲突(Conflict)检测和处理
    小A是支枪,子弹未打光之"步枪"篇
    微软同步框架入门之三分析生成的同步类文件
    微软同步框架入门之二增量和修改同步方式
  • 原文地址:https://www.cnblogs.com/qwerta/p/9699938.html
Copyright © 2020-2023  润新知