Mai intai trebuie sa te autentifici.

Cod sursa(job #203273)

Utilizator nusmaibunkeleviprofesor cicalescu nusmaibunkelevi Data 14 august 2008 23:29:50
Problema Subsir 2 Scor 33
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.87 kb
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#define NMAX 5000
#define MMAX 60000

struct el{int p,v;};
int n,a[NMAX+1];
int m[NMAX+1],M[NMAX+1],l[NMAX+1];
el e[NMAX+1];

int main(){
freopen("subsir2.in","r",stdin);
freopen("subsir2.out","w",stdout);
int i,j,lmin,ua,max,min,sol[NMAX+1],k;
int first,vfirst,pmin,pmax;
el t;
char sir[MMAX],*p;
scanf("%d\n",&n);
//for(i=1;i<=n;++i) scanf("%d",&a[i]);
fgets(sir,MMAX,stdin);p=sir;
for(i=1;i<=n;++i){
	a[i]=atoi(p);
	while(*p!=32) ++p;
	++p;
	}
min=max=a[1];pmin=pmax=1;
for(i=2;i<=n;++i){
	if(a[i]<min) min=a[i],pmin=i;
	if(a[i]>=max) max=a[i],pmax=i;
	}

if(min==max) {
	lmin=n;first=1;
	for(i=1;i<=n;++i) sol[i]=i;
	goto finish;
	}
if(pmin==n) {lmin=1;first=n;sol[1]=n; goto finish;}
if(pmax==1) {lmin=1;first=1;sol[1]=1; goto finish;}

int ales;
M[n]=1;
for(i=n-1;i;--i){
	lmin=0;ua=a[i];ales=0;
	for(j=n;j>i;--j)
		if(a[i]<=a[j]){
			if(!ales){
				lmin=M[j];ua=a[j];
				ales=1;
				}
			else{
				if(a[j]<=ua){
					lmin=M[j];ua=a[j];
					}
				else
					if(lmin>M[j]){
						lmin=M[j];ua=a[j];
						}
				}
			}
	M[i]=lmin+1;
	}
m[1]=1;
for(i=2;i<=n;++i){
	lmin=0;ua=a[i];ales=0;
	for(j=1;j<i;++j)
		if(a[i]>=a[j]){
			if(!ales){
				lmin=m[j];ua=a[j];
				ales=1;
				}
			else{
				if(a[j]>=ua){
					lmin=m[j];ua=a[j];
					}
				else
					if(lmin>m[j]){
						lmin=m[j];ua=a[j];
						}
				}
			}
	m[i]=lmin+1;
	}
lmin=n+1;
for(i=1;i<=n;++i) {
	l[i]=m[i]+M[i]-1;
	if(lmin>l[i]) lmin=l[i];
	}
k=0;
for(i=1;i<=n;++i)
	if(lmin==l[i]) {k++;e[k].p=i;e[k].v=a[i];}
if(k==lmin) goto gata;
for(i=1;i<k;++i)
	for(j=i+1;j<=k;++j)
		if(e[i].v>e[j].v)
			t=e[i],e[i]=e[j],e[j]=t;
gata:
printf("%d\n",lmin);
for(i=1;i<=lmin;++i)
	printf("%d ",e[i].p);

return 0;
finish:
printf("%d\n",lmin);
for(i=1;i<=lmin;++i)
	printf("%d ",sol[i]);
return 0;
}