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. 写给程序员的机器学习入门 (二) - pytorch 与矩阵计算入门

    pytorch 简介 pytorch 是目前世界上最流行的两个机器学习框架的其中之一,与 tensoflow 并峙双雄.它提供了很多方便的功能,例如根据损失自动微分计算应该怎样调整参数,提供了一系列的 ...

  2. python:列表生成式的学习

    看个例子: # 定义一个列表 l=[1,2,3,4,5] #()用于创建一个list,结果依次返回列表l的元素的平方,返回list s=[i*i for i in l] # 打印列表s print(s ...

  3. 1040 Longest Symmetric String (25分)(dp)

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

  4. Thinkphp getLastSql函数用法

    如何判断一个更新操作是否成功: $Model = D('Blog'); $data['id'] = 10; $data['name'] = 'update name'; $result = $Mode ...

  5. flask-include、set、with、模板继承

    flask-include.set.with include: 跟django的include类似,将一个html的代码块直接嵌入另一个html文件中 {%   include    'html    ...

  6. 安卓开发学习日记 DAY4——Button,ImageButton

    Button与ImageButton基本类似 也有类似于TextView和ImageView的区别 这里需要注意的是: 在你定义text属性的内容时,最好是在Values文件下的String.xml中 ...

  7. Java类锁和对象锁实践和内部私有锁关联

    Java类锁和对象锁实践 感谢[jiehao]同学的投稿,投稿可将文章发送到tengfei@ifeve.com 类锁和对象锁是否会冲突?对象锁和私有锁是否会冲突?通过实例来进行说明. 一.相关约定 为 ...

  8. Fiddler实战之拟2G、3G、4G网络进行弱网测试

    至于fidder网络代理设置就不多说了 模拟网速: 1.启动Fiddler,打开菜单栏Rules---Performances---Simulate Modem Speeds这里打开了模拟调节速度 2 ...

  9. 负载均衡服务之HAProxy基础入门

    首先我们来了解下haproxy是干嘛的?haproxy是一个法国人名叫Willy Tarreau开发的一个开源软件:这款软件主要用于解决客户端10000以上的同时连接的高性能的TCP和HTTP负载均衡 ...

  10. L3 多层感知机

    **本小节用到的数据下载 1.涉及语句 import d2lzh1981 as d2l 数据1 : d2lzh1981 链接:https://pan.baidu.com/s/1LyaZ84Q4M75G ...