File tree 2 files changed +131
-0
lines changed
2 files changed +131
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < queue>
3
+ #include < unordered_map>
4
+ using namespace std ;
5
+
6
+ int main (void ) {
7
+ ios_base::sync_with_stdio (0 );
8
+ cin.tie (0 ); cout.tie (0 );
9
+
10
+ int T, N;
11
+ cin >> T;
12
+ for (int k = 0 ; k < T; k++) {
13
+ int size = 0 ;
14
+ unordered_map <int , int > visited;
15
+ priority_queue <int > MaxHeap;
16
+ priority_queue <int , vector <int >, greater<>> MinHeap;
17
+ cin >> N;
18
+ for (int i = 0 ; i < N; i++) {
19
+ char opt;
20
+ int num;
21
+ cin >> opt >> num;
22
+ if (!size) {
23
+ MaxHeap = priority_queue <int >();
24
+ MinHeap = priority_queue <int , vector <int >, greater<>>();
25
+ }
26
+ if (opt == ' I' ) {
27
+ MaxHeap.push (num);
28
+ MinHeap.push (num);
29
+ size++;
30
+ visited[num]++;
31
+ }
32
+ else {
33
+ if (!size) continue ;
34
+ else if (opt == ' D' && num == 1 ) {
35
+ while (!MaxHeap.empty () && !visited[MaxHeap.top ()]) {
36
+ MaxHeap.pop ();
37
+ }
38
+ visited[MaxHeap.top ()]--;
39
+ MaxHeap.pop ();
40
+ size--;
41
+ }
42
+ else if (opt == ' D' && num == -1 ) {
43
+ while (!MinHeap.empty () && !visited[MinHeap.top ()]) {
44
+ MinHeap.pop ();
45
+ }
46
+ visited[MinHeap.top ()]--;
47
+ MinHeap.pop ();
48
+ size--;
49
+ }
50
+ }
51
+ while (!MaxHeap.empty () && !visited[MaxHeap.top ()]) {
52
+ MaxHeap.pop ();
53
+ }
54
+ while (!MinHeap.empty () && !visited[MinHeap.top ()]) {
55
+ MinHeap.pop ();
56
+ }
57
+ }
58
+ if (size == 0 ) {
59
+ cout << " EMPTY\n " ;
60
+ }
61
+ else {
62
+ cout << MaxHeap.top () << ' ' << MinHeap.top () << ' \n ' ;
63
+ }
64
+ }
65
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < queue>
3
+ #include < vector>
4
+ using namespace std ;
5
+
6
+ typedef struct tomato {
7
+ int x;
8
+ int y;
9
+ };
10
+
11
+ int map[1002 ][1002 ];
12
+ queue <tomato> que;
13
+ int N, M, num, res = 0 ;
14
+
15
+ int dx[4 ] = { 0 , 1 , 0 , -1 };
16
+ int dy[4 ] = { 1 , 0 , -1 , 0 };
17
+
18
+ void bfs () {
19
+ int nx, ny;
20
+ while (!que.empty ()) {
21
+ int x = que.front ().x ;
22
+ int y = que.front ().y ;
23
+ que.pop ();
24
+ for (int i = 0 ; i < 4 ; i++) {
25
+ nx = x + dx[i];
26
+ ny = y + dy[i];
27
+ if (nx < 0 || nx >= N || ny < 0 || ny >= M) {
28
+ continue ;
29
+ }
30
+ if (map[nx][ny]) {
31
+ continue ;
32
+ }
33
+ map[nx][ny] = map[x][y] + 1 ;
34
+ que.push ({ nx, ny });
35
+ }
36
+ }
37
+ }
38
+
39
+ int main (void ) {
40
+ ios_base::sync_with_stdio (0 );
41
+ cin.tie (0 ); cout.tie (0 );
42
+
43
+ cin >> M >> N;
44
+ for (int i = 0 ; i < N; i++) {
45
+ for (int j = 0 ; j < M; j++) {
46
+ cin >> map[i][j];
47
+ if (map[i][j] == 1 ) {
48
+ que.push ({ i, j });
49
+ }
50
+ }
51
+ }
52
+ bfs ();
53
+ int res = 0 ;
54
+ for (int i = 0 ; i < N; i++) {
55
+ for (int j = 0 ; j < M; j++) {
56
+ if (map[i][j] == 0 ) {
57
+ cout << " -1\n " ;
58
+ return 0 ;
59
+ }
60
+ if (res < map[i][j]) {
61
+ res = map[i][j];
62
+ }
63
+ }
64
+ }
65
+ cout << res - 1 ;
66
+ }
You can’t perform that action at this time.
0 commit comments