#include "gondola.h"
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cstring>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#include <numeric>
#include <bitset>
#include <sstream>
#include <chrono>
#include <random>
#define ff first
#define ss second
#define PB push_back
#define sz(x) int(x.size())
#define rsz resize
#define fch(xxx, yyy) for (auto xxx : yyy) // abusive notation
#define all(x) (x).begin(),(x).end()
#define eps 1e-9
// more abusive notation (use at your own risk):
// #define int ll
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
// debugging
void __print(int x) {std::cerr << x;}
void __print(ll x) {std::cerr << x;} /* remember to uncomment this when not using THE MACRO */
void __print(unsigned x) {std::cerr << x;}
void __print(ull x) {std::cerr << x;}
void __print(float x) {std::cerr << x;}
void __print(double x) {std::cerr << x;}
void __print(ld x) {std::cerr << x;}
void __print(char x) {std::cerr << '\'' << x << '\'';}
void __print(const char *x) {std::cerr << '\"' << x << '\"';}
void __print(const string& x) {std::cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V> void __print(const pair<T, V> &x) {std::cerr << '{'; __print(x.ff); std::cerr << ", "; __print(x.ss); std::cerr << '}';}
template<typename T> void __print(const T& x) {int f = 0; std::cerr << '{'; for (auto &i: x) std::cerr << (f++ ? ", " : ""), __print(i); std::cerr << "}";}
void _print() {std::cerr << "]\n";}
template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) std::cerr << ", "; _print(v...);}
void println() {std::cerr << ">--------------------<" << endl;}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// templates
template <class T> bool ckmin(T &a, const T &b) {return b<a ? a = b, 1 : 0;}
template <class T> bool ckmax(T &a, const T &b) {return b>a ? a = b, 1 : 0;}
template <class T> using gr = greater<T>;
template <class T> using vc = vector<T>;
template <class T> using p_q = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vc<T>, gr<T>>;
template <class T1, class T2> using pr = pair<T1, T2>;
mt19937_64 rng_ll(chrono::steady_clock::now().time_since_epoch().count());
int rng(int M) {return (int)(rng_ll()%M);} /*returns any random number in [0, M) */
// const variables
constexpr int INF = (int)2e9;
constexpr int MOD = 1e9 + 9;
constexpr ll LL_INF = (ll)3e18;
constexpr int mod = (int)1e9 + 7;
constexpr ll inverse = 500000004LL; // inverse of 2 modulo 1e9 + 7
void setIO(const string& str) {// fast input/output
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (str.empty()) return;
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
namespace math {
// Binary Exponentiation, with or without modulo
ll binpow(ll a, ll b) {
ll res = 1;
while (b > 0) {
if (b & 1) res *= a;
a *= a;
b >>= 1;
}
return res;
}
ll binpow(ll a, ll b, ll m) {
a%=m;
ll res = 1;
while (b > 0) {
if (b & 1) res = (res * a)%m;
a = (a * a)%m;
b >>= 1;
}
return res;
}
}
void mult(ll& x, ll y) {
y %= MOD;
x = (x * y)%MOD;
}
int valid(int n, int inputSeq[]) {
int N = n;
vi v(N);
int n1 = 0, mn = N;
set<int> st;
for (int i = 0; i < N; ++i) {
v[i] = inputSeq[i] - 1;
st.insert(v[i]);
if (v[i] <= N - 1) {
n1++;
ckmin(mn, v[i]);
}
}
if (sz(st) < N) return 0;
if (n1 == 0) return 1;
int idx = -1;
for (int i = 0; i < N; ++i) {
if (v[i] == mn) idx = i;
}
assert(idx != -1);
idx -= v[idx];
if (idx < 0) idx += N;
rotate(v.begin(), v.begin() + idx, v.end());
for (int i = 0; i < N; ++i) {
if (v[i] <= N - 1 && i != v[i]) {
return 0;
}
}
return 1;
}
int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
int N = n;
vi v(N), order;
int n1 = 0, mn = N;
for (int i = 0; i < N; ++i) {
v[i] = gondolaSeq[i] - 1;
if (v[i] <= N - 1) {
n1++;
ckmin(mn, v[i]);
}
}
vc<pii> pairs;
if (n1 == 0) {
for (int i = 0; i < N; ++i) {
pairs.PB({v[i], i});
}
} else {
int idx = -1;
for (int i = 0; i < N; ++i) {
if (v[i] == mn) idx = i;
}
assert(idx != -1);
idx -= v[idx];
if (idx < 0) idx += N;
rotate(v.begin(), v.begin() + idx, v.end());
for (int i = 0; i < N; ++i) {
if (v[i] >= N) {
pairs.PB({v[i], i});
}
}
}
sort(all(pairs));
int cur_replace = N - 1;
for (int i = 0; i < sz(pairs); ++i) {
order.PB(pairs[i].ss);
cur_replace++;
while (cur_replace < pairs[i].ff) {
order.PB(cur_replace);
cur_replace++;
}
}
for (int i = 0; i < sz(order); ++i) {
replacementSeq[i] = order[i] + 1;
}
return sz(order);
}
int countReplacement(int n, int inputSeq[]) {
if (!valid(n, inputSeq)) {
return 0;
}
int N = n;
vi v(N);
int n1 = 0, mn = N;
for (int i = 0; i < N; ++i) {
v[i] = inputSeq[i] - 1;
if (v[i] <= N - 1) {
n1++;
ckmin(mn, v[i]);
}
}
vc<pii> pairs;
if (n1 == 0) {
for (int i = 0; i < N; ++i) {
pairs.PB({v[i], i});
}
} else {
int idx = -1;
for (int i = 0; i < N; ++i) {
if (v[i] == mn) idx = i;
}
assert(idx != -1);
idx -= v[idx];
if (idx < 0) idx += N;
rotate(v.begin(), v.begin() + idx, v.end());
for (int i = 0; i < N; ++i) {
if (v[i] >= N) {
pairs.PB({v[i], i});
}
}
}
sort(all(pairs));
if (pairs.empty()) {
return 1;
}
vc<pii> sections;
int consec = 1, small = pairs[0].ff;
for (int i = 1; i < sz(pairs); ++i) {
if (pairs[i].ff > pairs[i-1].ff + 1) {
sections.PB({consec, small});
consec = 1;
small = pairs[i].ff;
} else {
consec++;
}
}
if (consec >= 1) sections.PB({consec, small});
int cur = N;
ll prod = 1;
vc<pii> exponen;
for (int i = 0; i < sz(sections); ++i) {
int dist = sections[i].ss - cur;
cur = sections[i].ss + sections[i].ff;
if (dist == 0) continue;
exponen.PB({dist, sections[i].ff});
}
for (int i = sz(exponen)-2; i >= 0; --i) {
exponen[i].ss += exponen[i+1].ss;
}
for (int i = 0; i < sz(exponen); ++i) {
ll term = math::binpow(exponen[i].ss, exponen[i].ff, MOD);
mult(prod, term);
}
if (n1 == 0) mult(prod, N);
return prod;
}
// TLE -> TRY NOT USING DEFINE INT LONG LONG
// CE -> CHECK LINE 45
// 5000 * 5000 size matrices are kinda big (potential mle)
// Do something, start simpler
Compilation message
gondola.cpp: In function 'void setIO(const string&)':
gondola.cpp:89:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen((str + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gondola.cpp:90:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen((str + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
2556 KB |
Output is correct |
7 |
Correct |
20 ms |
4440 KB |
Output is correct |
8 |
Correct |
13 ms |
4700 KB |
Output is correct |
9 |
Correct |
5 ms |
1628 KB |
Output is correct |
10 |
Correct |
17 ms |
5244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
2652 KB |
Output is correct |
7 |
Correct |
19 ms |
4664 KB |
Output is correct |
8 |
Correct |
13 ms |
4700 KB |
Output is correct |
9 |
Correct |
4 ms |
1624 KB |
Output is correct |
10 |
Correct |
16 ms |
5408 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
9 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
25 ms |
5552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
448 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
444 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
1372 KB |
Output is correct |
12 |
Correct |
5 ms |
1624 KB |
Output is correct |
13 |
Correct |
8 ms |
1628 KB |
Output is correct |
14 |
Correct |
4 ms |
1372 KB |
Output is correct |
15 |
Correct |
11 ms |
2360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
444 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
24 ms |
4700 KB |
Output is correct |
10 |
Correct |
19 ms |
3928 KB |
Output is correct |
11 |
Correct |
8 ms |
1664 KB |
Output is correct |
12 |
Correct |
9 ms |
2048 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
26 ms |
4732 KB |
Output is correct |
10 |
Correct |
19 ms |
3928 KB |
Output is correct |
11 |
Correct |
7 ms |
1628 KB |
Output is correct |
12 |
Correct |
9 ms |
1972 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
33 ms |
5700 KB |
Output is correct |
15 |
Correct |
35 ms |
6360 KB |
Output is correct |
16 |
Correct |
7 ms |
1372 KB |
Output is correct |
17 |
Correct |
23 ms |
4424 KB |
Output is correct |
18 |
Correct |
13 ms |
2768 KB |
Output is correct |