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.
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.
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
总结:
好水好水的一道题,思路不难想,但是要对逆序的数列的性质要去思考一下,需要逆序的数列中的每两个元素两两之间都是逆序对的关系,然后我们就可以定义状态,然后就不难解决,这道题比较debug时间很长,写代码的时候一定要小心,不然一个错误废掉的时间真的是太长了,还有用栈来实现步骤的打印还是可以的
// // main.cpp // FatMouse's Speed // // Created by 张嘉韬 on 16/8/15. // Copyright © 2016年 张嘉韬. All rights reserved. // #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <queue> #include <stack> using namespace std; const int maxn=1000+10; const int inf=99999999; int f[maxn][2]; struct mice { int w; int s; int num; }mices[maxn]; int cmp(mice a,mice b) { return a.w<b.w; } int main(int argc, const char * argv[]) { //freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin); int n=0,w,s,r=0,p=0,a[maxn]; while(scanf("%d%d",&w,&s)!=EOF) { n++; mices[n].w=w; mices[n].s=s; mices[n].num=n; } sort(mices+1,mices+n+1,cmp); //for(int i=1;i<=n;i++) cout<<mices[i].w<<" "<<mices[i].s<<" "<<mices[i].num<<endl; for(int i=0;i<=n;i++) {f[i][0]=0; f[i][1]=i;} for(int i=1;i<=n;i++) // is the staion of a[i] { int maximum=0,maxp=i; for(int j=1;j<i;j++) { if(mices[i].s<mices[j].s&&mices[i].w>mices[j].w) { //cout<<"shit"<<endl; if(f[j][0]>maximum) {maximum=f[j][0]; maxp=j;} } } f[i][0]=maximum+1; f[i][1]=maxp; if(f[i][0]>=r) {r=f[i][0]; p=i;} //cout<<f[i][0]<<" "<<f[i][1]<<endl; } printf("%d\n",r); stack <int> st; int tem=p; while(f[tem][1]!=tem) { st.push(mices[tem].num); tem=f[tem][1]; } st.push(mices[tem].num); while(st.empty()!=1) { printf("%d\n",st.top()); st.pop(); } return 0; }