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.
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.
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
4
5
9
7
题意 找一组最长的数据,数据的体重逐渐增加,速度逐渐减小,输出序列标号
方法 在体重递增的序列中,找速度最长递减序列,同时记住每个序列的上个序列
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
#define N 4000
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a));
vector<vector<int> >Q;
struct node
{
int p,w,i,l; } s[N];
int dp[N];
int cmp(node a,node b)
{
if(a.w!=b.w)
return a.w>b.w;
return a.p<b.p;
}
int main()
{
int k=;
while(scanf("%d%d",&s[k].w, &s[k].p)!=EOF)
{
s[k].i=k+;
s[k].l=-;
k++;
}
sort(s,s+k,cmp);
for(int i=; i<=k; i++)
dp[i]=;
dp[]=;
int ans=;
for(int i=; i<k; i++)
{
for(int j=; j<i; j++)
{
if(s[i].p>s[j].p && s[i].w<s[j].w)
{
if(dp[j]+>=dp[i])
{
dp[i]=dp[j]+;
s[i].l=j;
}
} if(dp[i]>dp[ans])
ans=i;
}
}
printf("%d\n",dp[ans]);
while(s[ans].l!=-)
{
printf("%d\n",s[ans].i);
ans=s[ans].l;
}
printf("%d\n",s[ans].i);
return;
}