Submission #880323

# Submission time Handle Problem Language Result Execution time Memory
880323 2023-11-29T07:32:58 Z tsumondai Akcija (COCI21_akcija) C++14
110 / 110
787 ms 74380 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME  (1.0 * clock() / CLOCKS_PER_SEC)

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
 
const int N = 2024, OFS=2048;
 
const int oo = 1e9, mod = 1e9 + 7;

int n, k, curr_idx;
int w[N], best_substitute[N];
int d[N], status[N][N], opt[N][N];
priority_queue <pair<pair<int, int>, pair<int,int> > > pq;

struct node {
    int mn = 0, pos = 0, prop = 0;

    node () {}

    node (int _mn, int _pos, int _prop) {
        mn = _mn; pos = _pos; prop = _prop;
    }
} INF_NODE(2 * OFS, 0, 0);

struct tour {
    node nodes[2 * OFS];

    tour () {}

    node spoji (node a, node b) {
        if (a.mn < b.mn) return a; else return b;
    }

    void init () {
        for (int i = 0; i < OFS; i++) {
            nodes[i + OFS].mn = i;
            nodes[i + OFS].pos = i;
            nodes[i + OFS].prop = 0;
        }
        for (int i = OFS - 1; i > 0; i--) {
            nodes[i] = spoji(nodes[2 * i], nodes[2 * i + 1]);
        }
    }

    void propagate (int x) {
        if (nodes[x].prop == 0) return;

        if (x < OFS) {
            nodes[2 * x].prop += nodes[x].prop;
            nodes[2 * x + 1].prop += nodes[x].prop;
        }

        nodes[x].mn += nodes[x].prop;
        nodes[x].prop = 0;
    }

    void update (int x, int from, int to, int lo, int hi, int val) {
        propagate(x);
        if (from <= lo && hi <= to) {
            nodes[x].prop += val;
            propagate(x);
            return;
        }
        if (to < lo || hi < from) return;
        update(2 * x, from, to, lo, (lo + hi) / 2, val);
        update(2 * x + 1, from, to, (lo + hi) / 2 + 1, hi, val);
        nodes[x] = spoji(nodes[2 * x], nodes[2 * x + 1]);
    }

    void update (int from, int to, int val) {update(1, from, to, 0, OFS - 1, val);}

    node upit (int x, int from, int to, int lo, int hi) {
        propagate(x);
        if (from <= lo && hi <= to) return nodes[x];
        if (to < lo || hi < from) return INF_NODE;
        node lef = upit(2 * x, from, to, lo, (lo + hi) / 2);
        node rig = upit(2 * x + 1, from, to, (lo + hi) / 2 + 1, hi);
        return spoji(lef, rig);
    }

    node upit (int from, int to) {return upit(1, from, to, 0, OFS - 1);}
} t;

void make_sorted () {
    vector < pair <int, int> > v;
    for (int i = 1; i <= n; i++) {
        v.push_back({w[i], d[i]});
    }
    sort(v.begin(), v.end());
    for (int i = 1; i <= n; i++) {
        w[i] = v[i - 1].first;
        d[i] = v[i - 1].second;
    }
}

void pop_next () {
    pair <int, int> value = pq.top().first;
    pair <int, int> delta = pq.top().second;
    pq.pop();

    cout << value.first << " " << -value.second << endl;

    int parent_idx = delta.first;
    int old_pos = delta.second;

    if (parent_idx == -1) return;
    for (int i = 1; i <= n; i++) {
        status[curr_idx][i] = status[parent_idx][i];
        if (status[parent_idx][i] == 0 && opt[parent_idx][i] == 1) {
            if (i  < old_pos) status[curr_idx][i] = 1;
            if (i == old_pos) status[curr_idx][i] = -1;
            if (i  > old_pos) status[curr_idx][i] = 0;
        }
    }
}

pair <int, int> calc_opt () {
    int br = 0;
    int sum = 0;
    t.init();

    for (int i = 1; i <= n; i++) {
        opt[curr_idx][i] = 0;

        if (status[curr_idx][i] == 1 || status[curr_idx][i] == 0 && t.upit(d[i], OFS - 1).mn > 0) {
            t.update(d[i], OFS - 1, -1);
            opt[curr_idx][i] = 1;
            br++;
            sum += w[i];
        }
    }

    return {br, sum};
}

void add_neighbours (pair <int, int> current_value) {
    for (int i = 0; i <= n; i++) {
        best_substitute[i] = oo;
    }
    for (int i = 1; i <= n; i++) {
        if (status[curr_idx][i] == 0 && opt[curr_idx][i] == 0) {
            best_substitute[d[i]] = min(best_substitute[d[i]], w[i]);
        }
    }
    for (int i = n-1; i >= 0; i--) {
        best_substitute[i] = min(best_substitute[i], best_substitute[i + 1]);
    }

    for (int i = 1; i <= n; i++) {
        if (status[curr_idx][i] == 0 && opt[curr_idx][i] == 1) {

            int rightmost_zero = t.upit(0, d[i] - 1).pos;

            int sum, num;
            if (best_substitute[rightmost_zero + 1] == oo) {
                num = current_value.first - 1;
                sum = current_value.second - w[i];
            } else {
                num = current_value.first;
                sum = current_value.second - w[i] + best_substitute[rightmost_zero + 1];
            }

            pq.push({{num, -sum}, {curr_idx, i}});
        }
    }
}

void gen_ranking () {
    pair <int, int> tmp = calc_opt();
    pq.push({{tmp.first, -tmp.second}, {-1, -1}});

    while (curr_idx < k) {
        if (pq.empty()) {
            cout << "-1 -1" << endl;
            curr_idx++;
        } else {
            pop_next();
            add_neighbours(calc_opt());
            curr_idx++;
        }
    }
}

void process() {
	make_sorted();
	gen_ranking();
    return;
}

signed main() {
    cin.tie(0)->sync_with_stdio(false);
    //freopen(".inp", "r", stdin);
    //freopen(".out", "w", stdout);
	cin >> n >> k;
	foru(i,1,n) cin >> w[i] >> d[i];
    process();
    cerr << "Time elapsed: " << __TIME << " s.\n";
    return 0;
}

/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Keep calm and get VOI 
Flow:

*/

Compilation message

Main.cpp: In function 'std::pair<long long int, long long int> calc_opt()':
Main.cpp:136:66: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  136 |         if (status[curr_idx][i] == 1 || status[curr_idx][i] == 0 && t.upit(d[i], OFS - 1).mn > 0) {
      |                                         ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 2 ms 568 KB Output is correct
3 Correct 2 ms 576 KB Output is correct
4 Correct 2 ms 600 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 572 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 568 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 2 ms 568 KB Output is correct
3 Correct 2 ms 576 KB Output is correct
4 Correct 2 ms 600 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 572 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 568 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 568 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 2 ms 600 KB Output is correct
18 Correct 2 ms 608 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 612 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 604 KB Output is correct
2 Correct 3 ms 608 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 3 ms 860 KB Output is correct
6 Correct 1 ms 616 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 616 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 62576 KB Output is correct
2 Correct 27 ms 62556 KB Output is correct
3 Correct 25 ms 62716 KB Output is correct
4 Correct 22 ms 62556 KB Output is correct
5 Correct 24 ms 62632 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 23 ms 62328 KB Output is correct
8 Correct 21 ms 58196 KB Output is correct
9 Correct 21 ms 62552 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 22 ms 60508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4908 KB Output is correct
6 Correct 2 ms 4696 KB Output is correct
7 Correct 3 ms 4700 KB Output is correct
8 Correct 3 ms 4696 KB Output is correct
9 Correct 2 ms 4908 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 2 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 2 ms 568 KB Output is correct
3 Correct 2 ms 576 KB Output is correct
4 Correct 2 ms 600 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 572 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 568 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 568 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 2 ms 600 KB Output is correct
18 Correct 2 ms 608 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 612 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 3 ms 604 KB Output is correct
28 Correct 3 ms 608 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 2 ms 604 KB Output is correct
31 Correct 3 ms 860 KB Output is correct
32 Correct 1 ms 616 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 616 KB Output is correct
35 Correct 1 ms 600 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 23 ms 62576 KB Output is correct
41 Correct 27 ms 62556 KB Output is correct
42 Correct 25 ms 62716 KB Output is correct
43 Correct 22 ms 62556 KB Output is correct
44 Correct 24 ms 62632 KB Output is correct
45 Correct 1 ms 2652 KB Output is correct
46 Correct 23 ms 62328 KB Output is correct
47 Correct 21 ms 58196 KB Output is correct
48 Correct 21 ms 62552 KB Output is correct
49 Correct 1 ms 4700 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 22 ms 60508 KB Output is correct
53 Correct 3 ms 4952 KB Output is correct
54 Correct 3 ms 4956 KB Output is correct
55 Correct 2 ms 4956 KB Output is correct
56 Correct 3 ms 4956 KB Output is correct
57 Correct 3 ms 4908 KB Output is correct
58 Correct 2 ms 4696 KB Output is correct
59 Correct 3 ms 4700 KB Output is correct
60 Correct 3 ms 4696 KB Output is correct
61 Correct 2 ms 4908 KB Output is correct
62 Correct 2 ms 4956 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 2 ms 4956 KB Output is correct
66 Correct 653 ms 63792 KB Output is correct
67 Correct 663 ms 63504 KB Output is correct
68 Correct 624 ms 64968 KB Output is correct
69 Correct 632 ms 63456 KB Output is correct
70 Correct 787 ms 70940 KB Output is correct
71 Correct 149 ms 58964 KB Output is correct
72 Correct 308 ms 63660 KB Output is correct
73 Correct 306 ms 59024 KB Output is correct
74 Correct 312 ms 63676 KB Output is correct
75 Correct 349 ms 74380 KB Output is correct
76 Correct 1 ms 344 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 31 ms 61036 KB Output is correct