hdu 3998(最大上升子序列加序列数目) (一)

2014-11-24 02:21:14 · 作者: · 浏览: 5

我看别人都是用网络流做的,我不会网络流。

我的方法是正常的求出最大上升子序列,然后对这个序列中所有的值全部做一下标记,标记这个数据已经使用过。然后对剩下的数据继续进行求最大上升子序列的操作。直到求出的序列长度达不到最大长度。

这样能AC是建立在hdu的测试数据比较弱和数据量比较小的情况下,在最恶劣的状况下,这样算的时间复杂度是O(n^3)。

还是不习惯用codeblocks debug。


[cpp]
#include
#include
#define N 500
struct node
{
int x,pre;
int count;
} a[N];
int mark[N];
int main()
{
int n;
int i,j,ans;
int count;
while(scanf("%d",&n)!=EOF)
{
for(i=0; i {
scanf("%d",&a[i].x);
a[i].pre=i;
a[i].count=1;
mark[i]=0;
}
for(i=0; i {
for(j=0; j {
if(a[j].x=a[i].count)
{
a[i].count=a[j].count+1;
a[i].pre=j;
}
}
}
ans=0;
int temp;
for(i=0; i {
if(ans {
ans=a[i].count;
temp=i;
}
}
int x;
x=temp;
while(1)
{
mark[x]=1;
if(a[x].pre==x)
break;
x=a[x].pre;
}
count=1;




int ss;
ss=ans;
while(ans==ss)
{
for(i=0; i {
a[i].count=1;
a[i].pre=i;
}
for(i=0; i {
if(mark[i]==1)
continue;
for(j=0; j {
if(mark[j]==1)
continue;
if(a[j].x=a[i].count)
{
a[i].count=a[j].count+1;
a[i].pre=j;
}
}
}
ans=0;
int temp;
for(i=0; i {
if(mark[i]==1)
continue;
if(ans {
ans=a[i].count;
temp=i;
}
}
if(ans==ss)
count++;
int x;
x=temp;
while(1)
{
mark[x]=1;
if(a[x].pre==x)
break;
x=a[x].pre;
}
}
printf("%d\n",ss);
printf("%d\n",count);
}
return 0;
}

#include
#include
#define N 500
struct node
{
int x,pre;
int count;
} a[N];
int mark[N];
int main()
{
int n;
int i,j,ans;
int count;
while(scanf("%d",&n)!=EOF)
{
for(i=0; i {
scanf("%d",&a[i].x);
a[i].pre=i;
a[i].count=1;
mark[i]=0;
}
for(i=0; i {
for(j=0; j {
if(a[j].x=a[i].count)
{
a[i].count=a[j].count+1;
a[i].pre=j;
}
}
}
ans=0;
int temp;
for(i=0; i {
if(ans {
ans=a[i].count;
temp=i;
}
}
int x;
x=temp;
while(1)
{
mark[x]=1;
if(a[x].pre==x)
break;