HDU 1160 FatMouse's Speed

这道题不是太难,但是需要注意几个地方,因为没有说明有多少组数据,所以用while(scanf!=EOF),之前只知道多组数据输入用这个,但是原因却不明白,现在知道了,就是一直输入,直到文件结束的意思,还有就是最长上升子序列的路径打印,自己想到的是用vector来记录路径,将所有的路径都记录下来,然后找到最长的那个,最后将其输出。
至于怎么来记录,可以这样想,每一个数字对应的从开始到自己的最长递增序列中自己前面的那个数字的序列加上自己。
比如 1 5 3 4
对应的递增下标是1 2 2 3 而4的递增子序列是1 3 4,3的递增子序列是1 3,那么4的递增子序列就是3的递增子序列加上自己,而3的递增子序列是1加上自己。而1就是本身,这样在for循环中加一句if就好了。

FatMouse’s Speed

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

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
struct stu
{
    int x,y,xb;
} a[1010];
int b[1010];
int cmp(stu aa,stu bb)
{
    if(aa.y==bb.y)
        return aa.x>bb.x;
    return aa.y>bb.y;
}
int main()
{
// freopen("1.txt","r",stdin);
    vector<int>v[1010];
    int x,y,n=0,i,j,minn,xb=0;
    while(scanf("%d%d",&x,&y)!=EOF)
    {
        a[++n].x=x;
        a[n].y=y;
        a[n].xb=n;
    }
    sort(a+1,a+n+1,cmp);
// for(i=1; i<=n; i++)
// printf("%d %d %d\n",a[i].x,a[i].y,a[i].xb);
    b[1]=1;
    v[1].push_back(a[1].xb);
    for(i=2; i<=n; i++)
    {
        b[i]=1,minn=0;
        for(j=1; j<i; j++)
                if(a[i].x>a[j].x&&b[i]<=b[j])
                    b[i]=b[j]+1,minn=j;
        if(minn)
            v[i]=v[minn],v[i].push_back(a[i].xb);
        else
            v[i].push_back(a[i].xb);
    }
    minn=b[1],xb=1;
    for(i=1; i<=n; i++)
        if(b[i]>minn)
            minn=b[i],xb=i;
    printf("%d\n",minn);
    for(i=0; i<v[xb].size(); i++)
        printf("%d\n",v[xb][i]);
}

你可能感兴趣的:(动态规划,hdu日常小练)