Submission #792371

#TimeUsernameProblemLanguageResultExecution timeMemory
792371Nhoksocqt1Doktor (COCI17_doktor)C++17
100 / 100
110 ms34124 KiB
#include<bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f #define sz(x) int((x).size()) #define fi first #define se second typedef long long ll; typedef pair<int, int> ii; template<class X, class Y> inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);} template<class X, class Y> inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);} mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int Random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } const int MAXN = 500005; vector<int> B[MAXN]; int a[MAXN], sum[MAXN], cnt[4 * MAXN], nArr; void brute(void) { int res(0), l(0), r(0); for (int i = 1; i <= nArr; ++i) { for (int j = i; j <= nArr; ++j) { reverse(a + i, a + j + 1); int cnt(0); for (int k = 1; k <= nArr; ++k) cnt += (a[k] == k); reverse(a + i, a + j + 1); if(cnt > res) { res = cnt; l = i, r = j; } } } cout << res << '\n' << a[l] << ' ' << a[r] << '\n'; } void process() { cin >> nArr; int cnto(0); for (int i = 1; i <= nArr; ++i) { cin >> a[i]; //if(i == 1) { for (int i = 1; i <= nArr; ++i) a[i] = i; shuffle(a + 1, a + nArr + 1, rng); for (int i = 1; i <= nArr; ++i) cout << a[i] << " \n"[i == nArr]; } B[max(i, a[i])].push_back(min(i, a[i])); cnto += (a[i] == i); } int maxcnt(cnto), l(1), r(1); for (int i = 1; i <= nArr; ++i) { sum[i] = sum[i - 1]; for (int it = 0; it < sz(B[i]); ++it) { int j(B[i][it]); if(i != j) { ++cnt[i + j]; int midc = ((i - j + 1) & 1) ? (a[j + (i - j + 1) / 2] == (j + (i - j + 1) / 2)) : 0; //cout << j << ' ' << i << ' ' << i + j << ' ' << cnt[i + j] << ' ' << sum[i] - sum[j - 1] - midc << '\n'; if(maxcnt < cnt[i + j] + cnto - sum[i] + sum[j - 1] + midc) { maxcnt = cnt[i + j] + cnto - sum[i] + sum[j - 1] + midc; l = j, r = i; } } else { ++sum[i]; } } } //brute(); //cout << maxcnt << '\n'; cout << a[l] << ' ' << a[r] << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); process(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...