FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19396 Accepted Submission(s): 8582
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.
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
LIS变形+输出
现在写个什么LIS的dp问题倒是挺熟练的了。
题意是让你找出体重递增,速度递减最长的子序列。
要输出长度和序号。
首先按照体重升序或者速度降序排序。
我是用体重升序做的,然后求体重的最长下降子序列就可以了。
注意虽然体重已经升过序列但还是要判断一下体重的前后关系。
因为题目要求的是严格递增(递减)
(但数据很弱不判断也能过)
至于输出的问题,用一个pre数组就可以了,然后递归输出。
此外还需要保存结点的原始顺序。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <math.h>
#include <queue>
#include <vector>
#include <climits>
using namespace std;
const int maxn = 2e3;
int maxlen[maxn];
int pre[maxn];
struct mouse
{
int W,S,index;
mouse(int w, int s,int i)
{
W = w;
S = s;
index = i;
}
mouse() {}
};
mouse a[maxn];
int cmp(const void *a, const void *b)
{
mouse pa = *(mouse*)a, pb = *(mouse*)b;
return pa.W-pb.W;
}
void output(int i)
{
if(pre[i] == -1)
{
cout<<a[i].index<<endl;
return;
}
output(pre[i]);
cout<<a[i].index<<endl;
}
int main()
{
int w,s,cnt = 1;
while(cin>>w>>s)
{
a[cnt] = mouse(w,s,cnt);
cnt++;
}
qsort(a+1,cnt-1,sizeof(a[0]),cmp);
for(int j = 1; j < cnt; j++)
maxlen[j] = 1;
memset(pre,-1,sizeof(pre));
for(int i = 2; i < cnt; i++)
for(int j = 1; j < i; j++)
{
if(a[j].S > a[i].S && a[j].W < a[i].W)//W仍然要判断
if(maxlen[i] < maxlen[j]+1)
{
pre[i] = j;
maxlen[i] = maxlen[j]+1;
}
}
int temp = max_element(maxlen+1,maxlen+cnt) - maxlen;
cout<<maxlen[temp]<<endl;
output(temp);
return 0;
}