답안 #719745

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
719745 2023-04-06T15:18:23 Z keta_tsimakuridze Alternating Current (BOI18_alternating) C++14
0 / 100
211 ms 10884 KB
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#define f first
#define s second
#define ll long long
#define pii pair<int,int>
#define Pii pair<pair<int, int>, int>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7;
int n, from[N], last2[N], last3[N];
set<Pii> S;
struct seg{
    int a, b, id;
} p[N];

int try_(seg s) {
    queue<int> q;
    q.push(s.id);
    from[s.id] = 0;
    while(q.size()) {
        int u = q.front();
        q.pop();
        if(p[u].b == n) return u;
        if(p[u].b > n && (s.a > p[u].b - n || last3[p[u].b - n] < s.a) && last2[p[u].b - n] == 0) return u;
        if (p[u].b >= n) continue;
        while (S.upper_bound({{p[u].b + 2, 0}, 0}) != S.begin()) {
            Pii x = *--S.upper_bound({{p[u].b + 2, 0}, 0});
            if (last2[p[u].b] < x.f.f) q.push(x.s), from[x.s] = u, S.erase(x);
            else break;
        }
    }
    return -1;
}
bool cmp(seg a, seg b) {
    return (a.a < b.a);
}
int len(int a, int b) {
    return (b >= a ? b - a + 1 : n - a + 1 + b);
}
int get(int x, int a) {
    if(x >= a) return x - a + 1;

    return n - a + 1 + x;
}
signed main() {
    int m;
    cin >> n >> m;
    int mx = 0;
    for(int i = 1; i <= m; i++) {
        cin >> p[i].a >> p[i].b;
        p[i].id = i;
        if(len(p[i].a, p[i].b) > len(p[mx].a, p[mx].b)) mx = i;
    }
    int st = p[mx].a;
    vector<int> c(n + 5);
    for(int i = 1; i <= m; i++) {
        p[i].a = get(p[i].a, st);
        p[i].b = get(p[i].b, st);
        ++c[p[i].a], --c[p[i].b + 1];
        if(p[i].a > p[i].b) ++c[1], p[i].b = p[i].b + n;
    }

    for(int i = 1; i <= n; i++) {
        c[i] += c[i - 1];
        last3[i] = last3[i - 1];
        last2[i] = last2[i - 1];
        if(c[i] == 1) {
            cout << "impossible";
            return 0;
        }
        if(c[i] == 2) last2[i] = i;
        if(c[i] <= 3) last3[i] = i;
    }
    vector<seg> x;
    for(int i = 1; i <= m; i++) {
        if(i == mx) continue;
        if(p[i].a <= p[mx].b + 1) {
            if(last2[p[mx].b] < p[i].a) x.push_back(p[i]);
        }
        else S.insert({{p[i].a, p[i].b}, i});
    }
    sort(x.begin(), x.end(), cmp);
    int ST = (p[mx].b == n ? mx : -1);
    while(x.size()) {

        seg s = x.back(); x.pop_back();

        ST = try_(s);
        if(ST != -1) {
            ST = s.id;
            break;
        }
    }
    if(ST == -1) {
        cout << "impossible";
        return 0;
    }
    S.clear();
    for(int i = 1; i <= m; i++) {
        if(i == mx) continue;
        if(p[i].a <= p[mx].b + 1) {}
        else S.insert({{p[i].a, p[i].b}, i});
    }
    vector<int> ans(m + 5); ans[mx] = 1;
    int EN = try_(p[ST]);
  	if(EN == -1) {
      while(true) EN = -1;
    }
    while(EN) {
        ans[EN] = 1;
        EN = from[EN];
    }
    for(int i = 1; i <= m; i++) cout << ans[i] << "";
}
/*
 10 5
 1 5
 6 7
 5 1
 7 2
 2 4
 */
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 224 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 312 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 316 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Incorrect 1 ms 212 KB 'impossible' claimed, but there is a solution
40 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 224 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 312 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 316 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Incorrect 1 ms 212 KB 'impossible' claimed, but there is a solution
40 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 224 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 312 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 316 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Incorrect 1 ms 212 KB 'impossible' claimed, but there is a solution
40 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 4308 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 28 ms 1288 KB Output is correct
4 Correct 31 ms 2296 KB Output is correct
5 Correct 192 ms 9420 KB Output is correct
6 Correct 55 ms 3636 KB Output is correct
7 Correct 188 ms 9888 KB Output is correct
8 Correct 6 ms 1748 KB Output is correct
9 Correct 3 ms 1492 KB Output is correct
10 Correct 211 ms 10884 KB Output is correct
11 Correct 145 ms 8448 KB Output is correct
12 Correct 55 ms 3916 KB Output is correct
13 Correct 2 ms 1468 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 60 ms 4324 KB Output is correct
16 Correct 28 ms 2576 KB Output is correct
17 Correct 77 ms 5848 KB Output is correct
18 Correct 62 ms 4184 KB Output is correct
19 Correct 4 ms 1620 KB Output is correct
20 Incorrect 79 ms 9472 KB 'impossible' claimed, but there is a solution
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 224 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 316 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 312 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 316 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Incorrect 1 ms 212 KB 'impossible' claimed, but there is a solution
40 Halted 0 ms 0 KB -