제출 #374841

#제출 시각아이디문제언어결과실행 시간메모리
374841valerikkXylophone (JOI18_xylophone)C++17
47 / 100
79 ms492 KiB
/* doing virtual contest start: 10:00 finish: 14:00 */ #include <bits/stdc++.h> using namespace std; int query(int s, int t); void answer(int i, int a); #ifndef EVAL const int N_ = 5007; int n_; int a_[N_], ans_[N_]; int query(int s, int t) { int mn = n_, mx = 1; for (int i = s; i <= t; ++i) { mn = min(mn, a_[i]); mx = max(mx, a_[i]); } return mx - mn; } void answer(int i, int a) { if (i < 1 || i > n_) { cout << "incorrect index " << i << endl; exit(0); } if (ans_[i] != -1) { cout << i << " used twice" << endl; exit(0); } ans_[i] = a; } void check_ans() { bool corr = 1; for (int i = 1; i <= n_; ++i) corr &= a_[i] == ans_[i]; if (!corr) { cout << "incorrect" << endl; for (int i = 1; i <= n_; ++i) cout << a_[i] << " "; cout << endl; for (int i = 1; i <= n_; ++i) cout << ans_[i] << " "; cout << endl; } else { cout << "correct\n"; } } void init(int n, vector<int> a) { n_ = n; for (int i = 1; i <= n; ++i) { a_[i] = a[i - 1]; ans_[i] = -1; } } #endif void solve(int n) { int l = 1, r = n; while (r - l > 1) { int mid = (l + r) / 2; if (query(mid, n) == n - 1) { l = mid; } else { r = mid; } } int pos1 = l; vector<int> a(n + 1, -1); a[pos1] = 1; if (pos1 < n) a[pos1 + 1] = 1 + query(pos1, pos1 + 1); if (pos1 > 1) a[pos1 - 1] = 1 + query(pos1 - 1, pos1); for (int i = pos1 - 2; i > 0; --i) { int x = a[i + 2], y = a[i + 1]; int d = query(i, i + 1); if (abs(x - y) + d == query(i, i + 2)) { a[i] = y < x ? y - d : y + d; } else { a[i] = y < x ? y + d : y - d; } } for (int i = pos1 + 2; i <= n; ++i) { int x = a[i - 2], y = a[i - 1]; int d = query(i - 1, i); if (abs(x - y) + d == query(i - 2, i)) { a[i] = y < x ? y - d : y + d; } else { a[i] = y < x ? y + d : y - d; } } for (int i = 1; i <= n; ++i) answer(i, a[i]); } #ifndef EVAL int main() { freopen("input.txt", "r", stdin); ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; init(n, a); solve(n); check_ans(); return 0; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...