Submission #341106

# Submission time Handle Problem Language Result Execution time Memory
341106 2020-12-29T02:37:18 Z mohamedsobhi777 Gondola (IOI14_gondola) C++14
100 / 100
108 ms 7024 KB
#include <bits/stdc++.h>

const int mod = 1e9 + 9;
using namespace std;
#define f first
#define s second
#include "gondola.h"

int valid(int n, int inputSeq[])
{
       map<int, int> mp;
       for (int i = 0; i < n; ++i)
              if (mp[inputSeq[i]]++)
                     return 0;
       if (*min_element(inputSeq, inputSeq + n) > n)
              return 1;
       for (int i = 0; i < n; ++i)
              inputSeq[i]--;

       auto check = [&](int i) -> bool {
              bool ret1 = 1;
              for (int j = 1; j < n; ++j)
              {
                     int nxt = (i + j) % n;
                     if (inputSeq[nxt] >= n)
                            continue;
                     if (inputSeq[nxt] != (inputSeq[i] + j) % n)
                            ret1 = 0;
              }
              return ret1;
       };
       for (int i = 0; i < n; ++i)
       {
              if (inputSeq[i] < n)
                     return check(i);
       }
}

//----------------------

int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
       for (int i = 0; i < n; ++i)
              gondolaSeq[i]--;
       vector<int> a(n);
       if (*min_element(gondolaSeq, gondolaSeq + n) >= n)
       {
              iota(a.begin(), a.end(), 0);
       }
       else
       {
              for (int i = 0; i < n; ++i)
              {
                     if (gondolaSeq[i] < n)
                     {
                            for (int j = 0; j < n; ++j)
                            {
                                   a[(i + j) % n] = (gondolaSeq[i] + j) % n;
                            }
                            break;
                     }
              }
       }
       vector<pair<int, int>> v;
       for (int i = 0; i < n; ++i)
       {
              v.push_back({gondolaSeq[i], i});
       }
       sort(v.begin(), v.end());
       int l = 0;
       int nxt = n;

       for (int i = 0; i < n; ++i)
       {
              int j = v[i].s;
              while (a[j] < gondolaSeq[j])
              {
                     replacementSeq[l++] = a[j] + 1;
                     a[j] = nxt++;
              }
       }
       return l;
}

//----------------------

int countReplacement(int n, int inputSeq[])
{
       using ll = long long;
       int inpo[n];
       for (int i = 0; i < n; ++i)
              inpo[i] = inputSeq[i];
       if (!valid(n, inpo))
              return 0;
       map<int, int> mp;
       for (int i = 0; i < n; ++i)
       {
              mp[inputSeq[i]]++;
       }
       long long ans = 1;
       int nxt = n + 1;
       ll rem = n;
       for (int i = 0; i < n; ++i)
              if (inputSeq[i] <= n)
                     --rem;
       if (rem == n)
              ans = n;
       auto faspow = [&](ll x, ll y) -> long long {
              ll ret = 1ll;
              while (y)
              {
                     if (y & 1)
                            ret = 1ll * ret * x % mod;
                     x = 1ll * x * x % mod;
                     y >>= 1ll;
              }
              return ret;
       };
       vector<int> nxts;
       for (int i = 0; i < n; ++i)
       {
              if (inputSeq[i] > n)
              {
                     nxts.push_back(inputSeq[i]);
              }
       }
       sort(nxts.begin(), nxts.end());
       int lst = n + 1;
       for (auto u : nxts)
       {
              ans = 1ll * ans * faspow(rem, u - lst) % mod;
              --rem;
              lst = u + 1;
       }
       assert(ans);
       return ans;
}

Compilation message

gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:101:12: warning: unused variable 'nxt' [-Wunused-variable]
  101 |        int nxt = n + 1;
      |            ^~~
gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:11:22: warning: control reaches end of non-void function [-Wreturn-type]
   11 |        map<int, int> mp;
      |                      ^~
# 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 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 14 ms 2412 KB Output is correct
7 Correct 13 ms 1260 KB Output is correct
8 Correct 27 ms 4332 KB Output is correct
9 Correct 8 ms 1644 KB Output is correct
10 Correct 31 ms 5100 KB Output is correct
# 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 14 ms 2412 KB Output is correct
7 Correct 12 ms 1260 KB Output is correct
8 Correct 27 ms 4332 KB Output is correct
9 Correct 8 ms 1772 KB Output is correct
10 Correct 34 ms 4972 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 19 ms 2412 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 46 ms 5228 KB Output is correct
# 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
# 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 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 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 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 15 ms 2148 KB Output is correct
12 Correct 17 ms 2148 KB Output is correct
13 Correct 19 ms 1356 KB Output is correct
14 Correct 15 ms 2148 KB Output is correct
15 Correct 25 ms 2404 KB Output is correct
# 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
# 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 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
# 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 0 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 1 ms 364 KB Output is correct
9 Correct 71 ms 4672 KB Output is correct
10 Correct 56 ms 4076 KB Output is correct
11 Correct 20 ms 1772 KB Output is correct
12 Correct 23 ms 2028 KB Output is correct
13 Correct 5 ms 748 KB Output is correct
# 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 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 1 ms 364 KB Output is correct
9 Correct 73 ms 4844 KB Output is correct
10 Correct 66 ms 4076 KB Output is correct
11 Correct 19 ms 1780 KB Output is correct
12 Correct 29 ms 2028 KB Output is correct
13 Correct 5 ms 748 KB Output is correct
14 Correct 94 ms 6380 KB Output is correct
15 Correct 108 ms 7024 KB Output is correct
16 Correct 14 ms 1644 KB Output is correct
17 Correct 66 ms 4844 KB Output is correct
18 Correct 34 ms 3052 KB Output is correct