QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#28352 | #2955. Stable Table | hld67890# | AC ✓ | 5ms | 4492kb | C++17 | 2.2kb | 2022-04-13 18:56:03 | 2022-04-29 09:39:49 |
Judging History
answer
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int n , m , tot;
int a[120][120];
int top[12000] , bottom[12000];
int tp[4] , ttp;
vector < int > g[12000] , rg[12000];
int dis[4][12000] , rdis[12000];
int ans;
void getdis ( int i ) {
int j , k;
queue < int > q;
for ( j = 1 ; j <= tot + 1 ; j++ ) {
dis[i][j] = -1;
}
dis[i][tp[i]] = 0;
q.push ( tp[i] );
while ( q.size () ) {
k = q.front (); q.pop ();
for ( auto l : g[k] ) {
if ( dis[i][l] == -1 ) {
dis[i][l] = dis[i][k] + 1;
q.push ( l );
}
}
}
}
void getrdis () {
int j , k;
queue < int > q;
for ( j = 1 ; j <= tot + 1 ; j++ ) {
rdis[j] = -1;
}
rdis[tot+1] = 0;
q.push ( tot + 1 );
while ( q.size () ) {
k = q.front (); q.pop ();
for ( auto l : rg[k] ) {
if ( rdis[l] == -1 ) {
rdis[l] = rdis[k] + 1;
q.push ( l );
}
}
}
}
void work () {
int i , j;
scanf ( "%d%d" , &n , &m );
tot = 0;
for ( i = 1 ; i <= n ; i++ ) {
for ( j = 1 ; j <= m ; j++ ) {
scanf ( "%d" , &a[i][j] );
if ( i == 1 ) {
top[a[i][j]] = 1;
}
if ( i == n ) {
bottom[a[i][j]] = 1;
}
tot = max ( tot , a[i][j] );
}
}
for ( i = 1 ; i <= tot ; i++ ) {
if ( top[i] == 1 ) tp[++ttp] = i;
}
for ( i = 1 ; i < n ; i++ ) {
for ( j = 1 ; j <= m ; j++ ) {
if ( a[i][j] != a[i+1][j] ) {
g[a[i][j]].push_back( a[i+1][j] );
rg[a[i+1][j]].push_back( a[i][j] );
}
}
}
for ( i = 1 ; i <= tot ; i++ ) {
if ( bottom[i] == 1 ) {
//printf ( "##%d\n" , i );
g[i].push_back(tot+1);
rg[tot+1].push_back(i);
}
}
for ( i = 1 ; i <= ttp ; i++ ) {
getdis ( i );
}
if ( ttp == 1 ) {
printf ( "%d\n" , dis[1][tot+1] );
return ;
}
getrdis ();
/*for ( i = 1 ; i <= tot + 1 ; i++ ) {
printf ( "%d %d %d\n" , dis[1][i] , dis[2][i] , rdis[i] );
}*/
ans = dis[1][tot+1] + dis[2][tot+1];
for ( i = 1 ; i <= tot ; i++ ) {
if ( dis[1][i] == -1 ) continue;
if ( dis[2][i] == -1 ) continue;
if ( rdis[i] == -1 ) continue;
ans = min ( ans , dis[1][i] + dis[2][i] + rdis[i] );
}
printf ( "%d\n" , ans );
}
int main () {
work ();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3784kb
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: 3788kb
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: 0ms
memory: 3900kb
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: 0ms
memory: 3708kb
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: 3772kb
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: 1ms
memory: 3736kb
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: 3708kb
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: 1ms
memory: 4104kb
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: 1ms
memory: 4484kb
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: 3ms
memory: 3764kb
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: 3964kb
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: 5ms
memory: 3740kb
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: 3796kb
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: 5ms
memory: 3988kb
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: 3ms
memory: 3748kb
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: 3ms
memory: 3768kb
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: 0ms
memory: 3764kb
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: 0ms
memory: 3764kb
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: 3908kb
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: 1ms
memory: 3904kb
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: 1ms
memory: 3728kb
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: 4492kb
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: 2ms
memory: 4416kb
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: 3ms
memory: 3788kb
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: 3ms
memory: 3772kb
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: 3ms
memory: 3756kb
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: 0ms
memory: 3740kb
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: 3796kb
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: 1ms
memory: 4048kb
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: 3796kb
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: 3ms
memory: 3788kb
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: 3776kb
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: 1ms
memory: 3780kb
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'