Submission #140839

# Submission time Handle Problem Language Result Execution time Memory
140839 2019-08-05T14:44:58 Z Minnakhmetov Alternating Current (BOI18_alternating) C++14
100 / 100
84 ms 10128 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[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:60: 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 348 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 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 324 KB Output is correct
17 Correct 2 ms 424 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 2 ms 388 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 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 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 380 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 424 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 348 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 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 324 KB Output is correct
17 Correct 2 ms 424 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 2 ms 388 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 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 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 380 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 424 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 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 376 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 376 KB Output is correct
58 Correct 2 ms 376 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 2 ms 376 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 428 KB Output is correct
65 Correct 2 ms 376 KB Output is correct
66 Correct 2 ms 376 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 348 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 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 324 KB Output is correct
17 Correct 2 ms 424 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 2 ms 388 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 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 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 380 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 424 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 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 376 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 376 KB Output is correct
58 Correct 2 ms 376 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 2 ms 376 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 428 KB Output is correct
65 Correct 2 ms 376 KB Output is correct
66 Correct 2 ms 376 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 2 ms 376 KB Output is correct
71 Correct 3 ms 504 KB Output is correct
72 Correct 2 ms 376 KB Output is correct
73 Correct 2 ms 376 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 2 ms 376 KB Output is correct
78 Correct 2 ms 376 KB Output is correct
79 Correct 2 ms 376 KB Output is correct
80 Correct 2 ms 380 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 508 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 380 KB Output is correct
89 Correct 2 ms 376 KB Output is correct
90 Correct 2 ms 376 KB Output is correct
91 Correct 2 ms 380 KB Output is correct
92 Correct 2 ms 376 KB Output is correct
93 Correct 2 ms 376 KB Output is correct
94 Correct 2 ms 504 KB Output is correct
95 Correct 2 ms 376 KB Output is correct
96 Correct 2 ms 504 KB Output is correct
97 Correct 2 ms 504 KB Output is correct
98 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 2920 KB Output is correct
2 Correct 3 ms 1144 KB Output is correct
3 Correct 14 ms 1516 KB Output is correct
4 Correct 23 ms 2160 KB Output is correct
5 Correct 84 ms 9676 KB Output is correct
6 Correct 28 ms 3320 KB Output is correct
7 Correct 73 ms 9192 KB Output is correct
8 Correct 5 ms 1400 KB Output is correct
9 Correct 3 ms 1144 KB Output is correct
10 Correct 83 ms 10128 KB Output is correct
11 Correct 60 ms 7912 KB Output is correct
12 Correct 34 ms 3820 KB Output is correct
13 Correct 2 ms 632 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 38 ms 4072 KB Output is correct
16 Correct 15 ms 2032 KB Output is correct
17 Correct 38 ms 4384 KB Output is correct
18 Correct 34 ms 3176 KB Output is correct
19 Correct 4 ms 1016 KB Output is correct
20 Correct 82 ms 9580 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 348 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 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 324 KB Output is correct
17 Correct 2 ms 424 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 2 ms 388 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 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 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 380 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 424 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 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 376 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 376 KB Output is correct
58 Correct 2 ms 376 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 2 ms 376 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 428 KB Output is correct
65 Correct 2 ms 376 KB Output is correct
66 Correct 2 ms 376 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 2 ms 376 KB Output is correct
71 Correct 3 ms 504 KB Output is correct
72 Correct 2 ms 376 KB Output is correct
73 Correct 2 ms 376 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 2 ms 376 KB Output is correct
78 Correct 2 ms 376 KB Output is correct
79 Correct 2 ms 376 KB Output is correct
80 Correct 2 ms 380 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 508 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 380 KB Output is correct
89 Correct 2 ms 376 KB Output is correct
90 Correct 2 ms 376 KB Output is correct
91 Correct 2 ms 380 KB Output is correct
92 Correct 2 ms 376 KB Output is correct
93 Correct 2 ms 376 KB Output is correct
94 Correct 2 ms 504 KB Output is correct
95 Correct 2 ms 376 KB Output is correct
96 Correct 2 ms 504 KB Output is correct
97 Correct 2 ms 504 KB Output is correct
98 Correct 2 ms 504 KB Output is correct
99 Correct 27 ms 2920 KB Output is correct
100 Correct 3 ms 1144 KB Output is correct
101 Correct 14 ms 1516 KB Output is correct
102 Correct 23 ms 2160 KB Output is correct
103 Correct 84 ms 9676 KB Output is correct
104 Correct 28 ms 3320 KB Output is correct
105 Correct 73 ms 9192 KB Output is correct
106 Correct 5 ms 1400 KB Output is correct
107 Correct 3 ms 1144 KB Output is correct
108 Correct 83 ms 10128 KB Output is correct
109 Correct 60 ms 7912 KB Output is correct
110 Correct 34 ms 3820 KB Output is correct
111 Correct 2 ms 632 KB Output is correct
112 Correct 2 ms 504 KB Output is correct
113 Correct 38 ms 4072 KB Output is correct
114 Correct 15 ms 2032 KB Output is correct
115 Correct 38 ms 4384 KB Output is correct
116 Correct 34 ms 3176 KB Output is correct
117 Correct 4 ms 1016 KB Output is correct
118 Correct 82 ms 9580 KB Output is correct
119 Correct 3 ms 1144 KB Output is correct
120 Correct 55 ms 7388 KB Output is correct
121 Correct 36 ms 5092 KB Output is correct
122 Correct 2 ms 376 KB Output is correct
123 Correct 4 ms 1016 KB Output is correct
124 Correct 12 ms 1776 KB Output is correct
125 Correct 23 ms 3572 KB Output is correct
126 Correct 3 ms 1144 KB Output is correct
127 Correct 7 ms 1720 KB Output is correct
128 Correct 64 ms 8428 KB Output is correct
129 Correct 37 ms 5420 KB Output is correct
130 Correct 45 ms 6128 KB Output is correct
131 Correct 4 ms 1144 KB Output is correct
132 Correct 3 ms 1144 KB Output is correct
133 Correct 56 ms 6536 KB Output is correct
134 Correct 16 ms 2720 KB Output is correct
135 Correct 3 ms 1144 KB Output is correct
136 Correct 17 ms 2284 KB Output is correct
137 Correct 22 ms 2928 KB Output is correct
138 Correct 3 ms 1144 KB Output is correct