算法题:UVA 10599 Robots(II)(dp lis)

Your company provides robots that can be used to pick up litter from fields after sporting events and concerts. Before robots are assigned to a job, an aerial photograph of the field is marked with a grid. Each location in the grid that contains garbage is marked. All robots begin in the Northwest corner and end their movement in the Southeast corner. A robot can only move in two directions, either to the East or South. Upon entering a cell that contains garbage, the robot can be programmed to pick it up before proceeding. Once a robot reaches its destination at the Southeast corner it cannot be repositioned or reused. Since your expenses are directly proportional to the number of robots used for a particular job, you are interested in making the most out of them. Your task would be to use a robot to clean the maximum number of cells containing garbage. Now there can be many ways to do this job, so your task would be to report that number of ways and show us one such sample.

You see your robot can traverse many cells without picking up garbage, so for us a valid solution would be the sequence of cell numbers that the robot cleans. The robots only clean cells that contain garbage; but you can program them to avoid picking up garbage from specific cells, if you would want to.

In the figure above we show a field map that has 6 rows and 7 columns. The cells in a field map are numbered in row major order starting from 1. For the example shown here, the following 7 cells contain garbage: 2 (1,2), 4 (1,4), 11 (2, 4), 13 (2, 6), 25 (4, 4), 28 (4, 7) and 41 (6, 7). Here cells are presented in cell_number (row, column) format. Now the maximum number of cells that can be cleaned is 5, and there are 4different ways to do that:

<2, 4, 11, 13, 28>

<2, 4, 11, 13, 41>

<2, 4, 11, 25, 28>

<2, 4, 11, 25, 41>

Input

An input file consists of one or more field maps followed by a line containing -1 -1 to signal the end of the input data. The description of a field map starts with the number of rows and the number of columns in the grid. Then in the subsequent lines, the garbage locations follows. The end of a field map is signaled by 0 0. Each garbage location consists of two integers, the row and column, separated by a single space. The rows and columns are numbered as shown in Figure 1. The garbage locations will not be given in any specific order. And a location would not be reported twice for a field map. Please note that for all the test cases you are required to solve, the field map would be of at most 100 rows and100 columns.

Output

The output for each test case starts with the serial number (starting from 1) for that test case. Then the following integers are listed on a line: N – the maximum number of cells that the robot can clean, C – the number of ways that these N cells can be cleaned, and N numbers describing one possible sequence of cell numbers that the robot will clean. As there can be C different such sequences and we are asking for only one sequence any valid sequence would do. Make sure that all these 2+N integers for a test case are printed on a single line. There must be one space separating two consecutive integers and a space between the colon and the first integer on the line. See the sample output format for a clear idea.

时间: 2024-09-09 02:55:51

算法题:UVA 10599 Robots(II)(dp lis)的相关文章

算法题:UVA 10534 Wavio Sequence(dp + LIS)

Wavio is a sequence of integers. It has some interesting properties. ·  Wavio is of odd length i.e. L = 2*n + 1. ·  The first (n+1) integers of Wavio sequence makes a strictly increasing sequence. ·  The last (n+1) integers of Wavio sequence makes a

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:uva 10318

题目链接: 首先,可以确定每个格子只能选一次,因为选任何大于0的偶数次,等于没有效果 一样. 然后,就可以把这题理解成从r*c的矩阵中选择一些格子进行"点亮"操作,使得最终所 有格子都是"亮"的状态.那么,每个格子要么有点亮操作,要么没有,总共复杂度为2^25,显然必须 进行减枝. 假设从第一行第一列开始,从左往右,从上往下一次依次选择,对于当前所在位置( x, y),它已经不能影响到x-2以前的行数了,所以当到x行时,如果第x-2行没有全部点亮,则进行减枝 . 此

算法题:uva 1330

题目链接: http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4076 以前做过一道一维的,这题只是变成了二维的,其他方法都一样.HDU 1506  Largest Rectangle in a Histogram   题解 代码1: #include<cstdio> #include<cs

uva 10599

题意 给一个n*m大小的网格,有一些格子上面会有一个垃圾.机器人从左上角(1,1)出发,每次只能选 择向右,或者向下走一步, 终点是(n, m).问最多可以捡多少个垃圾? 且捡最多垃圾有几种路径方案? 注意路径方案指和有垃圾的格子有关. 思路 一开始没注意到方案只和有垃圾的格子有关,当作水 题做,结果输出的方案数比样例大了非常多.然后又想了下. 根据题意,路径只和有垃圾的格子有关,那 么我们可以把这个网格抽象成一个图,图的节点就是有垃圾的格子,然后加上 一些边把这些格子连起来. 那么关键就是怎样

算法:uva 11456

题意 艾琳是个开火车的机师,她也负责车厢的调度.她喜欢把车厢依重量由大到小排列,把最重 的车厢摆在火车的前方. 不幸的是,排列车厢并不容易.你不能直接把一截车厢拿起来放在别处.把一截 车箱插入现有的列车中间并不切实际.一截车厢仅能接在列车的前面或后面. 车厢以事先排定的顺序抵达 车站.当一截车厢抵达时,艾琳可以把它接在列车的前方或后方,或根本不要这截车厢.列车越长越好,但 是其中的车厢要依重量排列. 依车厢抵达的顺序给你车厢的重量,艾琳所能接出的最长火车是多长? 思路 这题算是经典的类型题吧,看

算法题:poj 2541 Binary Witch(KMP水过,逆序转换)

链接: http://poj.org/problem?id=2541 分析与总结: 做这题估算了下复杂度,觉得无论KMP再怎么快,这题暴力也肯定要超时的. 想了很久也没想出个好办法,于是决定暴力之,但是TLE了....于是就放了几天.之后看了下discuss ,这题的正解应该是状态压缩dp,不过目前我还不懂,跪了. 之后百度发现也可以用KMP水过,虽然是因为数据水才过的,不过这种思路很巧妙,值得借鉴! 直接暴力是枚举字符串的后面13个的字母,然后再用KMP匹配,这样的话,就绪要枚举多次,分别是

时间复杂度-求教一个百度面试的算法题

问题描述 求教一个百度面试的算法题 一个有N个元素的一维数组(A[0],A[1], ..., A[n-1]),设计一个算法求解该数组最大子数组.(要求时间复杂度是O(n)) 解决方案 用动态规划http://www.cnblogs.com/xkfz007/archive/2012/05/17/2506299.html 解决方案二: http://www.ahathinking.com/archives/120.html 解决方案三: 哈,这道题啊,已经遇到好多次了,推荐一个很多人都在练习的网站,

从一道算法题说去2

今天的算法题是关于 字符串的最小编辑距离问题求解. 1. 什么是字符串编辑距离 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,添加一个字符,删除一个字符. 例如将kitten一字转成sitting: a. sitten (k→s)  b. sittin (e→i)  c. sitting (→g)  俄罗斯科学家Vladimir Levenshtein在1965年提出