Submission #146524

# Submission time Handle Problem Language Result Execution time Memory
146524 2019-08-24T09:59:15 Z MvC Alternating Current (BOI18_alternating) C++11
55 / 100
28 ms 3948 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 < n; 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[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

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 time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 420 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 380 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 424 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 380 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 2 ms 376 KB Output is correct
38 Correct 2 ms 376 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
# 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 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 420 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 380 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 424 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 380 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 2 ms 376 KB Output is correct
38 Correct 2 ms 376 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 2 ms 376 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 376 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 2 ms 376 KB Output is correct
47 Correct 2 ms 376 KB Output is correct
48 Correct 2 ms 376 KB Output is correct
49 Correct 2 ms 376 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 388 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 296 KB Output is correct
54 Correct 2 ms 376 KB Output is correct
55 Correct 2 ms 376 KB Output is correct
56 Correct 2 ms 376 KB Output is correct
57 Correct 2 ms 424 KB Output is correct
58 Correct 2 ms 504 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 2 ms 404 KB Output is correct
61 Correct 2 ms 376 KB Output is correct
62 Correct 2 ms 376 KB Output is correct
63 Correct 2 ms 376 KB Output is correct
64 Correct 2 ms 376 KB Output is correct
65 Correct 2 ms 388 KB Output is correct
66 Correct 2 ms 384 KB Output is correct
67 Correct 2 ms 376 KB Output is correct
68 Correct 2 ms 376 KB Output is correct
69 Correct 2 ms 376 KB Output is correct
# 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 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 420 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 380 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 424 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 380 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 2 ms 376 KB Output is correct
38 Correct 2 ms 376 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 2 ms 376 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 376 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 2 ms 376 KB Output is correct
47 Correct 2 ms 376 KB Output is correct
48 Correct 2 ms 376 KB Output is correct
49 Correct 2 ms 376 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 388 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 296 KB Output is correct
54 Correct 2 ms 376 KB Output is correct
55 Correct 2 ms 376 KB Output is correct
56 Correct 2 ms 376 KB Output is correct
57 Correct 2 ms 424 KB Output is correct
58 Correct 2 ms 504 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 2 ms 404 KB Output is correct
61 Correct 2 ms 376 KB Output is correct
62 Correct 2 ms 376 KB Output is correct
63 Correct 2 ms 376 KB Output is correct
64 Correct 2 ms 376 KB Output is correct
65 Correct 2 ms 388 KB Output is correct
66 Correct 2 ms 384 KB Output is correct
67 Correct 2 ms 376 KB Output is correct
68 Correct 2 ms 376 KB Output is correct
69 Correct 2 ms 376 KB Output is correct
70 Correct 3 ms 588 KB Output is correct
71 Correct 2 ms 376 KB Output is correct
72 Correct 2 ms 376 KB Output is correct
73 Correct 2 ms 380 KB Output is correct
74 Correct 2 ms 376 KB Output is correct
75 Correct 2 ms 376 KB Output is correct
76 Correct 2 ms 376 KB Output is correct
77 Correct 3 ms 380 KB Output is correct
78 Correct 2 ms 384 KB Output is correct
79 Correct 2 ms 376 KB Output is correct
80 Correct 2 ms 504 KB Output is correct
81 Correct 2 ms 376 KB Output is correct
82 Correct 2 ms 376 KB Output is correct
83 Correct 2 ms 376 KB Output is correct
84 Correct 3 ms 432 KB Output is correct
85 Correct 2 ms 376 KB Output is correct
86 Correct 2 ms 376 KB Output is correct
87 Correct 2 ms 376 KB Output is correct
88 Correct 2 ms 480 KB Output is correct
89 Correct 2 ms 376 KB Output is correct
90 Correct 3 ms 376 KB Output is correct
91 Correct 2 ms 376 KB Output is correct
92 Correct 2 ms 376 KB Output is correct
93 Correct 2 ms 380 KB Output is correct
94 Correct 2 ms 376 KB Output is correct
95 Correct 2 ms 376 KB Output is correct
96 Correct 3 ms 504 KB Output is correct
97 Correct 3 ms 376 KB Output is correct
98 Correct 3 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 3948 KB Output is correct
2 Runtime error 3 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 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 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 420 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 380 KB Output is correct
18 Correct 2 ms 348 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 424 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 380 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 2 ms 376 KB Output is correct
38 Correct 2 ms 376 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 2 ms 376 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 376 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 2 ms 376 KB Output is correct
47 Correct 2 ms 376 KB Output is correct
48 Correct 2 ms 376 KB Output is correct
49 Correct 2 ms 376 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 388 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 296 KB Output is correct
54 Correct 2 ms 376 KB Output is correct
55 Correct 2 ms 376 KB Output is correct
56 Correct 2 ms 376 KB Output is correct
57 Correct 2 ms 424 KB Output is correct
58 Correct 2 ms 504 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 2 ms 404 KB Output is correct
61 Correct 2 ms 376 KB Output is correct
62 Correct 2 ms 376 KB Output is correct
63 Correct 2 ms 376 KB Output is correct
64 Correct 2 ms 376 KB Output is correct
65 Correct 2 ms 388 KB Output is correct
66 Correct 2 ms 384 KB Output is correct
67 Correct 2 ms 376 KB Output is correct
68 Correct 2 ms 376 KB Output is correct
69 Correct 2 ms 376 KB Output is correct
70 Correct 3 ms 588 KB Output is correct
71 Correct 2 ms 376 KB Output is correct
72 Correct 2 ms 376 KB Output is correct
73 Correct 2 ms 380 KB Output is correct
74 Correct 2 ms 376 KB Output is correct
75 Correct 2 ms 376 KB Output is correct
76 Correct 2 ms 376 KB Output is correct
77 Correct 3 ms 380 KB Output is correct
78 Correct 2 ms 384 KB Output is correct
79 Correct 2 ms 376 KB Output is correct
80 Correct 2 ms 504 KB Output is correct
81 Correct 2 ms 376 KB Output is correct
82 Correct 2 ms 376 KB Output is correct
83 Correct 2 ms 376 KB Output is correct
84 Correct 3 ms 432 KB Output is correct
85 Correct 2 ms 376 KB Output is correct
86 Correct 2 ms 376 KB Output is correct
87 Correct 2 ms 376 KB Output is correct
88 Correct 2 ms 480 KB Output is correct
89 Correct 2 ms 376 KB Output is correct
90 Correct 3 ms 376 KB Output is correct
91 Correct 2 ms 376 KB Output is correct
92 Correct 2 ms 376 KB Output is correct
93 Correct 2 ms 380 KB Output is correct
94 Correct 2 ms 376 KB Output is correct
95 Correct 2 ms 376 KB Output is correct
96 Correct 3 ms 504 KB Output is correct
97 Correct 3 ms 376 KB Output is correct
98 Correct 3 ms 504 KB Output is correct
99 Correct 28 ms 3948 KB Output is correct
100 Runtime error 3 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
101 Halted 0 ms 0 KB -