Submission #395533

# Submission time Handle Problem Language Result Execution time Memory
395533 2021-04-28T12:56:26 Z MrRobot_28 Sateliti (COCI20_satellti) C++17
0 / 110
6 ms 2104 KB
#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define sz(a) (int)a.size()
#define ll long long
#define ld long double
const int mod1 = 1e9 + 7;
const int mod2 = 998244353;
const int N = 1e3 + 100;
int A[N][N];
vector <pair <int, int> > mass;
pair <int, int> hashes1[N][N];
pair <int, int> hashes2[N][N];
pair <int, int> hashes3[N][N];
int Pow1[N], Pow2[N];
int n, m;
bool cmp1(int a, int b)
{
    for(int j = 0; j < m; j++)
    {
        if(A[a][j] != A[b][j])
        {
            return A[a][j] < A[b][j];
        }
    }
    return 0;
}
bool cmp(pair <int, int>  a, pair <int, int>  b)
{
    int i1 = a.X;
    int j1 = a.Y;
    int i2 = b.X;
    int j2 = b.Y;
    for(int i = 0; i < n;i++)
    {
        if(hashes3[(i1 + i) % n][j1] != hashes3[(i2 + i) % n][j2])
        {
            for(int j = 0; j < m; j++)
            {
                int k1 = A[(i1 + i) % n][(j1 + j) % m];
                int k2 = A[(i2 + i) % m][(j2 + j) % m];
                if(k1 != k2)
                {
                    return k1 < k2;
                }
            }
        }
    }
    return 0;
}
signed main()
{
    //ifstream cin("286.txt");
//    ios_base::sync_with_stdio(0);
  //  cin.tie(0);
    //cout.tie(0);
    cin >> n >> m;
    Pow1[0] = Pow2[0] = 1;
    for(int i = 1; i < N; i++)
    {
        Pow1[i] = Pow1[i - 1] * 2 % mod1;
        Pow2[i] = Pow2[i - 1] * 2 % mod2;
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            char t;
            cin >> t;
            if(t == '*')
            {
                A[i][j] = 0;
            }
            else
            {
                A[i][j] = 1;
            }
        }
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            hashes1[i][j].X = hashes1[i][j].Y = A[i][j];
            if(j != 0)
            {
                hashes1[i][j].X = (hashes1[i][j].X + hashes1[i][j - 1].X * 2) % mod1;
                hashes1[i][j].Y = (hashes1[i][j].Y + hashes1[i][j - 1].Y * 2) % mod2;
            }
        }
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = m - 1; j >= 0; j--)
        {
            hashes2[i][j].X = hashes2[i][j].Y = A[i][j] * Pow1[m - j - 1];
            if(j != m - 1)
            {
                hashes2[i][j].X = (hashes2[i][j].X + hashes2[i][j + 1].X) % mod1;
                hashes2[i][j].Y = (hashes2[i][j].Y + hashes2[i][j + 1].Y) % mod2;
            }
        }
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(j == 0)
            {
                hashes3[i][j] = hashes2[i][j];
            }
            else
            {
                hashes3[i][j].X = (hashes1[i][j - 1].X + 1LL * hashes2[i][j].X * Pow1[j]) % mod1;
                hashes3[i][j].Y = (hashes1[i][j - 1].Y + 1LL * hashes2[i][j].Y * Pow2[j]) % mod2;
            }
        }
    }
  //  cout << "A\n";
    vector <int> sorted, sorted1, sorted2;
    vector <int> st1(n), col(n);
    vector <int> cnt(n);
    vector <int> start(n);
    vector <int> col1(n);
    for(int i = 0; i < n; i++)
    {
        sorted.push_back(i);
    }
    sort(sorted.begin(), sorted.end(), cmp1);
    for(int t = m - 1; t >= 0; t--)
    {
        for(auto i : sorted)
        {
            if(A[i][t] == 0)
            {
                sorted1.push_back(i);
            }
            else
            {
                sorted2.push_back(i);
            }
        }
        sorted.clear();
        for(auto i : sorted1)
        {
            sorted.push_back(i);
        }
        for(auto i : sorted2)
        {
            sorted.push_back(i);
        }
        int cl = 0;
        col[sorted[0]] = 0;
        for(int i = 1; i < n; i++)
        {
            if(hashes3[sorted[i]][t] != hashes3[sorted[i - 1]][t])
            {
                cl++;
            }
            col[sorted[i]] = cl;
        }
        sorted2 = sorted;
        int k = 0;
        while((1 << k) <= n)
        {
            for(int i = 0; i < n; i++)
            {
                cnt[i] = start[i] = 0;
            }
            for(int i = 0; i < n; i++)
            {
                cnt[col[i]]++;
            }
            for(int i=  1; i < n; i++)
            {
                start[i] = start[i - 1] + cnt[i - 1];
            }
            for(auto i : sorted)
            {
                int of = (i - (1 << k) + n) % n;
                st1[start[col[of]]++] = of;
            }
            int cl = 0;
            for(int i = 0; i < n; i++)
            {
                sorted[i] = st1[i];
                if(i != 0)
                {
                    int a = sorted[i - 1];
                    int b = sorted[i];
                    if(col[a] != col[b] || col[(a + (1 << k) + n) % n] != col[(b + (1 << k) + n) % n])
                    {
                        cl++;
                    }
                }
                col1[sorted[i]] = cl;
            }
            for(int i = 0; i < n; i++)
            {
                col[i] = col1[i];
            }
            k++;
        }
       // cout << sorted[0] << " " << t << "\n";
        mass.push_back({sorted[0], t});
        sorted = sorted2;
        sorted1.clear();
        sorted2.clear();
    }
    sort(mass.begin(), mass.end(), cmp);
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            int k = A[(i + mass[0].X) % n][(j + mass[0].Y) % m];
            if(k == 0)
            {
                cout << "*";
            }
            else
            {
                cout << ".";
            }
        }
        cout << "\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1100 KB Output is correct
2 Correct 2 ms 1100 KB Output is correct
3 Correct 2 ms 1100 KB Output is correct
4 Correct 2 ms 1100 KB Output is correct
5 Runtime error 6 ms 2104 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1100 KB Output is correct
2 Correct 2 ms 1100 KB Output is correct
3 Correct 2 ms 1100 KB Output is correct
4 Correct 2 ms 1100 KB Output is correct
5 Runtime error 6 ms 2104 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1100 KB Output is correct
2 Correct 2 ms 1100 KB Output is correct
3 Correct 2 ms 1100 KB Output is correct
4 Correct 2 ms 1100 KB Output is correct
5 Runtime error 6 ms 2104 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -