Description
HDU-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.
Solution
先按照重量排序,再求出最长上升子序列,记录模板在此。
Code
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define max(a,b) a>b?a:b
#define inf 0x3f3f3f3f
int dp[1050];
struct Mouse{
int w,s,n,l;
}m[1050];
int cmp(Mouse m1,Mouse m2){
if(m1.w==m2.w)return m1.s<m2.s;
return m1.w>m2.w;
}
int main(){
int c=1,i,j;
while(scanf("%d%d",&m[c].w,&m[c].s)!=EOF){
m[c].n=c;
m[c].l=0;
c++;
}
sort(m+1,m+c+1,cmp);
m[0].w=inf;
m[0].s=0;
m[0].l=0;
m[0].n=0;
int ans=0;
for(i=1;i<=c;i++){
dp[i]=0;
for(j=0;j<i;j++){
if(m[j].w>m[i].w&&m[j].s<m[i].s){
if(dp[j]+1>=dp[i]){
dp[i]=dp[j]+1;
m[i].l=j;
if(dp[i]>dp[ans])ans=i;
}
}
}
}
printf("%d
",dp[ans]);
while(m[ans].l!=0){
printf("%d
",m[ans].n);
ans=m[ans].l;
}
printf("%d
",m[ans].n);
return 0;
}