1. 程式人生 > >hdoj1160:FatMouse's Speed(dp+最長遞減子序列思想+陣列巧妙記錄輸出)

hdoj1160:FatMouse's Speed(dp+最長遞減子序列思想+陣列巧妙記錄輸出)

目錄

FatMouse's Speed

解題思路:

ac程式碼:


FatMouse's Speed

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


解題思路:


結構體內先以weight為基準排序,再排speed,排完序後weight可能有相等的情況 ,再找符合條件的speed的最長遞減子序列

(最長遞減子序列求解的方法參見https://blog.csdn.net/Cassie_zkq/article/details/82900389,思路相同)

如何解決輸出的序號的問題呢?

1)last記錄最長遞減子序列最後一個元素的排序後的序號;

2)pre[i]陣列記錄dp[i]對應的子序列的倒數第二個元素,最後就可以找到dp[m](假設dp[m]是最大的)中所以元素的排完序的序號了;

3)out[]陣列記錄輸出;

舉個栗子:

輸出4個元素,這4個元素在sort之後的順序依次是 4 ,5,7,9

此時last=9,

且已經記錄 :pre[9]=7, pre[7]=5 , pre[5]=4, pre[4]=0;

則輸出為out[0]=9,out[1]=7,out[2]=5,out[3]=4;(用while即可實現,詳見程式碼)

最後再倒序輸出

 

ac程式碼:


#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#define maxn 100005
using namespace std;
struct node{
    int weight;
    int speed;
    int order;//記錄最初輸入的順序
}mice[maxn];
int dp[maxn],pre[maxn],out[maxn];
bool cmp(node a,node b)
{
    if(a.weight==b.weight) return a.speed>b.speed;
    else return a.weight<b.weight;//先以weight為標準排序
}
int main()
{
    int num=1,count=0,last=1;//num記錄總的mouse的個數,count記錄有多少隻符合條件的mouse
    int i,j;
    while(scanf("%d %d",&mice[num].weight,&mice[num].speed)!=EOF)
    {
        dp[num]=1;
        pre[num]=0;
        mice[num].order=num;
        num++;
    }
    num--;
    sort(mice+1,mice+1+num,cmp);//注意下標從1開始要從1開始比較,mice+1
    for(i=1;i<=num;i++)
    {
        for(j=1;j<i;j++)//尋找speed的最大遞減子序列,pd[i]記錄以mice[i]結尾的最大遞減子序列的長度
        {
            if(mice[i].weight>mice[j].weight && mice[i].speed<mice[j].speed && dp[i]<(dp[j]+1))
            {//有可能出現weight相同的情況,且要求weight一定要遞增!!
                pre[i]=j;//記錄當前子序列的倒數第二個元素
                dp[i]=dp[j]+1;
            }
        }
        if(dp[i]>count)
        {
            last=i;//last記錄最長遞減子序列的最後一個下標(從1開始)
            count=dp[i];
        }
    }
    int t=0;
    while(last!=0)
    {
        out[t++]=last;
        last=pre[last];
    }
    printf("%d\n",count);
    for(i=t-1;i>=0;i--)//倒序輸出
    {
        printf("%d\n",mice[out[i]].order);
    }
    return 0;
}