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. 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  2. 本地Vue项目跨域请求本地Node.js服务器的配置方法

    前言:跨域请求是在本地开发时经常遇到的需求,也很简单,只是几句代码配置一下的问题.我初次配置跨域请求时由于官方的说明太简洁,找到的教程又落伍,调试了一番并没有解决问题,到最后解决问题,已花费了很多时间 ...

  3. MySQl 和 Redis

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

  4. 从一个慢查询到MySQL字符集编码

    从一个慢查询到MySQL字符集编码 目录 从一个慢查询到MySQL字符集编码 1. 问题起源 2. MySQL字符集和字符集排序规则 2.1 字符集相关概念 2.2 MySQL中的字符集和字符集排序规 ...

  5. MTK Android 耳机线控的实现方法

    android 耳机线控的实现方法 keycodeonkeydownkeyevent 耳机线控的功能 耳机线控是一种很好用,并且能提升用户体验的功能.可以用来实现一些常用和基本的功能.比如:实现音乐播 ...

  6. C#两大知名Redis客户端连接哨兵集群的姿势

    前言 前面利用<Docker-Compose搭建Redis高可用哨兵集群>, 我们的思路是将Redis.Sentinel.Redis Client App链接到同一个网桥网络,这个网桥内的 ...

  7. 单线程IP扫描解析

    扫描代码: private void Button_Click(object sender, RoutedEventArgs e) { a5.Items.Clear(); string str = t ...

  8. istream_iterator && istream_iteratorbuf

    注意 读字符时, std::istream_iterator 默认跳过空白符(除非用 std::noskipws 或等价物禁用,而 std::istreambuf_iterator 不跳过.另外, s ...

  9. echarts多个数据添加多个纵坐标

    在我们echarts开发中,肯定会遇到一个问题.那就是当有多个数据且数据大小差距太大时,就会出现有些数据小到看不到的情况.所以在遇到这种情况时,我通常的解决办法就是给他多加一个坐标轴. option  ...

  10. CTR学习笔记&代码实现3-深度ctr模型 FNN->PNN->DeepFM

    这一节我们总结FM三兄弟FNN/PNN/DeepFM,由远及近,从最初把FM得到的隐向量和权重作为神经网络输入的FNN,到把向量内/外积从预训练直接迁移到神经网络中的PNN,再到参考wide& ...