Cod sursa(job #275254)

Utilizator runnaway90Oprescu Radu Constantin runnaway90 Data 10 martie 2009 12:36:22
Problema NextSeq Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include<stdio.h>
#include<algorithm>
#define M 10006
#define Dim (1<<13)

using namespace std;

int a[M], b[M], c[M], nr[M], n, m, p, i, r, j, q, poz;

char lin[Dim];

inline void cit(int &x){
     x = 0;
     while (lin[poz]<'0' || lin[poz]>'9'){
           poz++;
           if (poz == Dim) fread(lin, 1, Dim, stdin), poz=0;
     }
     while (lin[poz]>='0' && lin[poz]<='9'){
         x = 10*x+lin[poz++]-'0';
         if (poz == Dim) fread(lin, 1, Dim, stdin), poz=0;
     }
}

int main()
{
	freopen("nextseq.in","r",stdin);
	freopen("nextseq.out","w",stdout);
	cit(n); cit(m); cit(p);
	for (i = 1; i <= n; ++i) cit(a[i]);
	sort(a+1, a+n+1);

	for (i = 1; i <= n; ++i) nr[a[i]] = i-1;

	for (i = 1; i <= m; ++i) cit(r), b[i] = nr[r];

	for (i = 1; i <= p; ++i) cit(r), c[i] = nr[r];

	b[0] = m; c[0] = p;

	for(j = 1; ; ++j)
	{
		for (i = b[0]; b[i] == n-1; --i);
		if (i){
			b[i]++;
			for (q = i+1; q <= b[0]; ++q) b[q] = 0;
		}
		else
		{
			b[0]++;
			for (q = 1; q <= b[0]; ++q) b[q] = 0;
		}
		if (b[0] < c[0]);
		else
			if (b[0] > c[0]) break;
			else{
				i = 1;
				while (b[i] == c[i]) ++i;
				if (b[i] >= c[i]) break;
			}
	}
	printf("%d",j-1);

	return 0;
}