FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14980    Accepted Submission(s): 6618
Special Judge

Problem Description
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.

 
Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file.

The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.

Two mice may have the same weight, the same speed, or even the same weight and speed.

 
Output
Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that

W[m[1]] < W[m[2]] < ... < W[m[n]]

and

S[m[1]] > S[m[2]] > ... > S[m[n]]

In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.

 
Sample Input
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
 
Sample Output
4
4
5
9
7
 

题目链接:HDU 1160

比较裸的二维LIS,但是WA了无数次, 好像是把id和下标搞混了………………,由于题目说重量weight要递增的情况下speed要递减,显然是先对weight进行排序再按speed关键字找最长严格下降子序列,但这样不符合个人习惯……于是就换了一下按weight递减,speed递增,找最长严格上升子序列,显然当weight相同时要把speed小的排前面,然后记录每一个可能组成LIS的老鼠的前驱,由于顺序是反着找的,最后回溯出来的顺序就是答案了,不用放到栈里,但由于要统计最长的个数,还是放到队列里方面。我这个代码出来的样例答案是 4 4 5 6 1,也是符合题意的

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1010;
struct info
{
int speed;
int weight;
int pre;
int id;
bool operator<(const info &t)const
{
return (weight>t.weight||(weight==t.weight&&speed<t.speed));
}
};
info arr[N];
int dp[N]; int main(void)
{
int w,s,i,j;
int cnt=0;
while (~scanf("%d%d",&w,&s))
{
arr[cnt].id=cnt+1;
arr[cnt].pre=-1;
arr[cnt].weight=w;
arr[cnt].speed=s;
++cnt;
}
sort(arr,arr+cnt);
CLR(dp,0);
for (i=0; i<cnt; ++i)
{
int tpre=-1;
int pre_max=0;
for (j=0; j<i; ++j)
{
if(dp[j]>pre_max&&arr[j].speed<arr[i].speed&&arr[j].weight>arr[i].weight)
{
pre_max=dp[j];
tpre=j;
}
dp[i]=pre_max+1;
arr[i].pre=tpre;
}
}
int indx=max_element(dp,dp+cnt)-dp;
queue<int>pos;
while (indx!=-1)
{
pos.push(arr[indx].id);
indx=arr[indx].pre;
}
printf("%d\n",pos.size());
while (!pos.empty())
{
printf("%d\n",pos.front());
pos.pop();
}
return 0;
}

HDU 1160 FatMouse's Speed(要记录路径的二维LIS)的更多相关文章

  1. HDU 1160 FatMouse's Speed 动态规划 记录路径的最长上升子序列变形

    题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了 ...

  2. HDU - 1160 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化

    HDU - 1160 给一些老鼠的体重和速度 要求对老鼠进行重排列,并找出一个最长的子序列,体重严格递增,速度严格递减 并输出一种方案 原题等于定义一个偏序关系 $(a,b)<(c.d)$ 当且 ...

  3. hdu 1160 FatMouse's Speed(最长不下降子序列+输出路径)

    题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to ...

  4. HDU 1160 FatMouse's Speed (DP)

    FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  5. HDU 1160 FatMouse's Speed (动态规划、最长下降子序列)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. 题解报告:hdu 1160 FatMouse's Speed(LIS+记录路径)

    Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...

  7. 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed

    https://vjudge.net/contest/68966#problem/J [Accepted] #include<iostream> #include<cstdio> ...

  8. hdu 1160 FatMouse's Speed (最长上升子序列+打印路径)

    Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...

  9. hdu 1160 FatMouse's Speed 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题目意思:给出一堆老鼠,假设有 n 只(输入n条信息后Ctrl+Z).每只老鼠有对应的weigh ...

随机推荐

  1. for的冒泡排序练习题

    这是一个冒泡排序的方法,请汲取其中的思想.有一组数: 1,2,3,4,5,6请将这组数用降序排列.我们可以将数组里面的数两两相比,如果第二个数比第一个数大,那么将第二个数值与第一个数值交换,然后让其循 ...

  2. html标题上加上小图标

    一般网站标题上都会有小图标,后面跟上文字,实现代码如下: <link rel = "Shortcut Icon" href="images/nav_logo.ico ...

  3. iOS学习07之C语言指针

    本次随笔主要是为了学习和理解C语言中的指针,指针树状图如下: 1.访问数据的两种方式 1> 直接访问:定义变量后,直接访问变量 ; printf("a = %d\n", a) ...

  4. BZOJ1114 : [POI2008]鲁滨逊逃生Rob

    设船最宽行列的交点为船的重心,那么只要预处理出重心在每个位置是否可行,以及在边界上走出边界所需的最小值之后,进行一遍BFS即可. 枚举每个点$(x,y)$,求出它上下最近的障碍物的距离.考虑重心在第$ ...

  5. [深入浅出WP8.1(Runtime)]应用实例——移动截图

    10.2应用实例——移动截图 移动截图例子是实现一个把一张图片的某个部分截取出来的功能,并且用户可以选定截取的图片区间.那个该例子会使用ManipulationDelta事件来实现对截取区间的选择.然 ...

  6. git 放弃本地某个文件的修改,或所有修改

    18:57 2015/11/17git 放弃本地某个文件的修改,或所有修改git checkout 文件名git checkout // 放弃所有文件的所有修改git reset --hard 版本号 ...

  7. tornado 学习笔记4 异步以及非阻塞的I/O

    Read-time(实时)的网站需要针对每个用户保持长时间的连接.在传统的同步网站服务中,通常针对每个用户开启来一个线程来实现,但是这样做非常昂贵. 为了使并发连接的成本最小化,Tornada使用单个 ...

  8. 【JAVA】通过公式字符串表达式计算值,网上的一种方法

    public class Test {    public static void main(String[] args) {     SimpleCalculator s=new SimpleCal ...

  9. #define is unsafe——I

    I. #define is unsafe Have you used #define in C/C++ code like the code below? #include <stdio.h&g ...

  10. mysql安装及卸载

    一.关于mysql MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是 ...