#include "gondola.h"
#include <bits/stdc++.h>
#define MOD 1000000009LL
long long powmod(int a,int b) {
if (b == 0)return 1;
if (b == 1)return a;
long long lilhelp = powmod(a,b/2);
long long ret = (lilhelp * lilhelp) % MOD;
if (b % 2 == 1) ret = (ret * a) % MOD;
return ret;
}
int valid(int n, int inputSeq[])
{
std::set<int>st;
int ls = -1,in;
for (int i = 0;i<n;i++) {
st.insert(inputSeq[i]);
if (inputSeq[i] <= n) {
if (ls == -1) {
in = i;
ls = inputSeq[i];
continue;
}
if (inputSeq[i] < ls) {
if (inputSeq[i] != 1)return 0;
else if (ls - inputSeq[i] != n - i + in)return 0;
}
else {
if (inputSeq[i] - ls != i - in)return 0;
}
in = i;
ls = inputSeq[i];
}
}
if (st.size() != n)return 0;
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
int in[n],le = 0,idx = -1 , sz = 0;
std::vector< std::pair<int,int> >v;
for (int i = 0;i<n;i++) in[i] = i + 1;
for (int i = 0;i<n;i++) {
if (gondolaSeq[i] <= n) {
idx = i;
in[i] = gondolaSeq[i];
}
else {
v.push_back(std::make_pair(gondolaSeq[i],i));
sz++;
}
}
std::sort(v.begin(),v.end());
if (idx != -1) {
int l = idx - 1,r = idx + 1;
while (l >= 0) {
if (in[l + 1] == 1)in[l] = n;
else in[l] = in[l + 1] - 1;
l--;
}
while (r < n) {
if (in[r - 1] == n)in[r] = 1;
else in[r] = in[r - 1] + 1;
r++;
}
}
for (int i = 0;i<sz;i++) {
if (i == 0) {
replacementSeq[le++] = in[v[i].second];
int k = n + 1;
while (k < v[i].first) {
replacementSeq[le++] = k;
k++;
}
}
else {
replacementSeq[le++] = in[v[i].second];
int k = v[i - 1].first + 1;
while (k < v[i].first) {
replacementSeq[le++] = k;
k++;
}
}
/*std::cout<<i<<" "<<le<<std::endl;
for (int j = 0;j<le;j++) std::cout<<replacementSeq[j]<<" ";
std::cout<<std::endl;*/
}
return le;
}
//----------------------
int countReplacement(int n, int a[]) {
if(!valid(n, a)) return 0;
long long ans = 1;
std::vector<int> v;
for(int i = 0; i < n; i++)
if(a[i] > n) v.push_back(a[i]);
std::sort(v.begin(), v.end());
for(int i = 0; i < v.size(); i++) {
int rem = v.size() - i;
int tot = v[i] - (i ? v[i - 1] + 1 : n + 1);
ans *= powmod(rem, tot);
ans %= MOD;
}
if(v.size() == n)
ans = ans * n % MOD;
return ans;
}
Compilation message
gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:37:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (st.size() != n)return 0;
~~~~~~~~~~^~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:105:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++) {
~~^~~~~~~~~~
gondola.cpp:111:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(v.size() == n)
~~~~~~~~~^~~~
gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:28:52: warning: 'in' may be used uninitialized in this function [-Wmaybe-uninitialized]
else if (ls - inputSeq[i] != n - i + in)return 0;
~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
416 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
544 KB |
Output is correct |
2 |
Correct |
3 ms |
544 KB |
Output is correct |
3 |
Correct |
3 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
544 KB |
Output is correct |
5 |
Correct |
2 ms |
544 KB |
Output is correct |
6 |
Correct |
19 ms |
2464 KB |
Output is correct |
7 |
Correct |
23 ms |
2464 KB |
Output is correct |
8 |
Correct |
34 ms |
4176 KB |
Output is correct |
9 |
Correct |
11 ms |
4176 KB |
Output is correct |
10 |
Correct |
38 ms |
4844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4844 KB |
Output is correct |
2 |
Correct |
2 ms |
4844 KB |
Output is correct |
3 |
Correct |
3 ms |
4844 KB |
Output is correct |
4 |
Correct |
2 ms |
4844 KB |
Output is correct |
5 |
Correct |
3 ms |
4844 KB |
Output is correct |
6 |
Correct |
19 ms |
4844 KB |
Output is correct |
7 |
Correct |
15 ms |
4844 KB |
Output is correct |
8 |
Correct |
33 ms |
4844 KB |
Output is correct |
9 |
Correct |
12 ms |
4844 KB |
Output is correct |
10 |
Correct |
42 ms |
4844 KB |
Output is correct |
11 |
Correct |
3 ms |
4844 KB |
Output is correct |
12 |
Correct |
2 ms |
4844 KB |
Output is correct |
13 |
Correct |
9 ms |
4844 KB |
Output is correct |
14 |
Correct |
2 ms |
4844 KB |
Output is correct |
15 |
Correct |
18 ms |
4844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4844 KB |
Output is correct |
2 |
Correct |
3 ms |
4844 KB |
Output is correct |
3 |
Correct |
2 ms |
4844 KB |
Output is correct |
4 |
Correct |
3 ms |
4844 KB |
Output is correct |
5 |
Correct |
3 ms |
4844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4844 KB |
Output is correct |
2 |
Correct |
3 ms |
4844 KB |
Output is correct |
3 |
Correct |
3 ms |
4844 KB |
Output is correct |
4 |
Correct |
3 ms |
4844 KB |
Output is correct |
5 |
Correct |
2 ms |
4844 KB |
Output is correct |
6 |
Correct |
2 ms |
4844 KB |
Output is correct |
7 |
Correct |
3 ms |
4844 KB |
Output is correct |
8 |
Correct |
3 ms |
4844 KB |
Output is correct |
9 |
Correct |
3 ms |
4844 KB |
Output is correct |
10 |
Correct |
3 ms |
4844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4844 KB |
Output is correct |
2 |
Correct |
3 ms |
4844 KB |
Output is correct |
3 |
Correct |
3 ms |
4844 KB |
Output is correct |
4 |
Correct |
3 ms |
4844 KB |
Output is correct |
5 |
Correct |
3 ms |
4844 KB |
Output is correct |
6 |
Correct |
2 ms |
4844 KB |
Output is correct |
7 |
Correct |
2 ms |
4844 KB |
Output is correct |
8 |
Correct |
3 ms |
4844 KB |
Output is correct |
9 |
Correct |
3 ms |
4844 KB |
Output is correct |
10 |
Correct |
3 ms |
4844 KB |
Output is correct |
11 |
Correct |
12 ms |
4844 KB |
Output is correct |
12 |
Correct |
15 ms |
4844 KB |
Output is correct |
13 |
Correct |
22 ms |
4844 KB |
Output is correct |
14 |
Correct |
13 ms |
4844 KB |
Output is correct |
15 |
Correct |
23 ms |
4844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4844 KB |
Output is correct |
2 |
Correct |
2 ms |
4844 KB |
Output is correct |
3 |
Correct |
3 ms |
4844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4844 KB |
Output is correct |
2 |
Correct |
2 ms |
4844 KB |
Output is correct |
3 |
Correct |
2 ms |
4844 KB |
Output is correct |
4 |
Correct |
2 ms |
4844 KB |
Output is correct |
5 |
Incorrect |
2 ms |
4844 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4844 KB |
Output is correct |
2 |
Correct |
2 ms |
4844 KB |
Output is correct |
3 |
Correct |
2 ms |
4844 KB |
Output is correct |
4 |
Correct |
2 ms |
4844 KB |
Output is correct |
5 |
Incorrect |
3 ms |
4844 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4844 KB |
Output is correct |
2 |
Correct |
3 ms |
4844 KB |
Output is correct |
3 |
Correct |
3 ms |
4844 KB |
Output is correct |
4 |
Correct |
2 ms |
4844 KB |
Output is correct |
5 |
Incorrect |
2 ms |
4844 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |