Submission #140838

# Submission time Handle Problem Language Result Execution time Memory
140838 2019-08-05T14:32:24 Z Minnakhmetov Alternating Current (BOI18_alternating) C++14
0 / 100
28 ms 3760 KB
#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 = m - 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[j] < 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) {
            y = i;
            break;
        }
    }

    // cout << v[y].i << "\n";
    // cout << v[1].i << "\n";
    // return 0;

    if (y == -1) {
        cout << "impossible";
        return 0;
    }

    vector<int> part;
    while (y >= 0) {
        part.push_back(y);
        y = dp[y];
    }

    reverse(all(part));

    if (part.size() > 2 && v[part.back()].r >= n && 
        v[part.back()].r % n >= v[part[1]].l - 1) {
        part.erase(part.begin());
    }

    ans.resize(m, '0');

    for (int i : part) {
        ans[i] = '1';
    }

    cout << ans;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Incorrect 2 ms 248 KB no wires in direction 0 between segments 11 and 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Incorrect 2 ms 248 KB no wires in direction 0 between segments 11 and 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Incorrect 2 ms 248 KB no wires in direction 0 between segments 11 and 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 3760 KB Output is correct
2 Correct 3 ms 1144 KB Output is correct
3 Correct 15 ms 2032 KB Output is correct
4 Incorrect 23 ms 2672 KB no wires in direction 1 between segments 2 and 100000
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Incorrect 2 ms 248 KB no wires in direction 0 between segments 11 and 11
4 Halted 0 ms 0 KB -