首页 > 代码库 > 2014061404吃豆豆(C++)

2014061404吃豆豆(C++)

#include<cstdio>
#include<algorithm>
#define N 10010
using namespace std;
int n,i,j,rd[N],t,w,tot,id[N];
int dp,pre[3000000],p[N],tt[3000000],z[N],ID[N],tmp;
int f[2500][2500];
struct g{
int x,y;
}a[N];
bool cmp(g a,g b)
{
if (a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
void link(int x,int y)
{
dp++;pre[dp]=p[x];p[x]=dp;tt[dp]=y;
}
void Dp(int x,int y)
{
int i;
i=p[x];
while (i)
{
int a=tt[i],b=y;
if (id[a]>id[b])
swap(a,b);
if (a!=b)
f[a][b]=max(f[a][b],f[x][y]+1);
else
f[a][b]=max(f[a][b],f[x][y]);
i=pre[i];
}
}
int main()
{
scanf("%d",&n);

for (i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+1+n,cmp);
for (i=1;i<=n;i++)
{
tmp=0x37373737*2;
for (j=i+1;j<=n;j++)
if ((i!=j)&&(a[i].x<=a[j].x)&&(a[i].y<=a[j].y)&&(a[j].y<tmp))
{
tmp=a[j].y;
rd[j]++;
link(i,j);
}
}
for (i=1;i<=n;i++)
{
link(0,i);rd[i]++;
link(i,n+1);rd[n+1]++;
}
t=0;w=1;z[w]=0;
while (t!=w)
{
tot++;
t++;
id[z[t]]=tot;
ID[tot]=z[t];
i=p[z[t]];
while (i)
{
rd[tt[i]]--;
if (rd[tt[i]]==0)
{
w++;z[w]=tt[i];
}
i=pre[i];
}
}


for (i=1;i<=tot;i++)
for (j=i;j<=tot;j++)
Dp(ID[i],ID[j]);
printf("%d\n",f[n+1][n+1]-1);
}

2014061404吃豆豆(C++)