답안 #1111158

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1111158 2024-11-11T15:29:32 Z Ghulam_Junaid Tug of War (BOI15_tug) C++17
100 / 100
475 ms 12884 KB
#include <bits/stdc++.h>
using namespace std;
 
const int N = 30005;
int n, k, X, Y;
bitset<N * 20> dp;
multiset<pair<int, int>> g[2 * N];
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
 
    cin >> n >> k;
    for (int i = 0; i < 2 * n; i ++){
        int l, r, s;
        cin >> l >> r >> s;
        r += n;
        g[l].insert({r, s});
        g[r].insert({l, s});
    }
 
    set<pair<int, int>> st;
    for (int i = 1; i <= 2 * n; i ++)
        st.insert({g[i].size(), i});
 
    while (!st.empty()){
        auto p = *st.begin();
        st.erase(st.begin());
 
        if (p.first > 1) break;
        if (p.first == 0){
            cout << "NO" << endl;
            return 0;
        }
 
        int v = p.second;        
        auto q = *g[v].begin();
        int u = q.first;
        
        if (v <= n)
            X += q.second;
        else
            Y += q.second;
 
        g[v].clear();
        st.erase({g[u].size(), u});
        auto it = g[u].lower_bound({v, q.second});
        g[u].erase(it);
        st.insert({g[u].size(), u});
    }
 
    int cyc = -1;
    int tmp = 1;
    int dif = 0;
    
    int nxt = -1;
    if (!st.empty())
        nxt = (*st.begin()).second;
 
    vector<int> vec;
    while (!st.empty()){
        int v = nxt;
        st.erase({g[v].size(), v});
 
        auto q = *g[v].begin();
        int u = q.first;
        int s = q.second;
 
        nxt = u;
 
        if (cyc == -1){
            cyc = v;
            
            dif += s * tmp;
            tmp *= -1;
            st.erase({g[u].size(), u});
            auto it = g[u].lower_bound({v, s});
            g[u].erase(it);
            st.insert({g[u].size(), u});
        }
        else{
            dif += s * tmp;
            tmp *= -1;
            if (u == cyc){
                vec.push_back(abs(dif));
                cyc = -1;
                dif = 0;
                tmp = 1;
 
                if (!st.empty())
                    nxt = (*st.begin()).second;
            }
            else{
                st.erase({g[u].size(), u});
                g[u].erase({v, s});
                st.insert({g[u].size(), u});
            }
        }
    }
 
    int total = 0;
    for (int x : vec)
        total += x;
 
    if (total >= N * 20){
        cout << "ASDFASF" << endl;
        return 0;
    }
 
    dp[0] = 1;
    for (int x : vec)
        dp |= (dp << x);
 
    for (int sm = 0; sm <= total; sm ++){
        if (dp[sm]){
            if (abs((X + sm) - (total - sm + Y)) <= k){
                cout << "YES" << endl;
                return 0;
            }
        }
    }
 
    cout << "NO" << endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3408 KB Output is correct
2 Correct 2 ms 3408 KB Output is correct
3 Correct 2 ms 3408 KB Output is correct
4 Correct 2 ms 3408 KB Output is correct
5 Correct 2 ms 3408 KB Output is correct
6 Correct 2 ms 3408 KB Output is correct
7 Correct 2 ms 3420 KB Output is correct
8 Correct 2 ms 3408 KB Output is correct
9 Correct 2 ms 3408 KB Output is correct
10 Correct 2 ms 3408 KB Output is correct
11 Correct 2 ms 3408 KB Output is correct
12 Correct 2 ms 3408 KB Output is correct
13 Correct 3 ms 3432 KB Output is correct
14 Correct 2 ms 3408 KB Output is correct
15 Correct 2 ms 3408 KB Output is correct
16 Correct 2 ms 3408 KB Output is correct
17 Correct 2 ms 3408 KB Output is correct
18 Correct 3 ms 3432 KB Output is correct
19 Correct 2 ms 3408 KB Output is correct
20 Correct 1 ms 3408 KB Output is correct
21 Correct 2 ms 3408 KB Output is correct
22 Correct 1 ms 3408 KB Output is correct
23 Correct 2 ms 3408 KB Output is correct
24 Correct 2 ms 3432 KB Output is correct
25 Correct 2 ms 3408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3408 KB Output is correct
2 Correct 2 ms 3408 KB Output is correct
3 Correct 2 ms 3408 KB Output is correct
4 Correct 2 ms 3408 KB Output is correct
5 Correct 2 ms 3408 KB Output is correct
6 Correct 2 ms 3408 KB Output is correct
7 Correct 2 ms 3420 KB Output is correct
8 Correct 2 ms 3408 KB Output is correct
9 Correct 2 ms 3408 KB Output is correct
10 Correct 2 ms 3408 KB Output is correct
11 Correct 2 ms 3408 KB Output is correct
12 Correct 2 ms 3408 KB Output is correct
13 Correct 3 ms 3432 KB Output is correct
14 Correct 2 ms 3408 KB Output is correct
15 Correct 2 ms 3408 KB Output is correct
16 Correct 2 ms 3408 KB Output is correct
17 Correct 2 ms 3408 KB Output is correct
18 Correct 3 ms 3432 KB Output is correct
19 Correct 2 ms 3408 KB Output is correct
20 Correct 1 ms 3408 KB Output is correct
21 Correct 2 ms 3408 KB Output is correct
22 Correct 1 ms 3408 KB Output is correct
23 Correct 2 ms 3408 KB Output is correct
24 Correct 2 ms 3432 KB Output is correct
25 Correct 2 ms 3408 KB Output is correct
26 Correct 29 ms 3920 KB Output is correct
27 Correct 8 ms 3920 KB Output is correct
28 Correct 31 ms 3920 KB Output is correct
29 Correct 8 ms 3920 KB Output is correct
30 Correct 29 ms 4108 KB Output is correct
31 Correct 8 ms 3920 KB Output is correct
32 Correct 29 ms 3920 KB Output is correct
33 Correct 8 ms 3920 KB Output is correct
34 Correct 6 ms 3920 KB Output is correct
35 Correct 8 ms 3920 KB Output is correct
36 Correct 39 ms 3920 KB Output is correct
37 Correct 8 ms 3920 KB Output is correct
38 Correct 41 ms 3920 KB Output is correct
39 Correct 8 ms 3972 KB Output is correct
40 Correct 40 ms 4104 KB Output is correct
41 Correct 8 ms 3920 KB Output is correct
42 Correct 30 ms 4104 KB Output is correct
43 Correct 8 ms 3920 KB Output is correct
44 Correct 29 ms 3920 KB Output is correct
45 Correct 8 ms 3920 KB Output is correct
46 Correct 29 ms 3932 KB Output is correct
47 Correct 4 ms 3920 KB Output is correct
48 Correct 17 ms 4112 KB Output is correct
49 Correct 17 ms 4116 KB Output is correct
50 Correct 31 ms 3920 KB Output is correct
51 Correct 30 ms 3920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 108 ms 6480 KB Output is correct
2 Correct 15 ms 6480 KB Output is correct
3 Correct 81 ms 6480 KB Output is correct
4 Correct 11 ms 6652 KB Output is correct
5 Correct 84 ms 6480 KB Output is correct
6 Correct 11 ms 6480 KB Output is correct
7 Correct 92 ms 6480 KB Output is correct
8 Correct 12 ms 6480 KB Output is correct
9 Correct 106 ms 6492 KB Output is correct
10 Correct 11 ms 6364 KB Output is correct
11 Correct 90 ms 6480 KB Output is correct
12 Correct 11 ms 6480 KB Output is correct
13 Correct 91 ms 6456 KB Output is correct
14 Correct 96 ms 6480 KB Output is correct
15 Correct 12 ms 6480 KB Output is correct
16 Correct 96 ms 6472 KB Output is correct
17 Correct 11 ms 6480 KB Output is correct
18 Correct 94 ms 6480 KB Output is correct
19 Correct 12 ms 6240 KB Output is correct
20 Correct 97 ms 6528 KB Output is correct
21 Correct 14 ms 6316 KB Output is correct
22 Correct 60 ms 6588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3408 KB Output is correct
2 Correct 2 ms 3408 KB Output is correct
3 Correct 2 ms 3408 KB Output is correct
4 Correct 2 ms 3408 KB Output is correct
5 Correct 2 ms 3408 KB Output is correct
6 Correct 2 ms 3408 KB Output is correct
7 Correct 2 ms 3420 KB Output is correct
8 Correct 2 ms 3408 KB Output is correct
9 Correct 2 ms 3408 KB Output is correct
10 Correct 2 ms 3408 KB Output is correct
11 Correct 2 ms 3408 KB Output is correct
12 Correct 2 ms 3408 KB Output is correct
13 Correct 3 ms 3432 KB Output is correct
14 Correct 2 ms 3408 KB Output is correct
15 Correct 2 ms 3408 KB Output is correct
16 Correct 2 ms 3408 KB Output is correct
17 Correct 2 ms 3408 KB Output is correct
18 Correct 3 ms 3432 KB Output is correct
19 Correct 2 ms 3408 KB Output is correct
20 Correct 1 ms 3408 KB Output is correct
21 Correct 2 ms 3408 KB Output is correct
22 Correct 1 ms 3408 KB Output is correct
23 Correct 2 ms 3408 KB Output is correct
24 Correct 2 ms 3432 KB Output is correct
25 Correct 2 ms 3408 KB Output is correct
26 Correct 29 ms 3920 KB Output is correct
27 Correct 8 ms 3920 KB Output is correct
28 Correct 31 ms 3920 KB Output is correct
29 Correct 8 ms 3920 KB Output is correct
30 Correct 29 ms 4108 KB Output is correct
31 Correct 8 ms 3920 KB Output is correct
32 Correct 29 ms 3920 KB Output is correct
33 Correct 8 ms 3920 KB Output is correct
34 Correct 6 ms 3920 KB Output is correct
35 Correct 8 ms 3920 KB Output is correct
36 Correct 39 ms 3920 KB Output is correct
37 Correct 8 ms 3920 KB Output is correct
38 Correct 41 ms 3920 KB Output is correct
39 Correct 8 ms 3972 KB Output is correct
40 Correct 40 ms 4104 KB Output is correct
41 Correct 8 ms 3920 KB Output is correct
42 Correct 30 ms 4104 KB Output is correct
43 Correct 8 ms 3920 KB Output is correct
44 Correct 29 ms 3920 KB Output is correct
45 Correct 8 ms 3920 KB Output is correct
46 Correct 29 ms 3932 KB Output is correct
47 Correct 4 ms 3920 KB Output is correct
48 Correct 17 ms 4112 KB Output is correct
49 Correct 17 ms 4116 KB Output is correct
50 Correct 31 ms 3920 KB Output is correct
51 Correct 30 ms 3920 KB Output is correct
52 Correct 108 ms 6480 KB Output is correct
53 Correct 15 ms 6480 KB Output is correct
54 Correct 81 ms 6480 KB Output is correct
55 Correct 11 ms 6652 KB Output is correct
56 Correct 84 ms 6480 KB Output is correct
57 Correct 11 ms 6480 KB Output is correct
58 Correct 92 ms 6480 KB Output is correct
59 Correct 12 ms 6480 KB Output is correct
60 Correct 106 ms 6492 KB Output is correct
61 Correct 11 ms 6364 KB Output is correct
62 Correct 90 ms 6480 KB Output is correct
63 Correct 11 ms 6480 KB Output is correct
64 Correct 91 ms 6456 KB Output is correct
65 Correct 96 ms 6480 KB Output is correct
66 Correct 12 ms 6480 KB Output is correct
67 Correct 96 ms 6472 KB Output is correct
68 Correct 11 ms 6480 KB Output is correct
69 Correct 94 ms 6480 KB Output is correct
70 Correct 12 ms 6240 KB Output is correct
71 Correct 97 ms 6528 KB Output is correct
72 Correct 14 ms 6316 KB Output is correct
73 Correct 60 ms 6588 KB Output is correct
74 Correct 475 ms 12876 KB Output is correct
75 Correct 67 ms 12596 KB Output is correct
76 Correct 470 ms 12748 KB Output is correct
77 Correct 73 ms 12624 KB Output is correct
78 Correct 448 ms 12872 KB Output is correct
79 Correct 431 ms 12872 KB Output is correct
80 Correct 77 ms 12624 KB Output is correct
81 Correct 73 ms 12624 KB Output is correct
82 Correct 418 ms 12764 KB Output is correct
83 Correct 441 ms 12872 KB Output is correct
84 Correct 75 ms 12884 KB Output is correct
85 Correct 436 ms 12860 KB Output is correct
86 Correct 68 ms 12624 KB Output is correct
87 Correct 417 ms 12876 KB Output is correct
88 Correct 74 ms 12652 KB Output is correct
89 Correct 452 ms 12880 KB Output is correct
90 Correct 69 ms 12720 KB Output is correct
91 Correct 451 ms 12872 KB Output is correct
92 Correct 81 ms 12616 KB Output is correct
93 Correct 414 ms 12872 KB Output is correct
94 Correct 35 ms 12624 KB Output is correct
95 Correct 254 ms 12676 KB Output is correct
96 Correct 242 ms 12872 KB Output is correct
97 Correct 436 ms 12872 KB Output is correct
98 Correct 443 ms 12688 KB Output is correct