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. python:列表切片知识的总结

    列表的切片操作时对其中的单个或者多个索引对应元素的操作,具有如下特点: ①.切片区间是左闭右开区间 ②.切片的下标可以表示负数,-1表示倒数第一个数,-2表示倒数第二个数 ③.默认步长是1,可增加第三 ...

  2. 1066 Root of AVL Tree (25分)(AVL树的实现)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  3. 使用Cloudflare增强网站

    Cloudflare Cloudflare是美国的一家网络性能和安全公司,近期由于自己域名HTTP证书到期,了解到了Cloudflare,用到了它提供的CDN以及SSL 如何设置CDN 登入Cloud ...

  4. 家庭记账本app进度之android中AlertDialog的相关应用以及对日期时间的相关操作(应用alertdialog使用的谈话框)

    对于AlertDialog的相关知识: 1.创建构造器AlertDialog.Builder的对象:    2.通过构造器对象调用setTitle.setMessage.setIcon等方法构造对话框 ...

  5. Linux 文件管理篇(三 属性管理)

    可读        r 可写        w 可执行        x 档案属性: 第一栏:执行list -al后第一栏的十个标志[1 - 10] 1: d    目录 -    档案 l    连 ...

  6. MODIS系列之NDVI(MOD13Q1)一:数据下载(二)基于FTP

    这一篇我们来介绍下MODIS数据的下载方式.当然这边主要是介绍国外网站的下载方式,国内网站的普遍是在地理空间数据云和遥感集市下载.国外网站(NASA官网)下载方式主要介绍两种.本篇主要针对第一种方式, ...

  7. 中阶d01-- web前端 html css js bootstrap

    html 页面骨架结构css 页面优化js(脚本语言) 页面和用户互动 bootstrap 前端框架,主要实现不同设备直接打开页面时播放比例设置(全屏暂时,不要滚动条)

  8. canvas 实现光线沿不规则路径运动

    canvas 实现光线沿不规则路径运动 此文章为原创,请勿转载 1.svg实现 2.canvas实现 3.坑点 svg让动画沿着不规则路径运动 查阅svg文档后发现,svg动画运动有两种实现方式,且都 ...

  9. EL表达式 -- 比较不错

    EL表达式 EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} 所有E ...

  10. Kafka 2.5.0发布——弃用对Scala2.11的支持

    近日Kafka发布了最新版本 2.5.0,增加了很多新功能: 下载地址:https://kafka.apache.org/downloads#2.5.0 对TLS 1.3的支持(默认为1.2) 引入用 ...