FatMouse's Speed
HDU - 1160
题意: 若干只老鼠, 每只老鼠有体重, 速度两个属性, 要求输出按体重递增, 速度递减的最长序列;
先按速度由大到小排序, 然后找体重的最长子序列;O(n^2)复杂度的算法就行;
因为要求记录路径, 用path[]数组记录;
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct node{
int w, v, id;
}m[1100];
bool cmp(node x, node y){
return x.v>y.v;
}
int dp[1100], path[1100];
void print(int k){
if(k==-1) return;
print(path[k]);
printf("%d\n", m[k].id+1);
}
int main(){
int cnt=0, x, y;
while(~scanf("%d%d", &x, &y)){
dp[cnt]=0;
path[cnt]=-1;
m[cnt].id=cnt;
m[cnt].w=x;
m[cnt++].v=y;
}
sort(m, m+cnt, cmp);
for(int i=0; i<cnt; i++){
int temp=0;
for(int j=0; j<i; j++){
if(m[i].w>m[j].w){
if(temp<=dp[j]){
temp=dp[j];
path[i]=j;
}
}
}
dp[i]=temp+1;
}
int ans=0, k=0;
for(int i=0; i<cnt; i++){
if(ans <= dp[i]){
ans=dp[i];
k=i;
}
}
printf("%d\n", ans);
print(k);
return 0;
}