HDU1160_FatMouse's Speed【LIS变形】

来源:互联网 发布:java中的thread.sleep 编辑:程序博客网 时间:2024/05/29 14:19
FatMouse's Speed


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9624    Accepted Submission(s): 4284
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


题目大意:胖老鼠以为越胖的老鼠跑的越快。现在给你一些老鼠的重量和速度,

直到输入到文件结束,问:你能否找到一系列数据,来证明越胖的老鼠跑调越慢

最后输出满足条件的数据个数,并按顺序输出每组数据原本所在的行数。

思路:先用结构体存起来每只老鼠的体重、速度和所在行号。然后对结构体排序。

先按体重递增排序,再按速度递减排序。之后按最长递减序列的求法求出符合的

据最大个数。用x[]数组存嘴上递减子序列的长度,用y[]记录排序后的最长递减

子序列倒数第二个的下标。这样能够通过往前递推的方法依次找到最长递减子序

列的路径。用z[]数组记录路径,并输出。


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct node{    int a;    int b;    int i;}num[1010];int x[1010],y[1010],z[1010];bool cmp(node x,node y){    if(x.a==y.a)        return x.b > y.b;    return x.a < y.a;}int main(){    int M,N;    int i = 1;    memset(num,0,sizeof(num));    while(~scanf("%d%d",&M,&N))    {        num[i].i = i;        num[i].a = M;        num[i++].b = N;    }    sort(num+1,num+i,cmp);    int Max = 0,pos = -1;    for(int j = 1; j < i; j++)    {        x[j] = 1;        y[j] = -1;    }    for(int j = 2; j < i; j++)    {        for(int k = 1; k < j; k++)        {            if(num[k].a < num[j].a && num[k].b > num[j].b && x[k]+1>x[j])//满足体重增,速度减            {                x[j] = x[k]+1;                y[j] = k;//记录排序后的最长子序列倒数第二个的下标            }        }        if(x[j] > Max)//最长子序列的长度和最后一位的下标        {            Max = x[j];            pos = j;        }    }    int count = 0;    while(pos != -1)//得到    {        z[count++] = pos;        pos = y[pos];    }    printf("%d\n",Max);    for(int i = count-1; i>=0; i--)        printf("%d\n",num[z[i]].i);    return 0;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 车停在停车场被划怎么办 专升本差两分怎么办 入职需要学士学位证怎么办 不喜欢写科研项目又没编制怎么办 易学堂密码忘了怎么办 易班手机号换了怎么办 易班登录不上怎么办 易到手机号换了怎么办 海外留学没有教育部认证怎么办 七过月宝宝便秘怎么办 6个月孩子便秘怎么办 6个月婴儿便秘怎么办 一个多月宝宝两天没拉大便怎么办 7个月宝宝便秘怎么办 9个月宝宝便秘怎么办 六个月宝宝严重便秘怎么办 公司调岗员工不同意怎么办 acca注册一直在审核怎么办 ieee ap二审被拒怎么办 文章投到假期刊怎么办 论文投了假网站怎么办 网上传了虚假的怎么办? 网上做兼职被骗了钱怎么办 通过支付宝扫码被骗了怎么办 支付宝扫二维码被骗怎么办 在is上被骗了怎么办 微信兼职被骗怎么办天涯论坛 公众号不给稿费怎么办 围绕服务改善民生提出怎么办 翰墨香林苑怎么办全产权 西城高铁联名卡怎么办 英语教学系统做题时超时了怎么办 sci被要求撤稿怎么办 论文投稿后初审通过想撤稿怎么办 河南大学读写译挂了怎么办 在职mba双证硕士怎么办户口 电脑开机了出现一些英文单词怎么办 不懂法语想读法语书怎么办 大鱼号文章需要修改怎么办 过了上诉期怎么办申诉 法院判刑后被告逃亡监护人怎么办