답안 #343779

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
343779 2021-01-04T13:20:49 Z apostoldaniel854 결혼 문제 (IZhO14_marriage) C++14
100 / 100
671 ms 2924 KB
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define dbg(x) cerr << #x << " " << x << "\n"
using ll = long long;

const int MAX_N = 3e4;
vector <int> gr[1 + MAX_N];
int pereche[1 + MAX_N];
int viz[1 + MAX_N];

int t;
int lb, rb;

bool bkt_op (int fata);

bool fits_req (int baiat) {
    return lb <= baiat && baiat <= rb && (not pereche[baiat] || bkt_op (pereche[baiat]));
}

bool bkt_op (int fata) {
    if (viz[fata] != t) {
        viz[fata] = t;
        for (int gagic : gr[fata]) {
            if (fits_req (gagic)) {
                pereche[gagic] = fata;
                return true;
            }
        }
    }
    return false;
}
int main () {
    ios::sync_with_stdio (false);
    cin.tie (0); cout.tie (0);
    int n, m, k;
    cin >> n >> m >> k;
    for (int i = 1; i <= k; i++) {
        int x, y;
        cin >> x >> y;
        gr[y].pb (x);
    }
    for (int i = 1; i <= m; i++)
        sort (gr[i].begin (), gr[i].end ());
    lb = rb = 1;
    ll ans = 0;
    for (int i = 1; i <= m; i++) {
		t++;
		while(rb <= n && not bkt_op (i)) rb++, t++;
		if (rb > n) {
            cout << ans << "\n";
            return 0;
        }
    }
    vector <int> unpaired;
    while (lb <= n) {
        while (unpaired.size ()) {
            int cur = unpaired.back ();
            unpaired.pop_back ();
            t++;
            while(rb <= n && not bkt_op (cur)) rb++, t++;
            if (rb > n) {
                cout << ans << "\n";
                return 0;
            }
        }
        ans += (n - rb + 1);
        if (pereche[lb]) {
            unpaired.pb (pereche[lb]);
            pereche[lb] = 0;
        }
        lb++;
    }
    cout << ans << "\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1004 KB Output is correct
2 Correct 1 ms 1004 KB Output is correct
3 Correct 1 ms 1004 KB Output is correct
4 Correct 1 ms 1004 KB Output is correct
5 Correct 1 ms 1004 KB Output is correct
6 Correct 1 ms 1004 KB Output is correct
7 Correct 1 ms 1004 KB Output is correct
8 Correct 1 ms 1004 KB Output is correct
9 Correct 1 ms 1004 KB Output is correct
10 Correct 1 ms 1004 KB Output is correct
11 Correct 1 ms 1004 KB Output is correct
12 Correct 1 ms 1004 KB Output is correct
13 Correct 1 ms 1004 KB Output is correct
14 Correct 1 ms 1004 KB Output is correct
15 Correct 1 ms 1004 KB Output is correct
16 Correct 2 ms 1004 KB Output is correct
17 Correct 1 ms 1004 KB Output is correct
18 Correct 1 ms 1004 KB Output is correct
19 Correct 3 ms 1132 KB Output is correct
20 Correct 1 ms 1004 KB Output is correct
21 Correct 1 ms 1004 KB Output is correct
22 Correct 1 ms 1004 KB Output is correct
23 Correct 1 ms 1108 KB Output is correct
24 Correct 1 ms 1004 KB Output is correct
25 Correct 45 ms 1292 KB Output is correct
26 Correct 6 ms 1136 KB Output is correct
27 Correct 1 ms 1004 KB Output is correct
28 Correct 1 ms 1004 KB Output is correct
29 Correct 3 ms 1184 KB Output is correct
30 Correct 3 ms 1132 KB Output is correct
31 Correct 237 ms 1772 KB Output is correct
32 Correct 4 ms 1132 KB Output is correct
33 Correct 1 ms 1004 KB Output is correct
34 Correct 6 ms 1132 KB Output is correct
35 Correct 167 ms 2412 KB Output is correct
36 Correct 127 ms 2284 KB Output is correct
37 Correct 671 ms 1896 KB Output is correct
38 Correct 21 ms 2412 KB Output is correct
39 Correct 105 ms 1260 KB Output is correct
40 Correct 5 ms 1260 KB Output is correct
41 Correct 9 ms 1516 KB Output is correct
42 Correct 8 ms 1516 KB Output is correct
43 Correct 13 ms 1772 KB Output is correct
44 Correct 23 ms 2540 KB Output is correct
45 Correct 11 ms 1644 KB Output is correct
46 Correct 321 ms 2796 KB Output is correct
47 Correct 26 ms 2668 KB Output is correct
48 Correct 23 ms 2540 KB Output is correct
49 Correct 419 ms 2924 KB Output is correct
50 Correct 234 ms 1256 KB Output is correct