Submission #146525

#TimeUsernameProblemLanguageResultExecution timeMemory
146525MvCAlternating Current (BOI18_alternating)C++11
100 / 100
84 ms9960 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define all(aaa) aaa.begin(), aaa.end() struct Seg { int l, r, i; }; const int N = 2e5 + 5; vector<Seg> v; int dp[N], w[N], nt[N]; string ans; signed main() { #ifdef HOME freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; v.push_back({ a - 1, b - 1, i }); } int x = -1, mx_len = 0; for (int i = 0; i < m; i++) { int len; if (v[i].l <= v[i].r) len = v[i].r - v[i].l + 1; else len = n - v[i].l + 1 + v[i].r; if (len > mx_len) { mx_len = len; x = i; } } int b = v[x].l, id = v[x].i; for (int i = 0; i < m; i++) { v[i].l = (v[i].l - b + n) % n; v[i].r = (v[i].r - b + n) % n; w[v[i].l]++; w[v[i].r + 1]--; if (v[i].l > v[i].r) { w[0]++; v[i].r += n; } } for (int i = 0; i < n; i++) { w[i + 1] += w[i]; if (w[i] < 2) { cout << "impossible"; return 0; } } for (int i = n - 1, j = n; i >= 0; i--) { if (w[i] < 3) j = i; nt[i] = j; } // for (int i = 0; i < n; i++) { // cout << w[i]; // } // cout << "\n"; sort(all(v), [](Seg a, Seg b) { return a.r < b.r; }); x = 0; while (v[x].i != id) x++; fill(dp, dp + m, -1); dp[x] = -2; set<pair<int, int>> st = { { v[x].r, x } }; for (int i = x + 1; i < m; i++) { auto it = st.lower_bound({ v[i].l - 1, -1 }); if (it != st.end() && it->first == v[i].l - 1) { dp[i] = it->second; } else { it = st.lower_bound({ v[i].l, -1 }); if (it != st.end() && it->first < nt[v[i].l]) { dp[i] = it->second; } } if (dp[i] != -1) { st.insert({ v[i].r, i }); } } int y = -1; for (int i = 0; i < m; i++) { if (dp[i] != -1 && (v[i].r == n - 1 || v[i].r >= n && v[i].r % n < nt[0])) { y = i; break; } } if (y == -1) { cout << "impossible"; return 0; } vector<Seg> part; while (y >= 0) { part.push_back(v[y]); y = dp[y]; } reverse(all(part)); if (part.size() > 2 && part.back().r >= n && part.back().r % n >= part[1].l - 1) { part.erase(part.begin()); } ans.resize(m, '0'); for (Seg s : part) { ans[s.i] = '1'; } cout << ans; return 0; }

Compilation message (stderr)

alternating.cpp: In function 'int main()':
alternating.cpp:117:64: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
             if (dp[i] != -1 && (v[i].r == n - 1 || v[i].r >= n && v[i].r % n < nt[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...