Submission #367017

# Submission time Handle Problem Language Result Execution time Memory
367017 2021-02-16T03:52:52 Z fishy15 Arranging Tickets (JOI17_arranging_tickets) C++14
45 / 100
6000 ms 5228 KB
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>

#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 200010

using namespace std;

int n, m;
vector<array<int, 3>> range[MAXN];
ll init[MAXN];
int min_check, max_check;

struct cmp {
    bool operator()(const array<int, 3> &a, const array<int, 3> &b) const {
        if (a[1] == b[1]) {
            if (a[0] == b[0]) {
                return a[2] < b[2];
            }
            return a[0] > b[0];
        }
        return a[1] < b[1];
    }
};

struct bit {
    ll vals[MAXN];
    void reset() {
        for (int i = 0; i < n + 10; i++) {
            vals[i] = 0;
        }
        for (int i = 0; i < n; i++) {
            ll x = init[i];
            if (i) x -= init[i - 1];
            upd(i, x);
        }
    }
    void upd(int x, ll v) {
        x++;
        while (x < n + 10) {
            vals[x] += v;
            x += x & -x;
        }
    }
    void upd(int l, int r, ll v) {
        upd(l, v);
        upd(r, -v);
    }
    ll qry(int x) {
        x++;
        ll res = 0;
        while (x) {
            res += vals[x];
            x -= x & -x;
        }
        return res;
    }
} b;

ll check(ll m, ll n, ll t) {
    b.reset();
    priority_queue<array<int, 3>, vector<array<int, 3>>, cmp> pq;
    for (int i = 0; i <= t; i++) {
        ll need_rem = max((b.qry(i) + n - m + 1) / 2, 0LL);
        n -= need_rem;
        for (auto &arr : range[i]) {
            if (arr[1] > t) {
                pq.push(arr);
            }
        }
        while (need_rem) {
            if (pq.empty()) {
                return false;
            }
            auto t = pq.top(); pq.pop();
            ll to_rem = min<ll>(t[2], need_rem);
            b.upd(0, t[0], to_rem);
            b.upd(t[0], t[1], -to_rem);
            b.upd(t[1], ::n, to_rem);
            t[2] -= to_rem;
            need_rem -= to_rem;
            if (t[2]) pq.push(t);
        }
    }

    for (int i = 0; i < ::n; i++) {
        if (b.qry(i) > m) {
            return false;
        }
    }

    return true;
}

ll check(ll m) {
    ll a = init[min_check];
    for (int i = 0; i <= ::m; i++) {
        for (int j = 0; j < n; j++) {
            if (check(m, i, j)) {
                return true;
            }
        }
    }

    return false;
    /* return check(m, a - m, min_check) || check(m, a - m + 1, min_check) */
        /* || check(m, a - m, max_check) || check(m, a - m + 1, max_check); */
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int a, b, c; cin >> a >> b >> c;
        a--; b--;
        if (a > b) swap(a, b);
        init[a] += c;
        init[b] -= c;
        range[a].push_back({a, b, c});
    }

    for (int i = 1; i < n; i++) {
        init[i] += init[i - 1];
    }

    for (int i = 0; i < n; i++) {
        if (init[i] > init[min_check]) {
            min_check = i;
            max_check = i;
        } else if (init[i] == init[min_check]) {
            max_check = i;
        }
    }

    ll l = 0;
    ll r = init[min_check];
    ll ans = init[min_check];

    while (l <= r) {
        ll m = (l + r) / 2;
        if (check(m)) {
            ans = m;
            r = m - 1;
        } else {
            l = m + 1;
        }
    }

    cout << ans << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 6 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 5 ms 5100 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 5 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 6 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 5 ms 5100 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 5 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 3271 ms 5120 KB Output is correct
17 Correct 3457 ms 5228 KB Output is correct
18 Correct 3380 ms 5228 KB Output is correct
19 Correct 3452 ms 5100 KB Output is correct
20 Correct 3654 ms 5100 KB Output is correct
21 Correct 3472 ms 5124 KB Output is correct
22 Correct 3623 ms 5124 KB Output is correct
23 Correct 3546 ms 5124 KB Output is correct
24 Correct 2137 ms 5228 KB Output is correct
25 Correct 2368 ms 5228 KB Output is correct
26 Correct 2827 ms 5228 KB Output is correct
27 Correct 2498 ms 5228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 6 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 5 ms 5100 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 5 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 3271 ms 5120 KB Output is correct
17 Correct 3457 ms 5228 KB Output is correct
18 Correct 3380 ms 5228 KB Output is correct
19 Correct 3452 ms 5100 KB Output is correct
20 Correct 3654 ms 5100 KB Output is correct
21 Correct 3472 ms 5124 KB Output is correct
22 Correct 3623 ms 5124 KB Output is correct
23 Correct 3546 ms 5124 KB Output is correct
24 Correct 2137 ms 5228 KB Output is correct
25 Correct 2368 ms 5228 KB Output is correct
26 Correct 2827 ms 5228 KB Output is correct
27 Correct 2498 ms 5228 KB Output is correct
28 Execution timed out 6049 ms 5228 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 6 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 5 ms 5100 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 5 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 3271 ms 5120 KB Output is correct
17 Correct 3457 ms 5228 KB Output is correct
18 Correct 3380 ms 5228 KB Output is correct
19 Correct 3452 ms 5100 KB Output is correct
20 Correct 3654 ms 5100 KB Output is correct
21 Correct 3472 ms 5124 KB Output is correct
22 Correct 3623 ms 5124 KB Output is correct
23 Correct 3546 ms 5124 KB Output is correct
24 Correct 2137 ms 5228 KB Output is correct
25 Correct 2368 ms 5228 KB Output is correct
26 Correct 2827 ms 5228 KB Output is correct
27 Correct 2498 ms 5228 KB Output is correct
28 Execution timed out 6049 ms 5228 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 6 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 5 ms 5100 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 5 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5100 KB Output is correct
16 Correct 3271 ms 5120 KB Output is correct
17 Correct 3457 ms 5228 KB Output is correct
18 Correct 3380 ms 5228 KB Output is correct
19 Correct 3452 ms 5100 KB Output is correct
20 Correct 3654 ms 5100 KB Output is correct
21 Correct 3472 ms 5124 KB Output is correct
22 Correct 3623 ms 5124 KB Output is correct
23 Correct 3546 ms 5124 KB Output is correct
24 Correct 2137 ms 5228 KB Output is correct
25 Correct 2368 ms 5228 KB Output is correct
26 Correct 2827 ms 5228 KB Output is correct
27 Correct 2498 ms 5228 KB Output is correct
28 Execution timed out 6049 ms 5228 KB Time limit exceeded
29 Halted 0 ms 0 KB -