FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 20803    Accepted Submission(s): 9227
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

题意

有若干老鼠,每个老鼠都有体重和奔跑的速度。找到一个最长的老鼠序列,使老鼠的体重递增,速度递减,并输出这些老鼠的编号

思路

将老鼠的体重按照递增顺序排序,然后查找符合题目要求的老鼠序列,同时记录下老鼠排序之后的老鼠的编号。然后找到最长序列中的最后一个老鼠(体重最小,速度最大),然后递归寻找就行了

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,-1,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
int dp[maxn];
// vis[i]=j 表示序列中第i个老鼠的前一个老鼠是第j个老鼠
int vis[maxn];
struct wzy
{
int weight,speed;
int id;
}p[maxn];
bool cmp(wzy u,wzy v)
{
return u.weight>v.weight;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int k=0;
while(cin>>p[k].weight>>p[k].speed)
{
p[k].id=k+1;
dp[k]=1;
k++;
}
sort(p,p+k,cmp);
int ans=0;
int res;
ms(vis);
for(int i=0;i<k;i++)
{
for(int j=0;j<i;j++)
{
if(p[i].weight<p[j].weight&&p[i].speed>p[j].speed)
{
if(dp[i]<dp[j]+1)
{
dp[i]=dp[j]+1;
vis[i]=j;
}
}
}
if(dp[i]>ans)
{
ans=dp[i];
res=i;
}
}
cout<<ans<<endl;
// res=-1时表示该序列已经输出完全
while(res!=-1)
{
cout<<p[res].id<<endl;
// 更新老鼠编号
res=vis[res];
}
return 0;
}

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

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

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

  2. HDU 1160 FatMouse's Speed LIS DP

    http://acm.hdu.edu.cn/showproblem.php?pid=1160 同样是先按它的体重由小到大排,相同就按speed排就行. 这样做的好处是,能用O(n^2)枚举,因为前面的 ...

  3. HDU 1160 FatMouse's Speed(要记录路径的二维LIS)

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

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

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

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

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

  6. HDU 1160 FatMouse's Speed (DP)

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

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

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

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

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

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

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

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

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

随机推荐

  1. facebook广告上传Invalid appsecret_proof provided in the API argument

    Status: 400 Response: { "error": { "message": "Invalid appsecret_proof prov ...

  2. 模块化&os&sys

    syspath python 使用import模块调用的优先级是根据sys.path路径来的,此变量中位置在列表中的先后顺序来调用,如果先找到对应的模块,则先调用此模块. import sys pri ...

  3. HashMap与TreeMap按照key和value排序

    package com.sort; import java.util.ArrayList; import java.util.Collections; import java.util.Compara ...

  4. dgango中admin下添加搜索功能

    admin下添加搜索功能: 在表单中加入search_fields = ['ip','hostname']   可模糊匹配 当有人在管理搜索框中进行搜索时,Django将搜索查询分解成单词,并返回包含 ...

  5. python,判断操作系统是windows,linux

    import sys,platform print(sys.platform) print(platform.system()) sys.platform: 获取当前系统平台. platform.sy ...

  6. mybatis动态sql #和$的区别

    $和#都支持动态sql:就是你传什么它就是什么 区别: 1.#可以防止sql注入在sql执行时显示 '?' 比$安全 SELECT * FROM table WHERE id = ? 2.在使用#传入 ...

  7. spring的配置文件解析(转)

    http://www.cnblogs.com/as-dreamer/p/6523215.html 我们在使用Spring框架的时候首先要配置其xml文件,大量的头信息到底代表了什么呢,在这里总结下自己 ...

  8. DOS debug 命令的详细用法

    DOS下的DEBUG命令的详细用法       2 推荐 名称 解释 格式 a (Assemble) 逐行汇编 a [address] c (Compare) 比较两内存块 c range addre ...

  9. Linux文件系统命令 ls

    名称:ls 功能:查看文件列表 renjg@renjg-HP-Compaq-Pro--MT:~$ ls add-on.yaml Desktop examples.desktop meta-gnome3 ...

  10. Web API 跨域访问(CORS)

    1.在web.config里把“    <remove name="OPTIONSVerbHandler" />  ”删掉. 2. 到nuget上装一个包:    ht ...