现在的位置: 首页 > 综合 > 正文

杭电1160-FatMouse’s Speed(超详细解释)

2018年05月02日 ⁄ 综合 ⁄ 共 3204字 ⁄ 字号 评论关闭

FatMouse's Speed

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7138    Accepted Submission(s): 3142
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
下面是小鱼超详细解释-0-
/*题目意思(网上搜的,我英语也很差所以请见谅)
就是说,对于输入的数据,每行代表一个老鼠的信息,包含一对整数,第一个整数代表老鼠的重量W,第二个整数代表老鼠跑的速度S,如下面的输入:
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
你的程序要求从中找出这样一组n个数据,它们严格服从鼠的重量一直增大,而鼠的速度却一直减小。
(来推翻the fatter a mouse is, the faster it runs)
如对于上面的输入有
4    1000 4000
5    1100 3000
9    2000 1900
7    8000 1400
(必须所有的数据都左边一直增加,右边一直减小)
然后输出的数据中,第一行输出符合这样关系的数据的个数n,后面每一行为从小到大排列的数据所在的行号。

要注意的是输入的数据中可能有两只老鼠体重完全相同,或速度完全相同,甚至体重与速度都相同;这样对于符合的多种可能解,程序只需要找出一组就行了。
*/
#include<iostream>//动态规划
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
const int MAX=10010;//题目中给了最多1000只老鼠但是没说给几组测试数据所以尽量开大点,这里输出时是特殊输出:Ctrl+Z
int DP[MAX];//最大子序列
int rem[MAX];//记录符合条件的老鼠
using namespace std;
typedef struct Mouse
{
    int weight;//重量
    int speed;//速度
    int num;//编号
}mice;
mice s[MAX];
bool cmp(mice x,mice y)//这里安体重从大到小,速度从小到大排序当然也可以全部反过来
{
    if(x.weight==y.weight)
    return x.speed<y.speed;
    else
    return x.weight>y.weight;
}
int main()
{
    int n,m,maxn,i,j,k,t,mark;
    for(i=1;i<MAX;i++)//初始化,每只记录的老鼠都是自己
    rem[i]=i;
    k=1;
    while(scanf("%d%d",&s[k].weight,&s[k].speed)!=EOF)//特殊输入输出,输入完数据按下Ctrl+Z就会出数据
    {
        s[k].num=k;
        k+=1;
    }
    sort(s+1,s+k,cmp);//排序
    memset(DP,0,sizeof(DP));//初始化为0
    for(i=1,m=0;i<k;i++)
    {
        maxn=0;//maxn表示最大的子序列长度即找到符合条件的最多的老鼠数量
        for(j=1;j<i;j++)
        {
            if(s[i].weight<s[j].weight&&s[i].speed>s[j].speed)//如果符合条件
            {
                if(maxn<DP[j])//如果此时数量大于记录的最大数量
                {
                    maxn=DP[j];//把此时数量赋给maxn
                    mark=s[j].num;//并且记录此时这只老鼠的编号
                }
            }
            if(maxn)//如果最大数量存在
            rem[s[i].num]=mark;//把用于比较老鼠编号和记录的老鼠编号对应记录下来
            DP[i]=maxn+1;//如果不存在此时maxn==0加上1就是1即就算找不到也有它自己啊--找到了那就更新原来的值即+1
            if(m<DP[i])//m是记录符合条件的最大老鼠数量
            {
                m=DP[i];
                t=s[i].num;//t是记录最后一只符合条件的老鼠编号方便输出
            }
        }
    }
    if(m==1)//当m等于1时即没有符合条件的其他老鼠那只能是随便哪只都可以输出所以我就固定输出1(数量)和1(编号)
    {
        cout<<1<<endl<<1<<endl;
    }
    else//否则
    {
        cout<<m<<endl;//输出符合条件的最大数量
        while(rem[t]!=t)//如果此时老鼠编号对应的老鼠不是自己
        {
            cout<<t<<endl;//那么输出这只老鼠的编号
            t=rem[t];//并且把对应的老鼠的编号重新赋给t
        }
        cout<<t<<endl;//因为对应的最后一只老鼠的编号的rem[对应的最后一只老鼠]=它自己。。所以我们要把对应的最后一只老鼠的编号单独输出来
    }
    return 0;//完毕
}
 

抱歉!评论已关闭.