QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#24110 | #2955. Stable Table | George_Plover# | AC ✓ | 6ms | 5564kb | C++20 | 2.2kb | 2022-03-26 15:08:17 | 2022-04-30 04:57:20 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i <= r; i++)
using namespace std;
typedef long long ll;
const int N = 105;
const int M = N * N;
const int inf = 1e8;
int h, w, tot;
int a[N][N];
vector<int> E[M], E2[M];
set<pair<int, int>> edges;
bool bfsed[M];
int dis[M], tmp[M];
void bfs(int s) {
assert(!bfsed[s]);
bfsed[s] = true;
rep(i, 1, tot) { tmp[i] = inf; }
tmp[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : E[u]) {
if (tmp[v] == inf) {
tmp[v] = tmp[u] + 1;
q.push(v);
}
}
}
rep(i, 1, tot) {
// printf("%d ", tmp[i]);
dis[i] += tmp[i];
}
// printf("\n");
}
void bfs2() {
rep(i, 1, tot) { tmp[i] = inf; }
queue<int> q;
tmp[tot] = 0;
q.push(tot);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : E2[u]) {
if (tmp[v] == inf) {
tmp[v] = tmp[u] + 1;
q.push(v);
}
}
}
rep(i, 1, tot) {
// printf("%d ", tmp[i]);
dis[i] += tmp[i];
}
// printf("\n");
}
int main() {
scanf("%d%d", &h, &w);
rep(i, 1, h) {
rep(j, 1, w) { scanf("%d", &a[i][j]); }
}
edges.clear();
rep(i, 1, h - 1) {
rep(j, 1, w) {
int u = a[i][j], v = a[i + 1][j];
if (u != v && !edges.count(make_pair(u, v))) {
edges.insert(make_pair(u, v));
E[u].push_back(v);
E2[v].push_back(u);
}
}
}
tot = h * w + 1;
rep(j, 1, w) {
int u = a[h][j];
if (!edges.count(make_pair(u, tot))) {
edges.insert(make_pair(u, tot));
E[u].push_back(tot);
E2[tot].push_back(u);
}
}
rep(i, 1, tot) bfsed[i] = false;
rep(j, 1, w) {
int u = a[1][j];
if (!bfsed[u]) bfs(u);
}
bfs2();
int ans = inf;
rep(i, 1, tot) { ans = min(ans, dis[i]); }
printf("%d\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 4284kb
input:
7 8 3 3 3 3 10 10 10 10 2 14 3 7 7 10 4 11 2 14 3 1 1 10 4 11 2 14 8 8 8 8 4 11 9 14 5 5 5 5 4 13 9 14 12 12 12 12 4 13 9 6 6 6 6 6 6 13
output:
5
result:
ok single line: '5'
Test #2:
score: 0
Accepted
time: 3ms
memory: 4356kb
input:
8 8 1 1 1 1 1 1 1 1 2 3 3 4 5 6 6 7 2 2 3 4 5 6 7 7 2 3 3 3 6 6 6 7 2 2 3 8 8 8 6 7 9 2 9 10 10 11 11 12 9 9 9 9 10 11 12 12 13 9 14 14 10 10 15 15
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 3ms
memory: 4280kb
input:
10 10 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 2 3 4 5 6 7 8 9 10 11 2 12 13 5 6 7 7 7 7 2 2 14 15 5 6 6 6 6 7 16 16 14 15 15 15 6 16 16 16 16 16 14 14 14 15 16 16 17 16 18 19 15 15 15 15 20 20 20 20 18 19 20 20 20 20 20 21 21 21 21 19
output:
4
result:
ok single line: '4'
Test #4:
score: 0
Accepted
time: 3ms
memory: 4320kb
input:
10 8 20 20 20 20 16 16 16 16 18 19 1 3 3 21 17 2 18 19 1 4 4 21 17 2 18 19 1 5 5 21 17 2 18 19 6 6 6 6 17 2 18 7 7 7 7 7 7 2 18 8 8 8 8 8 8 8 9 9 9 9 9 9 11 11 14 14 9 9 9 9 12 12 15 15 10 10 10 10 13 13
output:
7
result:
ok single line: '7'
Test #5:
score: 0
Accepted
time: 3ms
memory: 4352kb
input:
10 5 4 4 4 2 2 4 4 4 8 8 15 4 1 1 8 3 4 9 9 9 3 6 6 6 6 3 13 13 12 12 3 10 10 12 12 3 14 14 7 7 11 11 11 7 7 5 5 5 7 7
output:
7
result:
ok single line: '7'
Test #6:
score: 0
Accepted
time: 3ms
memory: 4236kb
input:
10 5 4 4 4 2 2 4 4 4 8 8 15 4 1 1 8 3 4 9 9 9 3 4 6 6 6 3 13 13 12 12 3 10 10 12 12 3 14 14 7 7 11 11 11 7 7 5 5 5 7 7
output:
8
result:
ok single line: '8'
Test #7:
score: 0
Accepted
time: 3ms
memory: 4376kb
input:
10 10 1 1 1 1 1 1 1 1 1 1 2 2 3 1 1 1 1 1 4 4 2 2 5 6 7 1 1 8 9 9 2 2 10 11 12 13 14 8 9 9 15 15 16 17 18 13 14 14 19 19 20 20 16 21 22 23 23 23 24 24 25 26 21 21 27 28 28 29 30 30 31 32 21 33 34 34 35 36 37 38 39 40 21 41 41 42 42 43 43 43 44 40 45 46 47 48 49 50 51 43
output:
6
result:
ok single line: '6'
Test #8:
score: 0
Accepted
time: 4ms
memory: 4732kb
input:
50 50 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 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 59 60 61 62 63 64 65 66 67 68 69 ...
output:
100
result:
ok single line: '100'
Test #9:
score: 0
Accepted
time: 3ms
memory: 5492kb
input:
100 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 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 ...
output:
200
result:
ok single line: '200'
Test #10:
score: 0
Accepted
time: 2ms
memory: 4372kb
input:
8 12 13 13 13 13 13 13 13 13 3 3 3 3 13 13 13 13 13 13 13 13 3 2 2 3 13 13 13 1 1 13 13 13 3 3 3 3 13 13 13 1 1 13 13 13 8 11 11 11 13 13 13 13 13 7 8 8 8 11 11 11 13 13 13 13 13 7 9 9 9 11 11 11 4 4 4 4 6 6 10 10 10 11 12 12 5 5 5 5 6 6 10 12 12 12 12 12
output:
5
result:
ok single line: '5'
Test #11:
score: 0
Accepted
time: 4ms
memory: 4568kb
input:
50 50 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 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 59 60 61 62 63 64 65 66 67 68 69 ...
output:
75
result:
ok single line: '75'
Test #12:
score: 0
Accepted
time: 1ms
memory: 4260kb
input:
11 11 3 3 3 3 3 2 2 2 2 2 2 3 5 3 14 3 2 8 2 2 2 6 3 5 3 14 14 2 2 7 2 6 6 13 5 5 14 14 2 7 7 9 10 10 13 5 15 15 14 2 7 1 1 1 1 12 12 15 15 14 2 2 2 2 2 1 12 15 15 15 15 1 1 2 2 16 1 12 12 4 20 20 1 19 1 1 1 1 12 12 4 20 20 1 19 1 18 18 17 11 12 4 4 20 1 19 1 18 18 17 11 11 11 4 4 1 1 1 18 18 17
output:
6
result:
ok single line: '6'
Test #13:
score: 0
Accepted
time: 3ms
memory: 4408kb
input:
25 25 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 5 6 7 7 8 1 1 1 1 1 1 1 9 1 1 1 1 1 1 1 10 10 4 4 4 11 6 7 8 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10 12 13 14 4 15 8 8 8 8 1 1 16 1 1 1 1 1 1 1 17 1 1 18 10 10 12 12 12 19 19 8 8 8 8 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
4
result:
ok single line: '4'
Test #14:
score: 0
Accepted
time: 6ms
memory: 4708kb
input:
100 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 3 1 1 1 1 4 4 5 6 1 1 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 9 10 1 1 1 11 12 12 13 1 1...
output:
17
result:
ok single line: '17'
Test #15:
score: 0
Accepted
time: 0ms
memory: 4236kb
input:
10 10 1 1 1 1 1 1 1 1 1 2 3 4 4 1 5 5 6 1 7 8 9 4 10 10 10 11 11 1 12 13 9 14 10 15 15 16 17 18 18 19 20 14 21 15 22 23 24 25 18 26 14 14 27 27 22 28 29 30 18 31 14 32 27 33 28 28 29 34 34 31 14 27 27 35 36 37 37 38 39 40 41 27 42 42 43 44 38 38 45 40 46 47 42 42 42 48 49 38 50 51
output:
12
result:
ok single line: '12'
Test #16:
score: 0
Accepted
time: 1ms
memory: 4400kb
input:
20 20 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 4 4 5 6 6 7 8 9 9 10 2 11 12 2 13 2 2 14 14 3 15 16 17 18 18 19 8 20 9 10 21 22 22 13 13 23 23 14 24 3 25 25 18 18 18 26 27 20 28 10 21 22 29 13 30 31 32 14 24 3 3 25 18 18 33 34 35 36 28 10 37 38 39 40 30 30 14 14 41 42 43 44 45 46 33 34 47 48 49 50 5...
output:
14
result:
ok single line: '14'
Test #17:
score: 0
Accepted
time: 1ms
memory: 4288kb
input:
8 10 1 1 1 1 1 2 2 2 2 2 21 24 1 1 5 5 6 6 7 8 3 4 1 1 9 9 6 6 17 18 3 4 1 1 10 10 6 6 17 18 22 25 1 1 1 1 6 6 19 20 23 26 11 12 13 14 15 16 19 20 23 26 11 12 13 27 27 16 19 20 23 26 11 12 13 28 28 16 19 20
output:
5
result:
ok single line: '5'
Test #18:
score: 0
Accepted
time: 3ms
memory: 4372kb
input:
12 11 8 8 8 8 8 8 8 8 11 11 11 14 14 8 10 10 10 10 8 2 8 11 14 8 8 8 10 8 8 8 2 8 11 14 8 10 8 10 8 11 8 8 8 11 14 14 10 10 10 6 11 11 11 11 11 5 14 14 10 6 6 6 6 11 6 11 5 5 14 14 14 6 6 11 11 6 6 5 13 13 13 17 17 6 6 6 6 1 5 3 16 17 17 6 6 1 1 1 1 7 3 4 4 4 4 4 4 4 1 15 7 3 3 3 9 9 9 9 4 1 15 12 1...
output:
5
result:
ok single line: '5'
Test #19:
score: 0
Accepted
time: 3ms
memory: 4360kb
input:
12 11 8 8 8 8 8 8 8 8 11 11 11 14 14 8 10 10 10 10 8 2 8 11 14 8 8 8 10 8 8 8 2 8 11 14 8 10 8 10 8 11 8 8 8 11 14 14 10 10 10 6 11 11 11 11 11 5 14 14 10 6 6 6 6 11 6 11 5 5 14 14 14 6 6 11 11 6 6 5 13 13 13 14 14 6 6 6 6 1 5 3 13 14 14 6 6 1 1 1 1 7 3 4 4 4 4 4 4 4 1 15 7 3 3 3 9 9 9 9 4 1 15 12 1...
output:
5
result:
ok single line: '5'
Test #20:
score: 0
Accepted
time: 4ms
memory: 4556kb
input:
100 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ...
output:
101
result:
ok single line: '101'
Test #21:
score: 0
Accepted
time: 3ms
memory: 4352kb
input:
3 4 8 8 8 8 5 6 7 8 1 2 3 4
output:
2
result:
ok single line: '2'
Test #22:
score: 0
Accepted
time: 3ms
memory: 5564kb
input:
100 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 3...
output:
100
result:
ok single line: '100'
Test #23:
score: 0
Accepted
time: 3ms
memory: 5496kb
input:
100 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 3...
output:
99
result:
ok single line: '99'
Test #24:
score: 0
Accepted
time: 2ms
memory: 4228kb
input:
8 3 1 1 1 2 2 3 4 4 3 5 5 3 6 6 3 6 3 3 6 7 7 6 8 8
output:
4
result:
ok single line: '4'
Test #25:
score: 0
Accepted
time: 1ms
memory: 4216kb
input:
8 3 1 1 1 2 2 3 4 4 3 5 5 3 5 3 3 5 6 6 5 7 7 5 8 8
output:
4
result:
ok single line: '4'
Test #26:
score: 0
Accepted
time: 1ms
memory: 4308kb
input:
20 20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 3 1 1 1 1 1 1 4 5 6 1 1 1 7 7 8 1 2 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 7 7 7 7 9 3 10 3 11 11 12 12 12 1 1 1 1 1 1 7 7 7 7 7 13 13 13 13 14 14 14 14 15 14 1 1 1 1 16 7 7 7 7 7 13 13 13 17 14 14 14 14 14 14 18 19 20 20 21 21 22 7 7 7 23 24 24 14 14 14 14...
output:
5
result:
ok single line: '5'
Test #27:
score: 0
Accepted
time: 3ms
memory: 4384kb
input:
18 4 6 6 12 12 13 3 3 5 13 21 21 5 13 4 4 5 13 32 32 5 7 7 14 14 8 15 15 16 8 2 2 16 8 27 27 16 1 1 9 9 17 11 11 10 17 35 35 10 18 19 26 22 20 19 26 25 28 19 26 31 30 19 26 34 33 19 26 24 29 19 26 23
output:
12
result:
ok single line: '12'
Test #28:
score: 0
Accepted
time: 4ms
memory: 4488kb
input:
50 50 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 4 1 1 1 1 1 1 1 2 2 2 2 5 2 6 6 2 2 2 2 2 2 2 7 7 8 2 2 2 9 2 2 2 10 2 2 2 2 2 2 2 2 2 2 11 12 2 2 2 13 13 1 1 1 1 1 1 1 1 2 2 14 2 2 2 15 2 2 16 2 17 17 2 7 7 8 18 18 19 20 20 21 22 10 10 2 2...
output:
7
result:
ok single line: '7'
Test #29:
score: 0
Accepted
time: 2ms
memory: 4644kb
input:
75 75 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 3 4 5 1 1 1 1 1 1 1 1 1 1 1 1 6 6 6 7 8 8 9 9 9 10 10 10 2 11 12 13 14 2 2 2 2 2 15 2 2 2 2 2 2 2 16 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 17 ...
output:
13
result:
ok single line: '13'
Test #30:
score: 0
Accepted
time: 4ms
memory: 4288kb
input:
60 20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 3 1 1 1 1 1 1 1 4 1 1 1 5 1 1 6 1 1 7 7 8 8 9 1 1 1 10 10 4 1 1 1 1 11 11 12 12 1 13 7 8 8 1 1 1 1 1 14 4 1 1 1 1 15 15 16 17 1 13 7 1 1 1 1 1 18 1 1 19 20 21 21 22 22 22 16 17 23 24 24 1 25 26 1 1 1 1 1 1 1 27 27 22 22 22 28 28 28 28 29 30 25 ...
output:
9
result:
ok single line: '9'
Test #31:
score: 0
Accepted
time: 2ms
memory: 4372kb
input:
100 3 1 1 1 1 1 1 2 2 1 3 2 2 3 4 4 5 4 4 5 5 4 6 5 7 8 9 7 8 8 8 8 8 8 10 11 12 10 10 13 10 10 13 10 10 14 10 10 14 10 10 10 10 10 10 10 10 10 10 10 15 10 10 15 10 10 10 16 16 16 16 16 16 16 16 16 16 16 16 16 17 18 19 20 20 19 20 20 19 19 19 21 22 22 21 21 21 21 21 21 21 21 21 23 21 21 24 21 25 26 ...
output:
28
result:
ok single line: '28'
Test #32:
score: 0
Accepted
time: 3ms
memory: 4400kb
input:
10 100 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 4 3 3 5 5 5 6 2 2 2 2 2 7 7 2 8 2 2 2 2 9 2 2 2 2 10 10 2 2 2 11 2 2 12 2 2 2 2 2 2 2 2 2 2...
output:
5
result:
ok single line: '5'
Test #33:
score: 0
Accepted
time: 0ms
memory: 4472kb
input:
30 75 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 3 3 1 1 4 1 5 1 1 1 6 7 1 1 1 1 1 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 9 1 1 10 10 11 1 1 1 1 1 1 1 1 1 12 12 12 13 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
5
result:
ok single line: '5'