/*
Code written by Talant I.D.
*/
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define int long long
//typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
inline bool isvowel(char ch){
ch = tolower(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
inline bool isprime(int n){
if(n < 2 || (n%2 == 0 && n != 2)) return false;
for(int i = 3; i*i <= n; i++)
if(n%i == 0) return false;
return true;
}
class Union{
private:
vector <int> saizu, link;
public:
Union(int n){
saizu.assign(n, 1); link.resize(n);
iota(all(link), 0);
}
int find(int n){
if(link[n] == n) return n;
return link[n] = find(link[n]);
}
int same(int a, int b){
return find(a) == find(b);
}
void unite(int a, int b){
if(same(a, b)) return;
a = find(a); b = find(b);
if(saizu[a] < saizu[b]) swap(a, b);
saizu[a] += saizu[b];
link[b] = a;
}
int getsize(int a){
return saizu[find(a)];
}
};
const int mod = 1e9+7;
ll mode(ll a){
a %= mod;
if(a < 0) a += mod;
return a;
}
ll subt(ll a, ll b){
return mode(mode(a)-mode(b));
}
ll add(ll a, ll b){
return mode(mode(a)+mode(b));
}
ll mult(ll a, ll b){
return mode(mode(a)*mode(b));
}
ll binpow(ll a, ll b){
ll res = 1;
while(b){
if(b&1) res = mult(res, a);
a = mult(a, a);
b >>= 1;
}
return res;
}
int n, m, grid[5][5], grid2[5][5], grid3[5][5];
inline int check(int i1, int i2, int i3, int i4, int i5, int j1, int j2, int j3, int j4, int j5){
int i, j;
for(i = 0; i < n; i++)
grid2[(i+i1)-(i+i1 >= n ? n : 0)][0] = grid[i][0];
if(m > 1)
for(i = 0; i < n; i++)
grid2[(i+i2)-(i+i2 >= n ? n : 0)][1] = grid[i][1];
if(m > 2)
for(i = 0; i < n; i++)
grid2[(i+i3)-(i+i3 >= n ? n : 0)][2] = grid[i][2];
if(m > 3)
for(i = 0; i < n; i++)
grid2[(i+i4)-(i+i4 >= n ? n : 0)][3] = grid[i][3];
if(m > 4)
for(i = 0; i < n; i++)
grid2[(i+i5)-(i+i5 >= n ? n : 0)][4] = grid[i][4];
for(j = 0; j < m; j++)
grid3[0][(j+j1)-(j+j1 >= m ? m : 0)] = grid2[0][j];
if(n > 1)
for(j = 0; j < m; j++)
grid3[1][(j+j2)-(j+j2 >= m ? m : 0)] = grid2[1][j];
if(n > 2)
for(j = 0; j < m; j++)
grid3[2][(j+j3)-(j+j3 >= m ? m : 0)] = grid2[2][j];
if(n > 3)
for(j = 0; j < m; j++)
grid3[3][(j+j4)-(j+j4 >= m ? m : 0)] = grid2[3][j];
if(n > 4)
for(j = 0; j < m; j++)
grid3[4][(j+j5)-(j+j5 >= m ? m : 0)] = grid2[4][j];
bool flag1 = 1, flag2 = 1;
for(i = 0; i < n; i++){
bool flag = 0;
for(j = 1; j < m; j++){
if(grid3[i][j] != grid3[i][j-1]){
flag = 1;
break;
}
}
if(flag){
flag1 = 0;
break;
}
}
for(j = 0; j < m; j++){
bool flag = 0;
for(i = 1; i < n; i++){
if(grid3[i][j] != grid3[i-1][j]){
flag = 1;
break;
}
}
if(flag){
flag2 = 0;
break;
}
}
if(flag1 || flag2){
int ansi[5], ansj[5];
for(i = 0; i < 5; i++) ansi[i] = ansj[i] = 0;
ansi[0] = min(i1, n-i1);
if(m > 1) ansi[1] = min(i2, n-i2);
if(m > 2) ansi[2] = min(i3, n-i3);
if(m > 3) ansi[3] = min(i4, n-i4);
if(m > 4) ansi[4] = min(i5, n-i5);
ansj[0] = min(j1, m-j1);
if(n > 1) ansj[1] = min(j2, m-j2);
if(n > 2) ansj[2] = min(j3, m-j3);
if(n > 3) ansj[3] = min(j4, m-j4);
if(n > 4) ansj[4] = min(j5, m-j5);
int sum = 0;
for(i = 0; i < 5; i++){
sum += ansi[i]+ansj[i];
}
return sum;
}
return 100500;
}
main(){
do_not_disturb
int i, j;
cin >> n >> m;
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
cin >> grid[i][j];
}
}
int ans = 100500;
for(int i1 = 0; i1 < n; i1++)
for(int i2 = 0; i2 < n; i2++)
for(int i3 = 0; i3 < n; i3++)
for(int i4 = 0; i4 < n; i4++)
for(int i5 = 0; i5 < n; i5++)
for(int j1 = 0; j1 < m; j1++)
for(int j2 = 0; j2 < m; j2++)
for(int j3 = 0; j3 < m; j3++)
for(int j4 = 0; j4 < m; j4++)
for(int j5 = 0; j5 < m; j5++)
ans = min(ans, check(i1, i2, i3, i4, i5, j1, j2, j3, j4, j5));
cout << ans;
return 0;
}
Compilation message
riddicks.cpp:189:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
189 | main(){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
364 KB |
Output is correct |
7 |
Correct |
12 ms |
364 KB |
Output is correct |
8 |
Correct |
58 ms |
364 KB |
Output is correct |
9 |
Correct |
207 ms |
364 KB |
Output is correct |
10 |
Correct |
231 ms |
492 KB |
Output is correct |
11 |
Correct |
206 ms |
364 KB |
Output is correct |
12 |
Correct |
232 ms |
492 KB |
Output is correct |
13 |
Correct |
833 ms |
492 KB |
Output is correct |
14 |
Correct |
827 ms |
388 KB |
Output is correct |
15 |
Correct |
838 ms |
492 KB |
Output is correct |
16 |
Correct |
825 ms |
492 KB |
Output is correct |
17 |
Correct |
828 ms |
492 KB |
Output is correct |
18 |
Correct |
829 ms |
492 KB |
Output is correct |
19 |
Correct |
828 ms |
492 KB |
Output is correct |
20 |
Correct |
826 ms |
492 KB |
Output is correct |