# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
66474 | Kubalionzzale | Gondola (IOI14_gondola) | C++14 | 183 ms | 10768 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "gondola.h"
#include <iostream>
#include <functional>
#include <algorithm>
#include <set>
long long int mod = 1e9 + 9;
long long int powmod(long long int a, long long int b)
{
long long int res = 1;
a %= mod;
for (; b > 0; b >>= 1)
{
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
}
return res;
}
int valid(int n, int a[])
{
bool visited[250010] = { 0 };
int remember = -1;
for (int i = 0; i < n; ++i)
{
--a[i];
if (visited[a[i]])
return 0;
else
visited[a[i]] = 1;
if (a[i] < n)
{
if (remember == -1)
remember = i;
else
{
int val1 = a[remember];
int val2 = a[i];
if (val1 > val2)
{
if (remember == (i + (val1 - val2)) % n)
continue;
else
return 0;
}
else
{
if (i == (remember + (val2 - val1)) % n)
continue;
else
return 0;
}
}
}
}
return 1;
}
//----------------------
int replacement(int n, int a[], int ans[])
{
int model[100010] = { 0 };
std::set< std::pair<int, int>, std::greater< std::pair<int, int> > > set;
int diff = 0;
bool visited[250010] = { 0 };
int max = 0;
for (int i = 0; i < n; ++i)
{
if (a[i] <= n)
{
--a[i];
diff = (i - a[i] + n) % n;
++a[i];
}
if (a[i] > n)
set.insert(std::make_pair(a[i], i));
visited[a[i]] = true;
if (a[i] > max)
max = a[i];
}
int lastunused = max - 1;
while (visited[lastunused])
{
--lastunused;
}
if (max == n)
return 0;
int cnt = max - n;
int returning = cnt;
--cnt;
while (set.empty() == false)
{
std::pair<int, int> cur = *set.begin();
set.erase(set.begin());
if (lastunused <= n)
{
ans[cnt] = ((cur.second - diff) + n) % n;
++ans[cnt];
--cnt;
}
else
{
ans[cnt] = lastunused;
--cnt;
cur.first = lastunused;
set.insert(cur);
--lastunused;
while (visited[lastunused])
{
--lastunused;
}
}
}
return returning;
}
//----------------------
int countReplacement(int n, int a[])
{
bool visited[250010] = { 0 };
std::set<int> happen;
int remember = -1;
int max = 0;
for (int i = 0; i < n; ++i)
{
--a[i];
if (happen.find(a[i]) != happen.end())
return 0;
else
happen.insert(a[i]);
if (a[i] < n)
{
if (remember == -1)
remember = i;
else
{
int val1 = a[remember];
int val2 = a[i];
if (val1 > val2)
{
if (remember == (i + (val1 - val2)) % n)
continue;
else
return 0;
}
else
{
if (i == (remember + (val2 - val1)) % n)
continue;
else
return 0;
}
}
}
}
std::set<int> set;
long long int multi = 0;
for (int i = 0; i < n; ++i)
{
++a[i];
if (a[i] > max)
max = a[i];
if (a[i] > n)
set.insert(a[i]), ++multi;
}
set.insert(n);
if (max == n)
return 1;
long long int ans = 1LL;
for (auto it = set.begin(); it != set.end(); ++it)
{
auto it2 = it;
++it2;
if (it2 == set.end())
break;
long long int val1 = *it;
long long int val2 = *it2;
if (val2 - val1 == 1)
{
--multi;
}
else
{
ans = (ans * powmod(multi, val2 - val1 - 1)) % mod;
--multi;
}
}
if (remember == -1)
ans *= n;
ans %= mod;
return ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |