FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13391 Accepted Submission(s): 5893
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.
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
根据w升序排序,然后对s求最长递减子序列,注意保存每个元素的位置用于最后输出
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
//w为体重,s为速度,pos为最初输入时在数组中的位置
struct node {
int w, s, pos;
}a[1005];
bool cmp(node x, node y) {
return x.w < y.w;
}
int ans[1005], pre[1005];
int main()
{
int tw, ts, len;
len = 0;
while (~scanf("%d%d", &tw, &ts)) {
a[len].w = tw;
a[len].s = ts;
a[len].pos = len; //记录每个node的在数组中原来的位置,注意此时是从0开始的,回来打印的时候要加1
len++;
}
sort(a, a + len, cmp); //根据w升序排列
memset(ans, 0, sizeof(ans));
ans[0] = 1; //初始化边界
pre[0] = 0;
int maxn, index, MAX = 1; //最长递减子序列长度MAX初始为1
for (int i = 1; i < len; i++) { //i: 1 -> len - 1
maxn = 1; //当前位置i的最长长度初始为1
index = i; //初始的前驱为i自身
//考察前面的最优解,即i之前到比a[i].s大的某个位置j的最长子序列长度
for (int j = 0; j < i; j++) {
//注意还要满足a[j].w < a[i].w,因为题目要求w严格递增
if (a[j].s > a[i].s && a[j].w < a[i].w && ans[j] + 1 > maxn) {
maxn = ans[j] + 1;
MAX = max(MAX, maxn); //注意更新最大值
index = j;
}
}
//到当前位置的递减子序列长度最大值是maxn
ans[i] = maxn;
//记录前驱,用于最后输出
pre[i] = index;
}
printf("%d\n", MAX);
int dx;
//查找最长递减子序列的最后一个元素的位置dx
for (int i = 0; i < len; i++) {
if (ans[i] == MAX) {
dx = i;
break;
}
}
//用一个栈输出对前驱进行处理
//注意最后要输出的是刚开始时在数组里的位置
//当某个元素的前驱是自身的时候说明它就是最长子序列的第一个元素
//跟并查集找父节点的思想如出一辙
stack<int> s;
do {
s.push(a[dx].pos + 1);
dx = pre[dx];
} while (pre[dx] != dx);
s.push(a[dx].pos + 1);
//输出经栈处理过的顺序
while (!s.empty()) {
printf("%d\n", s.top());
s.pop();
}
return 0;
}