Skip to content

Commit 35a0664

Browse files
Add files via upload
1 parent 1cd0908 commit 35a0664

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# cannot iff there is a subarray [1,0,1]
2+
# assign X to all 1s, and unique numbers to all 0s, the edges are easy
3+
for _ in range(int(input())):
4+
n = int(input())
5+
arr = list(map(int, input().split()))
6+
7+
i = 0
8+
prev = 0
9+
seen1 = False
10+
can = True
11+
for i in range(n-2):
12+
if arr[i] == 1:
13+
if prev == 1 and seen1:
14+
can = False
15+
break
16+
17+
seen1 = True
18+
prev = 0
19+
continue
20+
21+
prev += 1
22+
23+
print("YES" if can else "NO")
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**CF2069B
2+
* For each colour, find out ifs doable in 1 or 2 moves (always doable in 2 moves via checkerboard style approach)
3+
*/
4+
#pragma GCC optimize("Ofast")
5+
#pragma GCC optimize("unroll-loops")
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
9+
#define fast_cin() \
10+
ios_base::sync_with_stdio(false); \
11+
cin.tie(NULL); \
12+
cout.tie(NULL);
13+
14+
int dr[] = {-1, 1, 0, 0};
15+
int dc[] = {0, 0, -1, 1};
16+
17+
int main() {
18+
fast_cin();
19+
int t;
20+
cin >> t;
21+
while (t--) {
22+
int h, w;
23+
cin >> h >> w;
24+
vector<vector<int>> arr(h, vector<int>(w));
25+
for (int i = 0; i < h; ++i) {
26+
for (int j = 0; j < w; ++j) {
27+
cin >> arr[i][j];
28+
}
29+
}
30+
31+
unordered_map<int, bool> d;
32+
33+
for (int r = 0; r < h; ++r) {
34+
for (int c = 0; c < w; ++c) {
35+
int colour = arr[r][c];
36+
if (d[colour]) {
37+
continue;
38+
}
39+
40+
for (int i = 0; i < 4; ++i) {
41+
int nr = r + dr[i];
42+
int nc = c + dc[i];
43+
44+
if (nr >= 0 && nr < h && nc >= 0 && nc < w) {
45+
if (arr[nr][nc] == colour) {
46+
d[colour] = true;
47+
break;
48+
}
49+
}
50+
}
51+
}
52+
}
53+
54+
vector<int> costs;
55+
for (const auto& pair : d) {
56+
costs.push_back(pair.second ? 2 : 1);
57+
}
58+
59+
int total_cost = accumulate(costs.begin(), costs.end(), 0);
60+
int max_cost = *max_element(costs.begin(), costs.end());
61+
cout << total_cost - max_cost << endl;
62+
}
63+
64+
return 0;
65+
}

0 commit comments

Comments
 (0)