Cod sursa(job #1713275)

Utilizator GabiTulbaGabi Tulba-Lecu GabiTulba Data 5 iunie 2016 01:25:52
Problema Subsir 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <cstdio>
#include <algorithm>
#define MAX 2000000
#define INF 200000005
using namespace std;
  
char f[MAX];
int Pos=0,size[100005]={},bef[100005]={},N,v[100005]={},P,start,m;
void r(int &nr)
{
    nr=0;
    while(f[Pos]<'0'||f[Pos]>'9')
        Pos++;
    while(f[Pos]>='0'&&f[Pos]<='9')
        nr=nr*10+f[Pos++]-'0';
}
 
int main()
{
    freopen("subsir2.in","r",stdin);
    freopen("subsir2.out","w",stdout);
  
    //fread(f,1,MAX,stdin);
	scanf("%d",&N);
	for(int i=1;i<=N;i++)
		scanf("%d",&v[i]);
    for(int i=N;i>=1;i--)
    {
        size[i]=INF;
		bef[i]=P=-1;
        for(int j=i+1;j<=N;j++)
        {
			if(v[i]<=v[j])
			{
				if(P==-1||v[P]>v[j])P=j;
				if(size[i]>size[P]+1||(size[i]==size[P]+1&&v[P]<v[bef[i]]))
				{
					size[i]=size[P]+1;
					bef[i]=j;
				}
			}	
		}
		if(size[i]==INF)size[i]=1;
	}
	m=v[1],start=1;
	for(int i=2;i<=N;i++)
		if(m>v[i])
		{
			m=v[i];
			if(size[i]<size[start]||(size[i]==size[start]&&v[i]<v[start]))
				start=i;
		}
	printf("%d\n",size[start]);
	while(start!=-1)
	{
		printf("%d ",start);
		start=bef[start];
	}
    return 0;
}