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

# include <stdio.h>
# include <string.h>
int n, m, k, map[101][101];
int dp[10001];//记录以i结尾的最长上升子序列长度
int pre[10001];//记录前驱节点
int cnt[10001];//保存以i结尾的最长上升子序列总数
int g[10001];//记录每个坐标的id
void init()
{
    int a, b;
    memset(map, 0, sizeof(map));
    while(scanf("%d%d",&a,&b),a+b)
        map[a][b] = 1;
    k = 0;
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
            if(map[i][j])
                g[k++] = (i-1)*m + j-1;//减1处理方便下面判断j点和i点的方位。
    if(!map[n][m])//将终点放进去,方便统计最长上升子序列和路径总数。
        g[k++] = n*m-1;
} void solve()
{
    for(int i=0; i<k; ++i)
    {
        dp[i]=1, cnt[i]=1, pre[i]=-1;
        for(int j=0; j<i; ++j)
            if((g[j]%m) <= (g[i]%m))
            {
                if(dp[j]+1 == dp[i])
                    cnt[i] += cnt[j];
                else if(dp[j]+1 > dp[i])
                    dp[i] = dp[j]+1, cnt[i] = cnt[j], pre[i] = j;
            }
    }
    if(!map[n][m])
        --dp[k-1];
} void put(int num)
{
    if(pre[num] != -1)
        put(pre[num]);
    if(num != k-1 || map[n][m])
        printf(" %d",g[num]+1);
} int main()
{
    int cas = 1;
    while(scanf("%d%d",&n,&m)==2)
    {
        if(n==-1 && m==-1)
            break;
        init();
        solve();
        printf("CASE#%d: %d %d",cas++, dp[k-1], cnt[k-1]);
        put(k-1);
        printf("\n");     }
    return 0;
}

转载于: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. MySQl 和 Redis

    MySQL MySQL 是关系型数据库,开放源码软件,主要使用持久化存储设备(像磁盘)数据存放在磁盘中,功能强大. 因为磁盘访问速度远远慢于内存,所以访问速度慢 Redis 是非关系型,高性能的key ...

  2. php--一些有用的Laravel辅助函数

    str_start()/str_finish() 将指定值添加到字符串的开头/结尾(当不是以该值开头/结尾时) blank() 判断给定的值是否为「空」 collect() 根据给定的数组创建一个集合 ...

  3. vscode如何安装eslint插件 代码自动修复

    ESlint:是用来统一JavaScript代码风格的工具,不包含css.html等. 方法和步骤: 通常情况下vue项目都会添加eslint组件,我们可以查看webpack的配置文件package. ...

  4. "小号文本"组件:<small> —— 快应用组件库H-UI

     <import name="small" src="../Common/ui/h-ui/text/c_tag_small"></impor ...

  5. 解决idea导入maven项目缺少jar包的问题

    之前一直用的elipse,现在用idea不熟悉,这里记录一下.这里以idea2017为例. 导入elipse的maven项目,提示缺少jar包,肯定是idea没有给你导包. 第一步,首先确认自己的id ...

  6. 最长公共子窜和最长公共子序列(LCS)

    他们都是用dp做;复杂度都是O(N方) 有一个大佬的博客写的很详细,是关于最长公共子序列的:https://blog.csdn.net/hrn1216/article/details/51534607 ...

  7. AJ整理问题之:copy,对象自定义copy 什么是property

    AJ分享,必须精品 copy copy的正目的 copy 目的:建立一个副本,彼此修改,各不干扰 Copy(不可变)和MutableCopy(可变)针对Foundation框架的数据类型. 对于自定义 ...

  8. ASE课程总结 by 朱玉影

    收获: 最大的收获应该就是对待选题要慎重吧,虽然前期做了一下调研,但是还是不够,所以到最后我们的项目才会不能公开发布,项目中间也是波折不断,导致我们走了很多弯路,浪费了很多时间吧.选题一定要慎重,慎重 ...

  9. Daily Scrum 1/4/2015

    Process: After New year's Day, we start our project in plan. Zhanyang: Add some useful UI in the IOS ...

  10. python圆周率计算小程序(非常慢)

    源码: from math import fabs #导入数学模块 from time import perf_counter #导入时间模块 from numba import jit @jit d ...