Pagini recente » Cod sursa (job #284067) | Cod sursa (job #667743) | Cod sursa (job #866413) | Cod sursa (job #1958084) | Cod sursa (job #979853)
Cod sursa(job #979853)
#include<stdio.h>
#include<vector>
#define NMAX 100001
#define LMAX 14000
using namespace std;
vector<int> A[NMAX];
struct PosLen{
vector<int> pos;
};
struct PosLen LP[NMAX];
int LMax, N, PMax;
void solve(int val, int position, int L){
int k, l;
unsigned int j;
while(L >= 1){
for(j = 0; j < LP[L].pos.size(); j++){
l = LP[L].pos[j];
if(val > A[l - 1][L - 1]){
for(k = 0; k < L; k++)
A[position - 1].push_back(A[l - 1][k]);
A[position - 1].push_back(val);
if(L+1 > LMax){
LMax = L+1;
PMax = position - 1;
}
LP[L+1].pos.push_back(position);
return;
}
}
L--;
}
if(L == 0){
A[position - 1].push_back(val);
LP[1].pos.push_back(position);
}
}
void process(FILE *pf){
int x, i;
fscanf(pf, "%d", &N);
for(i = 1; i <= N; i++){
fscanf(pf, "%d", &x);
if(i == 1){
LMax = 1;
PMax = 1;
LP[1].pos.push_back(1); // linia este pos, iar coloana este L
A[0].push_back(x);
}
else{
solve(x, i, LMax);
}
}
}
void print(FILE *pg){
int i;
fprintf(pg, "%d\n", LMax);
for(i = 0; i < LMax; i++)
fprintf(pg, "%d ", A[PMax][i]);
}
int main(){
FILE *pf, *pg;
pf = fopen("scmax.in", "r");
pg = fopen("scmax.out", "w");
process(pf);
print(pg);
fclose(pf);
fclose(pg);
return 0;
}