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
思路:
根据题意,我们可以按重量排序,然后去找速度的最大下降子序列,重量相同的要把速度大的放在前面。
第一种情况,序列只有他自己 dp[i] = 1
第二种情况,a[i].w>a[j].w && a[i].s<a[j].s i>j dp[i] = max(dp[i],dp[j]+1)
用pre 数组去存前驱,然后放在栈里输出。
AC代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<stack>
#define INF 0x3f3f3f3f
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define sca(x) scanf("%d", &x)
#define scas(x) scanf("%s",x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pris(x) printf("%s\n",x)
#define prl(x) printf("%lld\n",x)
//#include <bits/stdc++.h>
typedef long long ll;
const int maxn=1e6+7;
const int mod=1e9+7;
const double eps=1e-8;
using namespace std;
int dp[maxn];
int pre[maxn];
stack<int>s;
struct node
{
int w,s;
int id;
}a[maxn];
bool cmp(node a,node b)
{
if(a.w == b.w)
return a.s>b.s;
return a.w<b.w;
}
int main()
{
int n = 0;
while(sca2(a[n].w,a[n].s)!=EOF)
{
a[n].id = n+1;
n++;
}
sort(a,a+n,cmp);
// rep(i,0,n)
// {
// printf("%d %d %d\n",a[i].w,a[i].s,a[i].id);
// }
int cnt = 1;
int id = 0;
rep(i,0,n)
{
dp[i] = 1;
rep(j,0,i)
{
if(a[i].w>a[j].w && a[i].s<a[j].s)
{
if(dp[i] < dp[j]+1)
{
dp[i] = dp[j]+1;
pre[i] = j;
}
}
}
if(cnt<dp[i])
{
cnt = dp[i];
id = i;
}
}
pri(cnt);
if(id == 0)
{
pri(dp[0]);
}
while(id)
{
s.push(id);
id = pre[id];
}
while(!s.empty())
{
pri(a[s.top()].id);
s.pop();
}
return 0;
}