题目链接 HDU 1160
题目大意
每一只老鼠有两个参数,在n只老鼠中找出最大数量的老鼠,这些老鼠的第一个参数是递增的,第二个参数是递减的,输出这个数量并且输出这些老鼠的编号。
解题思路
其实用的就是最长上升子序列,然后主要是要保存这些老鼠的编号,我本来使用vector模拟保存的,但是吧。。。。然后一直wa,以为是我的思路不对,其实后面发现是智障的b打成了a,在网上还学到用pre数组保存前一个的值,回溯输出,这个好像比较方便我的代码看起来有点智障。
首先是pre数组的
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
int g[10005],pre[1005],ans[1005];
struct node
{
int a,b,id;
};
node e[10005];
int cmp(node x,node y)
{
if(x.a==y.a)
return x.b>y.b;
return x.a<y.a;
}
int main()
{
int x,y;
int tot=0;
while(~scanf("%d %d",&x,&y))
{
e[++tot].a=x;
e[tot].b=y;
e[tot].id=tot;
}
memset(pre,0,sizeof(pre));
sort(e+1,e+tot+1,cmp);
int maxx=1,k=1;
for(int i=1;i<=tot;i++)
{
g[i]=1;
int flag=0,c=0;
for(int j=1;j<i;j++)
{
if(e[i].a>e[j].a&&e[i].b<e[j].b)
{
if(g[i]<g[j]+1)
{
g[i]=g[j]+1;
pre[i]=j;
}
}
}
if(maxx<g[i])
{
maxx=g[i];
k=i;
}
}
printf("%d\n",maxx);
for(int i=1;i<=maxx;i++)
{
ans[i]=k;
k=pre[k];
}
for(int i=maxx;i>=1;i--)
printf("%d\n",e[ans[i]].id);
return 0;
}
然后是我的vector模拟的
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
int g[1005];
struct node
{
int a,b,id;
};
node e[100005];
vector<int>G[1005];
int cmp(node x,node y)
{
if(x.a==y.a)
{
if(x.b==y.b)
return x.id<y.id;
return x.b>y.b;
}
return x.a<y.a;
}
int main()
{
int x,y;
int tot=1;
while(~scanf("%d %d",&x,&y))
{
e[tot].a=x;
e[tot].b=y;
e[tot].id=tot;
tot++;
}
sort(e+1,e+tot,cmp);
int maxx=0,k=0;
for(int i=1;i<tot;i++)
{
g[i]=1;
int flag=0,c=0;
for(int j=1;j<i;j++)
{
if(e[i].a>e[j].a&&e[i].b<e[j].b)
{
if(g[i]<g[j]+1)
{
g[i]=g[j]+1;
flag=1;
c=j;
}
}
}
if(flag)
{
//printf("!!!%d %d\n",i,c);
int l=G[c].size();
for(int j=0;j<l;j++)
G[i].push_back(G[c][j]);
}
G[i].push_back(e[i].id);
if(maxx<g[i])
{
maxx=g[i];
k=i;
}
}
printf("%d\n",maxx);
int len=G[k].size();
for(int i=0;i<len;i++)
printf("%d\n",G[k][i]);
return 0;
}