#include "gondola.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;
constexpr int max_sub_1 = 2.5e5 + 5;
bool seen_sub_1[max_sub_1] = { 0 };
int valid(int n, int inputSeq[])
{
auto it = min_element(inputSeq, inputSeq + n);
int val = *it, pos = it - inputSeq;
int start_pos = pos;
while (pos != start_pos || val == *it)
{
if (inputSeq[pos] == val)
{
val++, pos++;
if (pos >= n) pos -= n;
val = min(val, n);
continue;
}
if (inputSeq[pos] > n)
{
if (seen_sub_1[inputSeq[pos]]) return 0;
seen_sub_1[inputSeq[pos]] = true;
val++, pos++;
if (pos >= n) pos -= n;
val = min(val, n);
continue;
}
return 0;
}
return 1;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
int min_val, min_pos;
auto min_it = min_element(gondolaSeq, gondolaSeq + n);
min_val = *min_it, min_pos = min_it - gondolaSeq;
vector <pii> numbers;
int val = min_val, pos = min_pos;
if (val > n) val = 1;
int replacement_pos = 0;
bool start = true;
while (pos != min_pos || start)
{
start = false;
if (gondolaSeq[pos] == val)
{
pos++, val++;
if (pos >= n) pos -= n;
if (val > n) val -= n;
continue;
}
if (gondolaSeq[pos] > n)
{
numbers.emplace_back(pii(gondolaSeq[pos], val));
pos++, val++;
if (pos >= n) pos -= n;
if (val > n) val -= n;
continue;
}
abort();
}
if (numbers.empty()) return 0;
sort(numbers.begin(), numbers.end());
int last_val = n;
for (const pii &p : numbers)
{
replacementSeq[replacement_pos++] = p.second;
last_val++;
while (last_val < p.first) replacementSeq[replacement_pos++] = last_val++;
}
return replacement_pos;
}
constexpr ll mod = 1e9 + 7, max_exp = 1e5 + 5, bits_max_exp = 31 - __builtin_clz(max_exp);
void modf(ll &num)
{
if (num >= mod) num %= mod;
}
ll binary_exp(ll base, ll exp)
{
ll output = 1;
for (int i = 0; i < bits_max_exp; i++)
{
if (exp & (1 << i))
{
output *= base;
modf(output);
}
base *= base;
modf(base);
}
return output;
}
int countReplacement(int n, int inputSeq[])
{
int validity = valid(n, inputSeq);
if (validity == 0) return 0;
int max_el = *max_element(inputSeq, inputSeq + n);
if (max_el == n) return 1;
int min_val, min_pos;
auto min_it = min_element(inputSeq, inputSeq + n);
min_val = *min_it, min_pos = min_it - inputSeq;
vector <ll> numbers;
int val = min_val, pos = min_pos;
if (val > n) val = 1;
int replacement_pos = 0;
bool start = true;
while (pos != min_pos || start)
{
start = false;
if (inputSeq[pos] == val)
{
pos++, val++;
if (pos >= n) pos -= n;
if (val > n) val -= n;
continue;
}
if (inputSeq[pos] > n)
{
numbers.emplace_back(inputSeq[pos]);
pos++, val++;
if (pos >= n) pos -= n;
if (val > n) val -= n;
continue;
}
abort();
}
if (numbers.empty()) return 0;
sort(numbers.begin(), numbers.end(), greater<ll>());
ll last_val = n, next_val;
ll output = 1;
for (size_t i = numbers.size(); i > 0; i--)
{
next_val = numbers.back();
numbers.pop_back();
output *= binary_exp(i, next_val - last_val - 1);
modf(output);
last_val = next_val;
}
return (int)output;
}
Compilation message
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:146:9: warning: unused variable 'replacement_pos' [-Wunused-variable]
146 | int replacement_pos = 0;
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
5 ms |
596 KB |
Output is correct |
7 |
Correct |
7 ms |
1060 KB |
Output is correct |
8 |
Correct |
10 ms |
980 KB |
Output is correct |
9 |
Correct |
3 ms |
448 KB |
Output is correct |
10 |
Correct |
8 ms |
1004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
552 KB |
Output is correct |
7 |
Correct |
9 ms |
1108 KB |
Output is correct |
8 |
Correct |
7 ms |
980 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
8 ms |
1048 KB |
Output is correct |
11 |
Correct |
1 ms |
312 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
5 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
9 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
11 ms |
968 KB |
Output is correct |
12 |
Correct |
9 ms |
1068 KB |
Output is correct |
13 |
Correct |
13 ms |
1452 KB |
Output is correct |
14 |
Correct |
8 ms |
1008 KB |
Output is correct |
15 |
Correct |
18 ms |
2368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
312 KB |
Output is correct |
7 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |