Submission #745324

# Submission time Handle Problem Language Result Execution time Memory
745324 2023-05-19T21:12:38 Z rainboy Exam (eJOI20_exam) C
77 / 100
113 ms 98244 KB
#include <stdio.h>

#define N	100000
#define N_	5000

int max(int a, int b) { return a > b ? a : b; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (ii[j] == i_)
				j++;
			else if (ii[j] < i_) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}

int ft[N];

void update(int i, int n, int x) {
	while (i < n) {
		ft[i] = max(ft[i], x);
		i |= i + 1;
	}
}

int query(int i) {
	int x = 0;

	while (i >= 0) {
		x = max(x, ft[i]);
		i &= i + 1, i--;
	}
	return x;
}

int main() {
	static int aa[N], bb[N], qu[N], pp[N], qq[N], ii[N], dp[N_][N_];
	int n, cnt, i, j, b, k, lower, upper, same, good;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (i = 0; i < n; i++)
		scanf("%d", &bb[i]);
	same = 1;
	for (i = 1; i < n; i++)
		if (bb[i] != bb[i - 1]) {
			same = 0;
			break;
		}
	if (same) {
		b = bb[0], k = 0;
		for (i = 0; i < n; i++)
			if (aa[i] <= b) {
				j = i;
				good = 0;
				while (j < n && aa[j] <= b)
					if (aa[j++] == b)
						good = 1;
				if (good)
					k += j - i;
				i = j;
			}
		printf("%d\n", k);
		return 0;
	}
	cnt = 0;
	for (i = 0; i < n; i++) {
		while (cnt && aa[qu[cnt - 1]] <= aa[i])
			cnt--;
		pp[i] = cnt ? qu[cnt - 1] : -1;
		qu[cnt++] = i;
	}
	cnt = 0;
	for (i = n - 1; i >= 0; i--) {
		while (cnt && aa[qu[cnt - 1]] <= aa[i])
			cnt--;
		qq[i] = cnt ? qu[cnt - 1] : n;
		qu[cnt++] = i;
	}
	if (n <= N_) {
		for (i = 0; i < n; i++)
			for (j = 0; j < n; j++)
				dp[i][j] = max(i == 0 ? 0 : dp[i - 1][j], (j == 0 ? 0 : dp[i][j - 1]) + (aa[i] == bb[j] && pp[i] < j && j < qq[i] ? 1 : 0));
		printf("%d\n", dp[n - 1][n - 1]);
	} else {
		for (i = 0; i < n; i++)
			ii[i] = i;
		sort(ii, 0, n);
		for (j = 0; j < n; j++) {
			lower = -1, upper = n;
			while (upper - lower > 1) {
				i = (lower + upper) / 2;
				if (aa[ii[i]] <= bb[j])
					lower = i;
				else
					upper = i;
			}
			if (lower >= 0 && aa[i = ii[lower]] == bb[j] && pp[i] < j && j < qq[i])
				update(i, n, query(i) + 1);
		}
		printf("%d\n", query(n - 1));
	}
	return 0;
}

Compilation message

exam.c: In function 'main':
exam.c:56:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
exam.c:58:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
exam.c:60:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |   scanf("%d", &bb[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 5 ms 340 KB Output is correct
3 Correct 17 ms 980 KB Output is correct
4 Correct 16 ms 980 KB Output is correct
5 Correct 25 ms 980 KB Output is correct
6 Correct 16 ms 1064 KB Output is correct
7 Correct 19 ms 952 KB Output is correct
8 Correct 24 ms 956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 2 ms 3284 KB Output is correct
3 Correct 16 ms 23892 KB Output is correct
4 Correct 62 ms 94320 KB Output is correct
5 Correct 67 ms 98244 KB Output is correct
6 Correct 65 ms 98232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 468 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 1236 KB Output is correct
11 Correct 1 ms 1236 KB Output is correct
12 Correct 1 ms 1236 KB Output is correct
13 Correct 1 ms 1236 KB Output is correct
14 Correct 1 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 2 ms 3284 KB Output is correct
9 Correct 16 ms 23892 KB Output is correct
10 Correct 62 ms 94320 KB Output is correct
11 Correct 67 ms 98244 KB Output is correct
12 Correct 65 ms 98232 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 1236 KB Output is correct
17 Correct 1 ms 1236 KB Output is correct
18 Correct 1 ms 1236 KB Output is correct
19 Correct 1 ms 1236 KB Output is correct
20 Correct 1 ms 1236 KB Output is correct
21 Correct 1 ms 1236 KB Output is correct
22 Correct 4 ms 8276 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 113 ms 98136 KB Output is correct
25 Correct 74 ms 98120 KB Output is correct
26 Correct 66 ms 98116 KB Output is correct
27 Correct 70 ms 98140 KB Output is correct
28 Correct 68 ms 98140 KB Output is correct
29 Correct 66 ms 98124 KB Output is correct
30 Correct 71 ms 98144 KB Output is correct
31 Correct 68 ms 98164 KB Output is correct
32 Correct 75 ms 98172 KB Output is correct