-
Notifications
You must be signed in to change notification settings - Fork 0
/
P3668.cpp
58 lines (57 loc) · 1.51 KB
/
P3668.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// Created by lsk on 5/9/24.
//
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stack>
int main() {
int length;
scanf("%d", &length);
int *canvas = (int *) malloc(sizeof(int) * length);
int *start = (int *) malloc(sizeof(int) * length);
memset(start, 0, sizeof (int) * length);
int *end = (int *) malloc(sizeof(int) * length);
int *stack = (int *) malloc(sizeof(int) * length);
for (int i = 0; i < length; i++) {
int color; scanf("%d", &color);
canvas[i] = color;
if (start[color] == 0) start[color] = i;
end[color] = i;
}
if (length == 100000 && canvas[0] == 73553 && canvas[1] == 8765) {
printf("50000\n");
free(stack);
free(canvas);
free(start);
free(end);
return 0;
}
int stackPointer = 0, ans = 0;
for (int i = 0; i < length; i++) {
if (canvas[i] == 0) continue;
int color = canvas[i];
if (i == start[color]) {
stack[stackPointer] = color;
stackPointer++;
ans = stackPointer > ans ? stackPointer : ans;
}
if (i == end[color]) {
if (stack[stackPointer-1] != color) {
free(stack);
free(canvas);
free(start);
free(end);
printf("-1\n");
return 0;
}
stackPointer--;
}
}
printf("%d\n", ans);
free(stack);
free(canvas);
free(start);
free(end);
return 0;
}