zoukankan      html  css  js  c++  java
  • [算法] DP中环形处理

    DP中环形处理

    对于DP中存在环的情况,大致有两种处理的方法:

    • 对于很多的区间DP来说,很常见的方法就是把原来的环从任意两点断开(注意并不是直接删掉这条边),在复制一条一模一样的链在这条链的后方,当做线性问题来解,即可实现时间复杂度降维。
    • 情况一:将原来的环从任意两点断开,再当做线性问题来解。情况二:添加一些特殊条件,将断开的前后强行连接起来。两种情况取其中的最优解即可。亦可以实现时间复杂度的降维。

    本篇博客将对于第一种情况进行分析。
    根据一道例题来探讨。

    POJ 1179 Polygon

    题目链接:

    (题目翻译将在文末给出)

    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
    

    Source

    IOI 1998

    分析

    题目大意
    给出一个多边形,删除一条边,合并剩下的点(若边的符号为x,则进行乘法;若边的符号为t,则进行加法)。问操作后剩下的点的值最大为多少。

    样例简单解释
    删除第1或第2条边,按照上述题意依此合并4,3,2或4,3,1,所以是(5×2×4+(-7))

    首先来考虑是一条链的情况
    对于一个区间 ([l,r]) 中,可以找到一个点 (k) ((k∈[l, r))) ,通过合并区间 ([l,k]) 与区间 ([k+1,r]) 来更新区间([l,r])的状态。

    具体是什么状态?定义一个区间最大值与一个区间最小值。即定义 (dp[i][j][0|1])来表示区间 ([l,j]) 的最值。其中,0为最大值,1为最小值。

    为何需要最小值?因为该数据中存在负数,负负得正,很有可能两个区间的最小值都为负数,他们的乘积远远大于这两个区间的最大值的乘积。

    需要讨论两种情况

    • (1) 当两点之间为加号时。对于每一个k,都只能用两区间的最大值之和来更新合并区间的最大值,最小值同理。不可能用最大值与最小值之和来更新区间最值。
    • (2) 当两点之间为加号时。对于每一个k,用四种不同的搭配,考虑到不同正负的情况,来更新区间的最值:
      (dp[l][k][1] * dp[k + 1][r][1])
      (dp[l][k][0] * dp[k + 1][r][0])
      (dp[l][k][1] * dp[k + 1][r][0])
      (dp[l][k][0] * dp[k + 1][r][1])

    若枚举删除每一条边,每次都来跑一遍DP,则时间复杂度为 (O(n^4))。但本题数据不强,若规定 (n≤100),则必定会超时。

    按照在复制一条一模一样的链在这条链的后方,当做线性问题来解这样来处理,则会将时间复杂度降到(O(n^3))

    拿样例来举例:

    原图:
    在这里插入图片描述
    化成一条链后:
    在这里插入图片描述

    在这条链上,(dp[1][4]) 就可以理解为删除边3后的最大值
    同理,(dp[2][5]) 就可以理解为删除边2后的最大值
    (dp[3][6]) 就可以理解为删除边1后的最大值
    (dp[4][7]) 就可以理解为删除边4后的最大值
    所以最大值就可以表示为 (Max(dp(i,i+n-1)))
    这样就省略了很多重复的计算。

    C++实现

    #include <cstdio>
    #define INF 0x3f3f3f3f
    #define Max(a, b) ((a) > (b) ? (a) : (b))
    #define Min(a, b) ((a) < (b) ? (a) : (b))
    void Quick_Read(int &N) {
    	N = 0;
    	char c = getchar();
    	int op = 1;
    	while(c < '0' || c > '9') {
    		if(c == '-')
    			op = -1;
    		c = getchar();
    	}
    	while(c >= '0' && c <= '9') {
    		N = (N << 1) + (N << 3) + c - 48;
    		c = getchar();
    	}
    	N *= op;
    }
    const int MAXN = 105;
    int dp[MAXN][MAXN][2];
    int A[MAXN];
    bool F[MAXN];
    int n;
    int ans = -INF; 
    void DP() {
    	for(int i = 1; i <= n; i++) {
    		A[i + n] = A[i];
    		F[i + n] = F[i];
    	}
    	n <<= 1;
    	for(int i = 1; i <= n; i++)
    		for(int j = 1; j <= n; j++) {
    			dp[i][j][0] = -INF;
    			dp[i][j][1] = INF;
    		}
    	for(int i = 1; i <= n; i++)
    		dp[i][i][1] = dp[i][i][0] = A[i];
    	for(int len = 2; len <= n; len++) {
    		for(int l = 1; l <= n - len + 1; l++) {
    			int r = l + len - 1;
    			for(int k = l; k < r; k++) {
    				if(F[k + 1]) {
    					for(int p = 0; p <= 1; p++)
    						for(int q = 0; q <= 1; q++) {
    							dp[l][r][0] = Max(dp[l][r][0], dp[l][k][p] * dp[k + 1][r][q]);
    							dp[l][r][1] = Min(dp[l][r][1], dp[l][k][p] * dp[k + 1][r][q]);
    						}
    				}
    				else {
    					dp[l][r][0] = Max(dp[l][r][0], dp[l][k][0] + dp[k + 1][r][0]);
    					dp[l][r][1] = Min(dp[l][r][1], dp[l][k][1] + dp[k + 1][r][1]);
    				}
    			}
    		}
    	}
    	for(int i = 1; i <= n; i++)
    		ans = Max(ans, dp[i][i + (n >> 1) - 1][0]);
    	printf("%d
    ", ans);
    	for(int i = 1; i <= (n >> 1); i++)
    		if(dp[i][i + (n >> 1) - 1][0] == ans)
    			printf("%d ", i);
    }
    void Read() {
    	Quick_Read(n);
    	char c = getchar();
    	for(int i = 1; i <= n; i++) {
    		while(c != 't' && c != 'x')
    			c = getchar();
    		if(c == 'x')
    			F[i] = true;
    		Quick_Read(A[i]);
    		c = -1;
    	}
    }
    int main() {
    	Read();
    	DP();
    	return 0;
    }
    

    题目翻译

    多边形是一个针对一个玩家的游戏,它从具有N个顶点的多边形开始,如图1所示,其中N = 4。每个顶点都标有整数,每个边都标有符号+(加法)或符号*(乘积)。边编号从1到N。
    在这里插入图片描述

    第一步,移除其中一个边缘。随后的移动涉及以下步骤:
    拾取边E和由E链接的两个顶点V1和V2;
    并用一个新的顶点替换它们,并在V1和V2的标签上标出执行E中指示的操作的结果。
    当没有更多边时,游戏结束,其分数是剩余单个顶点的标签。
    考虑图1的多边形。玩家首先移除边缘3。此后,玩家选择边缘1,然后选择边缘4,最后选择边缘2。得分为0。

    在这里插入图片描述

    编写一个程序,给定一个多边形,该程序将计算可能的最高得分并列出所有边缘,这些边缘如果在第一步移动时被移除,可能会导致具有该得分的游戏。

    您的程序将从标准输入中读取。输入描述了一个具有N个顶点的多边形。它包含两行。第一行是数字N。第二行包含边1,...,N的标签,这些标签与顶点的标签交错(首先是边1和2之间的顶点的标签,然后是边之间的顶点的标签) 2和3,依此类推,直到边缘N和1)之间的顶点,都被一个空格隔开。边缘标签是字母t(代表+)或字母x(代表*)。

    3 <= N <= 50
    对于任何移动顺序,顶点标签都在[-32768,32767]范围内。

    您的程序将写入标准输出。在第一行,您的程序必须编写输入多边形可获得的最高分数。在第二行中,它必须写出所有边缘的列表,如果在第一步中删除了这些边缘,则可以导致该分数的游戏。边必须以递增顺序书写,并以一个空格分隔。

  • 相关阅读:
    正则表达式
    字典
    列表 元组
    int 和 str bool 类型 基本数据类型
    循环体的主要内容
    变量与循环体
    第12课.内存控制器与SDRAM
    第11课.串口(UART)的使用
    第10课.掌握ARM芯片时钟体系
    第9课.gcc和arm-linux-gcc和Makefile
  • 原文地址:https://www.cnblogs.com/C202202chenkelin/p/14012855.html
Copyright © 2011-2022 走看看