题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=108
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 Specification
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 Specification
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
Output for Sample Input
4 4 5 9 7
题目大意:给你一些老鼠的体重和速度,叫你找出一些老鼠,这些老鼠满足的条件是,老鼠的体重递增,与此同时速度递减。问你最多可以选几个老鼠?
最长上升子序列的简单应用啦。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<sstream>
#include<vector>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<queue>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
using namespace std;
const int maxn=1005,maxe=100005,inf=1<<29;
int n,m;
struct node
{
int w,v,id;
}a[maxn];
int dp[maxn],p[maxn];
bool cmp(node x,node y)
{
if(x.w==y.w) return x.v>y.v;
return x.w<y.w;
}
void print(int n)
{
if(n==-1) return ;
print(p[n]);
printf("%d\n",a[n].id);
}
int main()
{
int n=0,Max=0,i,j,pos;
memset(dp,0,sizeof(dp));
memset(p,-1,sizeof(p));
while(~scanf("%d%d",&a[n].w,&a[n].v))
{
a[n].id=n+1;
n++;
//if(n==9) break;
}
sort(a,a+n,cmp);
//for(int i=0;i<n;i++)
//printf("w=%d,v=%d,id=%d\n",a[i].w,a[i].v,a[i].id);
for(i=0;i<n;i++)
{
int sum=0;
for(j=0;j<i;j++)
if(a[i].w>a[j].w&&a[i].v<a[j].v&&sum<dp[j])
{
sum=dp[j],p[i]=j;
//printf("sum=%d\n",sum);
}
dp[i]=sum+1;
if(Max<dp[i])
{
Max=dp[i];
pos=i;
}
}
printf("%d\n",Max);
print(pos);
return 0;
}