/** made by amunduzbaev **/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define fastios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vll vector<ll>
#define vii vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define cnt(a)__builtin_popcount(a)
template<class tmp> bool umin(tmp& a, const tmp b) {return a > b? a = b, true:false;}
template<class tmp> bool umax(tmp& a, const tmp b) {return a < b? a = b, true:false;}
typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int N = 6;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);
int n, m, k, ans = 100500, a[N][N];
bool check(){
bool ok = 1;
for(int i=0;i<n;i++)
for(int j=0;j<m-1;j++)
if(a[i][j] != a[i][j+1]) ok = 0;
if(ok) return 1;
for(int i=0;i<m;i++)
for(int j=0;j<n-1;j++) if(a[j][i] != a[j+1][i]) return 0;
return 1;
}
void rcol(int x){
int tmp = a[0][x];
for(int i=0;i<n-1;i++) a[i][x] = a[i+1][x];
a[n-1][x] = tmp;
return;
}
void rrow(int x){
int tmp = a[x][0];
for(int i=0;i<m-1;i++) a[x][i] = a[x][i+1];
a[x][m-1] = tmp;
}
void row(int x, int cost){
if(x == n) {
if(check()) umin(ans, cost);
return;
}
for(int i=0;i<m;i++){
row(x+1, min(i, m-i) + cost);
rrow(x);
}
}
void col(int x, int cost){
if(x == m){
row(0, cost);
return;
}
for(int i=0;i<n;i++){
col(x+1, cost + min(i, n-i));
rcol(x);
}
}
void solve(){
cin>>n>>m;
for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>a[i][j];
col(0, 0);
cout<<ans<<"\n";
return;
}
int main(){
fastios
int t = 1;
if(t) solve();
else {
cin>>t;
while (t--) solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
18 ms |
364 KB |
Output is correct |
10 |
Correct |
19 ms |
384 KB |
Output is correct |
11 |
Correct |
19 ms |
364 KB |
Output is correct |
12 |
Correct |
19 ms |
364 KB |
Output is correct |
13 |
Correct |
298 ms |
492 KB |
Output is correct |
14 |
Correct |
301 ms |
364 KB |
Output is correct |
15 |
Correct |
318 ms |
364 KB |
Output is correct |
16 |
Correct |
299 ms |
392 KB |
Output is correct |
17 |
Correct |
297 ms |
492 KB |
Output is correct |
18 |
Correct |
295 ms |
364 KB |
Output is correct |
19 |
Correct |
295 ms |
492 KB |
Output is correct |
20 |
Correct |
296 ms |
492 KB |
Output is correct |