// Black lives matter
#include <bits/stdc++.h>
#include "gondola.h"
using namespace std;
typedef long long int ll;
typedef complex<double> point;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
//#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
const int N=25e4+10;
ll mod=1e9+9;
int32_t valid(int32_t n, int32_t input[]){
ll id=-1;
for (int i=0;i<n;i++){
if (input[i]<=n){
id=i;
}
}
ll z=input[id]-id+n;
z%=n;
for (int i=0;i<n;i++){
if (input[i]<=n){
ll f=input[i]-i+n;
f%=n;
if (f!=z) return 0;
}
}
vector <int> a;
for (int i=0;i<n;i++){
a.pb(input[i]);
}
sort(a.begin(),a.end());
for (int i=1;i<a.size();i++){
if (a[i]==a[i-1]) return 0;
}
return 1;
}
ll ja[N];
int32_t WW[N];
ll val[N];
int32_t replacement(int32_t n, int32_t input[], int32_t replacementSeq[]){
vector <int> a;
ll id=-1;
for (int i=0;i<n;i++){
val[i+1]=i+1;
if (input[i]<=n) id=i;
}
if (id!=-1){
val[id+1]=input[id];
ll z=input[id];
// cout << z << endl;
for (int i=(id+1)%n;i!=id;i=(i+1)%n){
z++;
if (z==n+1) z=1;
val[i+1]=z;
}
// for (int i=1;i<=n;i++) cout << val[i] << " ";
// cout << endl;
}
for (int i=0;i<n;i++){
ja[input[i]]=val[i+1];
if (val[i+1]==0) cout << 1/0;
a.pb(input[i]);
}
sort(a.begin(),a.end());
vector <int> ans;
deque <int> b;
ll z=a.back();
while(z>n){
if (a.size() && z==a.back()){
a.pop_back();
b.pb(z);
if (ja[z]==0) cout << 1/0;
// cout << z << endl;
}
else{
ans.pb(z);
ja[z]=ja[b.front()];
b.pop_front();
b.pb(z);
}
z--;
}
sort(b.begin(),b.end());
reverse(b.begin(),b.end());
for (auto u : b){
ans.pb(ja[u]);
}
reverse(ans.begin(),ans.end());
for (int i=0;i<ans.size();i++){
replacementSeq[i]=ans[i];
// if (ans[i]==0) cout << 1/0;
}
return ans.size();
}
ll power(ll n,ll k){
if (k==0) return 1;
if (k%2==1){
ll x=power(n,k/2);
return x*x%mod*n%mod;
}
ll x=power(n,k/2);
return x*x%mod;
}
int32_t countReplacement(int32_t n, int32_t input[]){
ll id=-1;
for (int i=0;i<n;i++){
if (input[i]<=n){
id=i;
}
}
if (id!=-1){
ll z=input[id]-id+n;
z%=n;
for (int i=0;i<n;i++){
if (input[i]<=n){
ll f=input[i]-i+n;
f%=n;
if (f!=z) return -1;
}
}
}
vector <int> a;
for (int i=0;i<n;i++){
a.pb(input[i]);
}
sort(a.begin(),a.end());
for (int i=1;i<a.size();i++){
if (a[i]==a[i-1]) return -1;
}
ll ans=1;
ll z=a.back();
// if (z>N) cout << 1/0;
ll cnt=0;
while(z>n){
if (a.size() && a.back()==z){
a.pop_back();
cnt++;
}
else{
ll last=n;
if (a.size()) last=max(last,a.back());
ans*=power(cnt,z-last);
z=last+1;
ans%=mod;
}
z--;
}
if (id==-1){
ans*=n;
ans%=mod;
}
return ans;
}
/*
int32_t in[N];
int32_t main(){
ll n;
cin >> n;
for (int i=1;i<=n;i++){
cin >> in[i-1];
}
ll z=countReplacement(n,in);
cout << z << endl;
}
*/
/*
int gondolaSequence[100001];
int replacementSequence[250001];
int32_t main()
{
int i, n, tag;
int nr;
assert(scanf("%d", &tag)==1);
assert(scanf("%d", &n)==1);
for(i=0;i< n;i++)
assert( scanf("%d", &gondolaSequence[i]) ==1);
switch (tag){
case 1: case 2: case 3:
printf("%d\n", valid(n, gondolaSequence));
break;
case 4: case 5: case 6:
nr = replacement(n, gondolaSequence, replacementSequence);
printf("%d ", nr);
if (nr > 0)
{
for (i=0; i<nr-1; i++)
printf("%d ", replacementSequence[i]);
printf("%d\n", replacementSequence[nr-1]);
}
else printf("\n");
break;
case 7: case 8: case 9: case 10:
printf("%d\n", countReplacement(n, gondolaSequence));
break;
}
return 0;
}
*/
Compilation message
gondola.cpp: In function 'int32_t valid(int32_t, int32_t*)':
gondola.cpp:40:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i=1;i<a.size();i++){
| ~^~~~~~~~~
gondola.cpp: In function 'int32_t replacement(int32_t, int32_t*, int32_t*)':
gondola.cpp:70:35: warning: division by zero [-Wdiv-by-zero]
70 | if (val[i+1]==0) cout << 1/0;
| ~^~
gondola.cpp:81:36: warning: division by zero [-Wdiv-by-zero]
81 | if (ja[z]==0) cout << 1/0;
| ~^~
gondola.cpp:98:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for (int i=0;i<ans.size();i++){
| ~^~~~~~~~~~~
gondola.cpp: In function 'int32_t countReplacement(int32_t, int32_t*)':
gondola.cpp:136:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | for (int i=1;i<a.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 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 |
10 ms |
1128 KB |
Output is correct |
7 |
Correct |
12 ms |
620 KB |
Output is correct |
8 |
Correct |
14 ms |
1764 KB |
Output is correct |
9 |
Correct |
6 ms |
748 KB |
Output is correct |
10 |
Correct |
20 ms |
1764 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 |
9 ms |
1128 KB |
Output is correct |
7 |
Correct |
12 ms |
620 KB |
Output is correct |
8 |
Correct |
14 ms |
1892 KB |
Output is correct |
9 |
Correct |
6 ms |
748 KB |
Output is correct |
10 |
Correct |
19 ms |
1764 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
7 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
12 ms |
620 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 |
2 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 |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 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 |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
3 ms |
492 KB |
Output is correct |
11 |
Correct |
19 ms |
2916 KB |
Output is correct |
12 |
Correct |
18 ms |
3044 KB |
Output is correct |
13 |
Correct |
20 ms |
3564 KB |
Output is correct |
14 |
Correct |
15 ms |
2916 KB |
Output is correct |
15 |
Correct |
29 ms |
4316 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 |
2 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 |
2 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 |
# |
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 |
22 ms |
1764 KB |
Output is correct |
10 |
Correct |
15 ms |
1276 KB |
Output is correct |
11 |
Correct |
6 ms |
748 KB |
Output is correct |
12 |
Correct |
7 ms |
876 KB |
Output is correct |
13 |
Correct |
2 ms |
492 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 |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
18 ms |
1764 KB |
Output is correct |
10 |
Correct |
15 ms |
1256 KB |
Output is correct |
11 |
Correct |
6 ms |
748 KB |
Output is correct |
12 |
Correct |
8 ms |
876 KB |
Output is correct |
13 |
Correct |
3 ms |
492 KB |
Output is correct |
14 |
Correct |
23 ms |
1764 KB |
Output is correct |
15 |
Correct |
25 ms |
1892 KB |
Output is correct |
16 |
Correct |
6 ms |
748 KB |
Output is correct |
17 |
Correct |
17 ms |
1256 KB |
Output is correct |
18 |
Correct |
10 ms |
1128 KB |
Output is correct |