답안 #146525

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
146525 2019-08-24T10:00:06 Z MvC Alternating Current (BOI18_alternating) C++11
100 / 100
84 ms 9960 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 = 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

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])) {
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 372 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 376 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 424 KB Output is correct
15 Correct 2 ms 380 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 380 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 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 380 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 424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 372 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 376 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 424 KB Output is correct
15 Correct 2 ms 380 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 380 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 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 380 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 424 KB Output is correct
41 Correct 2 ms 484 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 424 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 392 KB Output is correct
53 Correct 2 ms 424 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 376 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
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 372 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 376 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 424 KB Output is correct
15 Correct 2 ms 380 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 380 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 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 380 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 424 KB Output is correct
41 Correct 2 ms 484 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 424 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 392 KB Output is correct
53 Correct 2 ms 424 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 376 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 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 388 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 348 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 376 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 376 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 376 KB Output is correct
92 Correct 2 ms 368 KB Output is correct
93 Correct 2 ms 376 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 376 KB Output is correct
97 Correct 3 ms 552 KB Output is correct
98 Correct 3 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 3840 KB Output is correct
2 Correct 3 ms 1272 KB Output is correct
3 Correct 16 ms 2156 KB Output is correct
4 Correct 22 ms 2672 KB Output is correct
5 Correct 81 ms 9576 KB Output is correct
6 Correct 28 ms 3152 KB Output is correct
7 Correct 77 ms 9168 KB Output is correct
8 Correct 5 ms 1272 KB Output is correct
9 Correct 3 ms 1144 KB Output is correct
10 Correct 84 ms 9960 KB Output is correct
11 Correct 63 ms 7916 KB Output is correct
12 Correct 34 ms 3820 KB Output is correct
13 Correct 3 ms 632 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 38 ms 4076 KB Output is correct
16 Correct 16 ms 2028 KB Output is correct
17 Correct 38 ms 4072 KB Output is correct
18 Correct 34 ms 3176 KB Output is correct
19 Correct 4 ms 1016 KB Output is correct
20 Correct 83 ms 9576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 372 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 376 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 424 KB Output is correct
15 Correct 2 ms 380 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 380 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 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 380 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 424 KB Output is correct
41 Correct 2 ms 484 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 424 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 392 KB Output is correct
53 Correct 2 ms 424 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 376 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 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 388 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 348 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 376 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 376 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 376 KB Output is correct
92 Correct 2 ms 368 KB Output is correct
93 Correct 2 ms 376 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 376 KB Output is correct
97 Correct 3 ms 552 KB Output is correct
98 Correct 3 ms 376 KB Output is correct
99 Correct 29 ms 3840 KB Output is correct
100 Correct 3 ms 1272 KB Output is correct
101 Correct 16 ms 2156 KB Output is correct
102 Correct 22 ms 2672 KB Output is correct
103 Correct 81 ms 9576 KB Output is correct
104 Correct 28 ms 3152 KB Output is correct
105 Correct 77 ms 9168 KB Output is correct
106 Correct 5 ms 1272 KB Output is correct
107 Correct 3 ms 1144 KB Output is correct
108 Correct 84 ms 9960 KB Output is correct
109 Correct 63 ms 7916 KB Output is correct
110 Correct 34 ms 3820 KB Output is correct
111 Correct 3 ms 632 KB Output is correct
112 Correct 3 ms 504 KB Output is correct
113 Correct 38 ms 4076 KB Output is correct
114 Correct 16 ms 2028 KB Output is correct
115 Correct 38 ms 4072 KB Output is correct
116 Correct 34 ms 3176 KB Output is correct
117 Correct 4 ms 1016 KB Output is correct
118 Correct 83 ms 9576 KB Output is correct
119 Correct 3 ms 1068 KB Output is correct
120 Correct 54 ms 7408 KB Output is correct
121 Correct 35 ms 5076 KB Output is correct
122 Correct 2 ms 380 KB Output is correct
123 Correct 4 ms 888 KB Output is correct
124 Correct 12 ms 1652 KB Output is correct
125 Correct 23 ms 3700 KB Output is correct
126 Correct 3 ms 1144 KB Output is correct
127 Correct 7 ms 1632 KB Output is correct
128 Correct 65 ms 8332 KB Output is correct
129 Correct 38 ms 5356 KB Output is correct
130 Correct 46 ms 6180 KB Output is correct
131 Correct 3 ms 1144 KB Output is correct
132 Correct 4 ms 1272 KB Output is correct
133 Correct 61 ms 6492 KB Output is correct
134 Correct 17 ms 2676 KB Output is correct
135 Correct 3 ms 1144 KB Output is correct
136 Correct 18 ms 2156 KB Output is correct
137 Correct 22 ms 2952 KB Output is correct
138 Correct 4 ms 1144 KB Output is correct