题意:
现在给你一些数据输入,第一个代表的是体积,第二个代表的是它的速度。然后题目让你找到当体积递增但是速度递减时的最长的一个子序列,注意这里体积是要严格的递增的,速度则是要严格的递减的。最后要你把他们的序号输出。
思路:
是不是有点感觉像LIS问题。但是这里还是有点差别的。因为它要让你记录。
首先我们先对体积从小到大排序,然后我们对速度进行最长递减子序列的查询。
这里的记录前驱和我们上次的那个迷宫问题(poj 3984)一样,只需要另外开一个数组就好了。
链接:http://blog.youkuaiyun.com/acmer_hades/article/details/47069073
感觉掌握了一个题,就可以引申到另外一个题目中去的。毕竟方法都是差不多的。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<math.h>
using namespace std;
#define maxn 1111
int dp[maxn],fa[maxn];
struct node{
int w,s;
int idx;
}a[maxn];
bool cmp(node a,node b){
return a.w<b.w;
}
void print(int t){
if(fa[t]==-1){
printf("%d\n",a[t].idx);
return;
}
print(fa[t]);
printf("%d\n",a[t].idx);
}
int main(){
int n=1;
while(~scanf("%d%d",&a[n].w,&a[n].s)){
a[n].idx=n++;
}
sort(a+1,a+n,cmp); //注意这里要从小到大排序,要不然会WA。
fill(dp,dp+n,1);
memset(fa,-1,sizeof(fa));
int dp_max=0,nmax=0,f=0;
//以下是重点,与平时所写的LIS有点区别;
for(int i=1;i<=n-1;i++){
f=-1;
dp_max=0;
for(int j=1;j<i;j++){
if(a[i].s<a[j].s&&a[i].w>a[j].w){
if(dp[j]>dp_max){
dp_max=dp[j];
f=j;
}
}
}
dp[i]=dp_max+1;
fa[i]=f;
if(dp[i]>dp[nmax]) nmax=i;
}
int len=dp[nmax];
printf("%d\n",len);
print(nmax);
}
/*
9
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
*/
Get better day by day!