Pagini recente » Istoria paginii runda/testqwerty | Diferente pentru ciorna intre reviziile 211 si 164 | Cod sursa (job #1507943) | Monitorul de evaluare | Cod sursa (job #764518)
Cod sursa(job #764518)
#include<stdio.h>
#include<algorithm>
#include<assert.h>
using namespace std;
FILE *f = fopen("secv.in","r");
FILE *g = fopen("secv.out","w");
#define MaxN 5010
int N,Nr,Max,Sol,poz;
int A[MaxN];
int Best[MaxN],Poz[MaxN];
void citire(void)
{
fscanf(f,"%d ",&N);
for(int i=1;i<=N;i++)
fscanf(f,"%d ",&A[i]);
}
void NumereUnice(void)
{
int C[MaxN] = {-1};
for(int i=1;i<=N;i++)
C[i] = A[i];
sort(C+1,C+N+1);
for(int i=1;i<=N;i++)
if(C[i] != C[i-1])
++ Nr;
}
void Rezolvare(void)
{
NumereUnice();
for(int i=1;i<=N;i++)
{
Best[i] = 1; Poz[i] = i;
for(int j=i-1;j;j--)
if(A[j] < A[i] && (Best[j]+1 > Best[i] || (Best[j]+1 == Best[i] && Poz[j] > Poz[i])))
Best[i] = Best[j]+1, Poz[i] = Poz[j];
//printf("%d ",Best[i]);
if(Best[Max] < Best[i] || (Best[Max] == Best[i] && Poz[i] > Poz[Max]))
Max = i ,Sol = i-Poz[i]+1;
}
if(Best[Max] != Nr)
Sol = -1;
assert(Sol != -1);
}
int main()
{
citire();
Rezolvare();
fprintf(g,"%d\n",Sol);
}