Submission #339361

# Submission time Handle Problem Language Result Execution time Memory
339361 2020-12-25T06:04:56 Z talant117408 Riddick's Cube (IZhO13_riddicks) C++17
0 / 100
2000 ms 492 KB
/*
    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;
 
//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];

int check(int i1, int i2, int i3, int i4, int i5, int j1, int j2, int j3, int j4, int j5){
    for(int i = 0; i < n; i++)
        grid2[(i+i1)%n][0] = grid[i][0];
    if(m > 1)
        for(int i = 0; i < n; i++)
            grid2[(i+i2)%n][1] = grid[i][1];
    if(m > 2)
        for(int i = 0; i < n; i++)
            grid2[(i+i3)%n][2] = grid[i][2];
    if(m > 3)
        for(int i = 0; i < n; i++)
            grid2[(i+i4)%n][3] = grid[i][3];
    if(m > 4)
        for(int i = 0; i < n; i++)
            grid2[(i+i5)%n][4] = grid[i][4];
            
    for(int j = 0; j < m; j++)
        grid3[0][(j+j1)%m] = grid2[0][j];
    if(n > 1)
        for(int j = 0; j < m; j++)
            grid3[1][(j+j2)%m] = grid2[1][j];
    if(n > 2)
        for(int j = 0; j < m; j++)
            grid3[2][(j+j3)%m] = grid2[2][j];
    if(n > 3)
        for(int j = 0; j < m; j++)
            grid3[3][(j+j4)%m] = grid2[3][j];
    if(n > 4)
        for(int j = 0; j < m; j++)
            grid3[4][(j+j5)%m] = grid2[4][j];
    
    int flag1 = 0, flag2 = 0;
    for(int i = 0; i < n; i++){
        int flag = 0;
        for(int j = 1; j < m; j++){
            if(grid3[i][j] != grid3[i][j-1]){
                flag++;
                break;
            }
        }
        if(!flag) flag1++;
    }
    for(int j = 0; j < m; j++){
        int flag = 0;
        for(int i = 1; i < n; i++){
            if(grid3[i][j] != grid3[i-1][j]){
                flag++;
                break;
            }
        }
        if(!flag) flag2++;
    }
    
    if(flag1 == n || flag2 == m){
        int ansi[5], ansj[5];
        for(int 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(int i = 0; i < 5; i++){
            sum += ansi[i]+ansj[i];
        }
        return sum;
    }
    
    return 100500;
}

int main(){
    do_not_disturb
    
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        for(int 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;
}
# 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 12 ms 364 KB Output is correct
5 Correct 14 ms 364 KB Output is correct
6 Correct 40 ms 364 KB Output is correct
7 Correct 38 ms 492 KB Output is correct
8 Correct 215 ms 364 KB Output is correct
9 Correct 808 ms 492 KB Output is correct
10 Correct 844 ms 492 KB Output is correct
11 Correct 840 ms 492 KB Output is correct
12 Correct 845 ms 492 KB Output is correct
13 Execution timed out 2095 ms 364 KB Time limit exceeded
14 Halted 0 ms 0 KB -