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.

Figure 1: One 6 × 7 field map Figure 2: Four sample solutions
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 f different 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
and 100 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.
Sample Input
6 7
1 2
1 4
2 4
2 6
4 4
4 7
6 6
0 0
4 4
1 1
2 2
3 3
4 4
0 0
-1 -1
Sample Output
CASE#1: 5 4 2 4 11 13 28
CASE#2: 4 1 1 6 11 16

参考博客:http://blog.csdn.net/keshuai19940722/article/details/12163563

  1. # include <stdio.h>
  2. # include <string.h>
  3. int n, m, k, map[101][101];
  4. int dp[10001];//记录以i结尾的最长上升子序列长度
  5. int pre[10001];//记录前驱节点
  6. int cnt[10001];//保存以i结尾的最长上升子序列总数
  7. int g[10001];//记录每个坐标的id
  8. void init()
  9. {
  10.     int a, b;
  11.     memset(map, 0, sizeof(map));
  12.     while(scanf("%d%d",&a,&b),a+b)
  13.         map[a][b] = 1;
  14.     k = 0;
  15.     for(int i=1; i<=n; ++i)
  16.         for(int j=1; j<=m; ++j)
  17.             if(map[i][j])
  18.                 g[k++] = (i-1)*m + j-1;//减1处理方便下面判断j点和i点的方位。
  19.     if(!map[n][m])//将终点放进去,方便统计最长上升子序列和路径总数。
  20.         g[k++] = n*m-1;
  21. }
  22. void solve()
  23. {
  24.     for(int i=0; i<k; ++i)
  25.     {
  26.         dp[i]=1, cnt[i]=1, pre[i]=-1;
  27.         for(int j=0; j<i; ++j)
  28.             if((g[j]%m) <= (g[i]%m))
  29.             {
  30.                 if(dp[j]+1 == dp[i])
  31.                     cnt[i] += cnt[j];
  32.                 else if(dp[j]+1 > dp[i])
  33.                     dp[i] = dp[j]+1, cnt[i] = cnt[j], pre[i] = j;
  34.             }
  35.     }
  36.     if(!map[n][m])
  37.         --dp[k-1];
  38. }
  39. void put(int num)
  40. {
  41.     if(pre[num] != -1)
  42.         put(pre[num]);
  43.     if(num != k-1 || map[n][m])
  44.         printf(" %d",g[num]+1);
  45. }
  46. int main()
  47. {
  48.     int cas = 1;
  49.     while(scanf("%d%d",&n,&m)==2)
  50.     {
  51.         if(n==-1 && m==-1)
  52.             break;
  53.         init();
  54.         solve();
  55.         printf("CASE#%d: %d %d",cas++, dp[k-1], cnt[k-1]);
  56.         put(k-1);
  57.         printf("\n");
  58.     }
  59.     return 0;
  60. }

转载于:https://www.cnblogs.com/junior19/p/6730084.html

UVA10599:Robots(II)(最长上升子序列)的更多相关文章

  1. [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  2. P1439 【模板】最长公共子序列

    题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子 ...

  3. [HAOI2007]上升序列(最长上升子序列)

    题目描述 对于一个给定的 S=\{a_1,a_2,a_3,…,a_n\}S={a1​,a2​,a3​,…,an​} ,若有 P=\{a_{x_1},a_{x_2},a_{x_3},…,a_{x_m}\ ...

  4. HDOJ1025(最长上升子序列)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  5. DP——最长上升子序列(LIS)

    DP——最长上升子序列(LIS) 基本定义: 一个序列中最长的单调递增的子序列,字符子序列指的是字符串中不一定连续但先后顺序一致的n个字符,即可以去掉字符串中的部分字符,但不可改变其前后顺序. LIS ...

  6. 【洛谷P4309】最长上升子序列

    题目大意:给定一个序列,初始为空.现在我们将 1 到 N 的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? 题解:学会了 rope 操 ...

  7. [LeetCode每日一题]1143. 最长公共子序列

    [LeetCode每日一题]1143. 最长公共子序列 问题 给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度.如果不存在 公共子序列 ,返回 0 . 一个字符串 ...

  8. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  9. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

随机推荐

  1. Azure多因素认证

    什么是多重身份验证? 双重验证是需要多种验证方法的身份验证方法,可为用户登录和事务额外提供一层重要的安全保障. 它的工作原理是需要以下两种或多种验证方法: 用户知道的某样东西(通常为密码) 用户具有的 ...

  2. VLAN、Trunk,以太通道及DHCP

    VLAN.Trunk,以太通道及DHCP 案例1:Vlan的划分 案例2:配置trunk中继链路 案例3:以太通道配置 案例4:DHCP服务配置 1 案例1:Vlan的划分 1.1 问题 VLAN(虚 ...

  3. mpvue-新建页面、页面跳转、自适应单位

    1.mpvue怎么新建页面? (1)粘贴复制一个页面文件夹,只需要改文件夹名- 文件名不需要改,main.js里的东西不用动.export default里更改局部顶部栏配置. (2)index.vu ...

  4. 中阶d03.2 JDBC联合properties使用,通过读取本地配置文件为代码传递参数

    * 使用properties读取本地配置文件为代码传递参数 * url.用户名.密码.驱动地址等配置可以在配置文件中使用 main package zj_1_JDBC.properties; impo ...

  5. CH5501 环路运输(单调栈)

    传送门 思路: 遇到一个环,用正常人类的思想就先把环从中间截断然后将其补成2*n长度的链.环上的最小距离换到链上就是i以n/2为半径范围内的点(画图肉眼可见).由于两个点是等价的,所以我们考虑有序对( ...

  6. jvm入门及理解(四)——运行时数据区(堆+方法区)

    一.堆 定义: Heap,通过new关键字创建的对象,都存放在堆内存中. 特点 线程共享,堆中的对象都存在线程安全的问题 垃圾回收,垃圾回收机制重点区域. jvm内存的划分: JVM内存划分为堆内存和 ...

  7. Maybatis的一些总结(三:增删改查)

    回顾一个点 之前不懂这句: UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 现在理解了一点点,相当于实现了userMap ...

  8. 2020年iOS进阶面试题总结(一)

    准备找工作的你,可以看看,复习复习!! 1.说一下OC的反射机制 在动态运行下我们可以构建任何一个类,然后我们通过这个类知道这个类的所有的属性和方法,并且如果我们创建一个对象,我们也可以通过对象找到这 ...

  9. Linux U盘启动盘制作工具

    近期由于自己使用的ubuntu系统一直出问题,想做一下启动盘帮助恢复系统和故障检测,以前一直是用ultraiso来进行写盘的,但是发现制作了几次后,失败的机率很高,主要有以下几种情况: 1.引导有问题 ...

  10. matlab计算相对功率

    1.对脑电数据进行db4四层分解,因为脑电频率是在0-64HZ,分层后如图所示, 细节分量[d1 d2 d3 d4] 近似分量[a4] 重建细节分量和近似分量,然后计算对应频段得相对功率谱,重建出来得 ...