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"
/*
ID: awesome35
LANG: C++14
TASK: vans
*/
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include<chrono>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpll;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define pb push_back
#define mp make_pair
#define rsz resize
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define f first
#define s second
#define cont continue
#define endl '\n'
#define ednl '\n'
#define test int testc;cin>>testc;while(testc--)
#define pr(a, b) trav(x,a)cerr << x << b; cerr << endl;
#define message cout << "Hello World" << endl;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; // for every grid problem!!
const ll linf = 4000000000000000000LL;
const ll inf = 1000000009;//998244353
const ld pi = 3.1415926535;
void pv(vi a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vll a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vector<vi>a) {
F0R(i, sz(a)) { cout << i << endl; pv(a[i]); cout << endl; }
}void pv(vector<vll>a) { F0R(i, sz(a)) { cout << i << endl; pv(a[i]); }cout << endl; }void pv(vector<string>a) { trav(x, a)cout << x << endl; cout << endl; }
int valid(int n, int* a)
{
set<int>t;
F0R(i, n)t.insert(a[i]);
if (sz(t) != n)return 0;
int index = 0;
for (; index < n; index++)
{
if (a[index] <= n)break;
}
if (index == n)return 1;
vi b(n);
F0R(i, n)
{
b[(i - index + a[index] - 1 + n) % n] = a[i];
}
F0R(i, n)
{
if (b[i] <= n)
{
if (b[i] - i != 1)return 0;
}
}
return 1;
}
int replacement(int n, int* a, int* ans)
{
int index = 0;
for (; index < n; index++)
{
if (a[index] <= n)break;
}
vi b(n);
F0R(i, n)b[i] = a[i];
if (index != n)
{
F0R(i, n)
{
b[(i - index + a[index] - 1 + n) % n] = a[i];
}
}
F0R(i, n)a[i] = b[i];
vi cur(n);
iota(all(cur), 1);
set<int>next;
map<int, int>last;
F0R(i, n)last.insert({ a[i],i });
F0R(i, n)if (a[i] != i + 1)next.insert(i);
index = 0;// ptr in ans
int t = *max_element(a, a + n);
FOR(i, n + 1, t + 1)
{
if (last.count(i))
{
ans[index++] = cur[last[i]];
next.erase(last[i]);
}
else
{
ans[index++] = cur[*next.begin()];
cur[*next.begin()] = i;
}
}
return t - n;
}
int countReplacement(int n, int* a)
{
if (!valid(n, a))return 0;
int index = 0;
for (; index < n; index++)
{
if (a[index] <= n)break;
}
vi b(n);
F0R(i, n)b[i] = a[i];
if (index != n)
{
F0R(i, n)
{
b[(i - index + a[index] - 1 + n) % n] = a[i];
}
}
F0R(i, n)a[i] = b[i];
vi cur(n);
iota(all(cur), 1);
set<int>next;
map<int, int>last;
F0R(i, n)last.insert({ a[i],i });
F0R(i, n)if (a[i] != i + 1)next.insert(i);
int t = *max_element(a, a + n);
ll ans = 1;
FOR(i, n + 1, t + 1)
{
if (last.count(i))
{
next.erase(last[i]);
}
else
{
ans *= sz(next);
ans %= inf;
}
}
if (index == n)
{
ans *= n;
ans %= inf;
}
return ans;
}
void subtask123()
{
int n = 7;
int a0[] = { 1,2,3,4,5,6,7 };
cout << valid(n, a0) << endl;
n = 6;
int a1[] = { 3,4,5,6,1,2 };
cout << valid(n, a1) << endl;
n = 7;
int a2[] = { 1,5,3,4,2,7,6 };
cout << valid(n, a2) << endl;
n = 4;
int a3[] = { 4,3,2,1 };
cout << valid(n, a3) << endl;
n = 7;
int a4[] = { 1,2,3,4,5,6,5 };
cout << valid(n, a4) << endl;
n = 7;
int a5[] = { 2,3,4,9,6,7,1 };
cout << valid(n, a5) << endl;
n = 5;
int a6[] = { 10,4,3,11,12 };
cout << valid(n, a6) << endl;
}
void subtask456()
{
int n = 3;
int a0[] = { 3,1,4 };
int ans0[] = { 0 };
cout << replacement(n, a0, ans0) << endl;
F0R(i, sizeof(ans0) / 4)cout << ans0[i] << " ";
cout << endl;
n = 5;
int a1[] = { 5,1,2,3,4 };
int ans1[] = { 0 };
cout << replacement(n, a1, ans1) << endl;
F0R(i, sizeof(ans1) / 4)cout << ans1[i] << " ";
cout << endl;
n = 7;
int a2[] = { 2,3,4,9,6,7,1 };
int ans2[] = { 0,0 };
cout << replacement(n, a2, ans2) << endl;
F0R(i, sizeof(ans2) / 4)cout << ans2[i] << " ";
cout << endl;
}
void subtask78910()
{
int n = 4;
int a0[] = { 1,2,7,6 };
cout << countReplacement(n, a0) << endl;
n = 7;
int a1[] = { 2,3,4,12,6,7,1 };
cout << countReplacement(n, a1) << endl;
n = 4;
int a2[] = { 4,7,4,7 };
cout << countReplacement(n, a2) << endl;
n = 2;
int a3[] = { 3,4 };
cout << countReplacement(n, a3) << endl;
}
Compilation message (stderr)
gondola.cpp: In function 'void subtask456()':
gondola.cpp:23:40: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]
23 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
gondola.cpp:24:18: note: in expansion of macro 'FOR'
24 | #define F0R(i,a) FOR(i,0,a)
| ^~~
gondola.cpp:197:2: note: in expansion of macro 'F0R'
197 | F0R(i, sizeof(ans0) / 4)cout << ans0[i] << " ";
| ^~~
gondola.cpp:23:40: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]
23 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
gondola.cpp:24:18: note: in expansion of macro 'FOR'
24 | #define F0R(i,a) FOR(i,0,a)
| ^~~
gondola.cpp:204:2: note: in expansion of macro 'F0R'
204 | F0R(i, sizeof(ans1) / 4)cout << ans1[i] << " ";
| ^~~
gondola.cpp:23:40: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]
23 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
gondola.cpp:24:18: note: in expansion of macro 'FOR'
24 | #define F0R(i,a) FOR(i,0,a)
| ^~~
gondola.cpp:211:2: note: in expansion of macro 'F0R'
211 | F0R(i, sizeof(ans2) / 4)cout << ans2[i] << " ";
| ^~~
# | 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... |