http://acm.hdu.edu.cn/showproblem.php?pid=1160
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-记录路径问题
记录路径
void print(int pos)
{
if(pre[pos]!=-1){
print(pre[pos]); //自下往上回溯
printf("%d\n",m[pos].id);
}
}
在刚开始dp寻找最长序列的时候,已经记录了此时的下标。
memset(pre,-1,sizeof(pre)); //初始化pre记录数组
for(int i=1;i<=t;i++){
int cnt = 0;
for(int k=0;k<i;k++){
if(m[i].w>m[k].w&&m[i].s<m[k].s&&dp[k]>dp[cnt])
cnt = k; //寻找最优K
}
dp[i] = dp[cnt]+1; //增加长度
pre[i] = cnt; //记录
}
int _max = 0;
for(int i=1;i<=t;i++)
if(dp[i]>dp[_max])
_max = i; //寻找最长长度的起点下标
其中的 pre[i] 就是说在计算第i个数的时候,与它相连的前一个数是 k,并且用 pre 数组记录下来!!!
这样,通过 pre 就可以把每个数给联系起来。
那么也就有了 print 函数里面 if 里面的pre[pos] != -1。
因为记录下标的时候是逆向的,所以输出的时候也要是逆向的。
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<utility>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define ll long long
#define llu unsigned long long
using namespace std;
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
int dp[maxn];
int pre[maxn];
struct In
{
int w,s,id;
}m[maxn];
bool cmp(In &a,In &b) //排序,对w排序,方便
{
if(a.w!=b.w) return a.s > b.s;
else return a.w < b.w;
}
void print(int pos) //输出
{
if(pre[pos]!=-1){
print(pre[pos]);
printf("%d\n",m[pos].id);
}
}
int main(void)
{
int t=1;
while(scanf("%d%d",&m[t].w,&m[t].s)!=EOF){
m[t].id = t;
t++;
}
sort(m+1,m+t+1,cmp);
for(int i=1;i<=t;i++)
dp[i] = 1;
memset(pre,-1,sizeof(pre)); //初始化pre记录数组
for(int i=1;i<=t;i++){
int cnt = 0;
for(int k=0;k<i;k++){
if(m[i].w>m[k].w&&m[i].s<m[k].s&&dp[k]>dp[cnt])
cnt = k; //寻找最优K
}
dp[i] = dp[cnt]+1; //增加长度
pre[i] = cnt; //记录
}
int _max = 0;
for(int i=1;i<=t;i++)
if(dp[i]>dp[_max])
_max = i; //寻找最长长度的起点下标
printf("%d\n",dp[_max]); //输出最长长度
print(_max);
return 0;
}