Submission #1109890

# Submission time Handle Problem Language Result Execution time Memory
1109890 2024-11-08T02:48:11 Z Trisanu_Das Nile (IOI24_nile) C++17
67 / 100
63 ms 21296 KB
#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
using namespace std;

int par[300005], sz[300005], res;

void make(int n){
    res = 2 * n;
    for(int i = 0; i < n; i++){
        par[i] = i;
        sz[i] = 1;
    }
}

int find(int u){
    if(par[u] == u) return u;
    return par[u] = find(par[u]);
}

void unite(int u, int v){
    u = find(u); v = find(v);
    if(u == v) return;
    if(sz[v] > sz[u]) swap(u, v);
    if((sz[v] & 1) && (sz[u] & 1)) res -= 2;
    par[v] = u;
    sz[u] += sz[v];
}
 
vector<long long> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E){
    int N = W.size(), Q = E.size();
    vector<ll> ans;
    if(Q <= 5){
        vector<tuple<int, int, int> > items;
        for(int i = 0; i < N; ++i) items.push_back(make_tuple(W[i], A[i], B[i]));
        sort(items.begin(), items.end());
        sort(items.begin(), items.end());
        for (int i = 0; i < N; ++i) {
            auto [w, a, b] = items[i];
            W[i] = w, A[i] = a, B[i] = b;
        }
        for(int i = 0; i < Q; ++i){
            int d = E[i];
            vector<ll> dp(N + 1, LLONG_MAX);
            for(int i = 0; i < N; ++i){
                if(i == 0) dp[i] = (ll)A[i];
                else if(i == 1){
                    if(W[i] - W[i - 1] <= d) dp[i] = (ll)B[i] + (ll)B[i - 1];
                    else dp[i] = dp[i - 1] + (ll)A[i];
                }
                else{
                    dp[i] = dp[i - 1] + (ll)A[i];
                    if(W[i] - W[i - 1] <= d) dp[i] = min(dp[i], (ll)((ll)B[i] + (ll)B[i - 1] + dp[i - 2]));
                    if(W[i] - W[i - 2] <= d){
                        if(i >= 3) dp[i] = min(dp[i], (ll)((ll)B[i] + (ll)A[i - 1] + (ll)B[i - 2] + dp[i - 3]));
                        else dp[i] = min(dp[i], (ll)((ll)B[i] + (ll)A[i - 1] + (ll)B[i - 2]));
                    }
                }
            }
            ans.push_back(dp[N - 1]);
        }
    }else{
        vector<pair<ll, ll> > v;
        vector<pair<ll, pair<int, int> > > vv;
        for(int i = 0; i < N; i++) v.push_back({W[i], i});
        sort(v.begin(), v.end());
        for(int i = 1; i < N; i++) vv.push_back({v[i].ff - v[i - 1].ff, {v[i - 1].ss, v[i].ss}});
        sort(vv.begin(), vv.end());
        vector<pair<int, int> > e;
        for(int i = 0; i < Q; i++) e.push_back({E[i], i});
        sort(e.begin(), e.end());
        int idx = 0;
        for(int q = 0; q < Q; q++){
            int d = e[q].ff, num = e[q].ss;
            while(vv[idx].ff <= d && idx < vv.size()){
                int u = vv[idx].ss.ff, v = vv[idx].ss.ss;
                unite(u, v);
                idx++;
            }
            ans[num] = res;
        }
    }
    return ans;
}

Compilation message

nile.cpp: In function 'std::vector<long long int> calculate_costs(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
nile.cpp:76:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |             while(vv[idx].ff <= d && idx < vv.size()){
      |                                      ~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 1 ms 456 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 7348 KB Output is correct
2 Correct 32 ms 7360 KB Output is correct
3 Correct 29 ms 7368 KB Output is correct
4 Correct 29 ms 7232 KB Output is correct
5 Correct 29 ms 7360 KB Output is correct
6 Correct 29 ms 7368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 5824 KB Output is correct
2 Correct 29 ms 6240 KB Output is correct
3 Correct 39 ms 6080 KB Output is correct
4 Correct 31 ms 6080 KB Output is correct
5 Correct 32 ms 6084 KB Output is correct
6 Correct 32 ms 6080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 1 ms 456 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 1 ms 592 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 2 ms 524 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 1 ms 592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 1 ms 456 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 33 ms 7348 KB Output is correct
8 Correct 32 ms 7360 KB Output is correct
9 Correct 29 ms 7368 KB Output is correct
10 Correct 29 ms 7232 KB Output is correct
11 Correct 29 ms 7360 KB Output is correct
12 Correct 29 ms 7368 KB Output is correct
13 Correct 29 ms 5824 KB Output is correct
14 Correct 29 ms 6240 KB Output is correct
15 Correct 39 ms 6080 KB Output is correct
16 Correct 31 ms 6080 KB Output is correct
17 Correct 32 ms 6084 KB Output is correct
18 Correct 32 ms 6080 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 2 ms 524 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 43 ms 7512 KB Output is correct
26 Correct 39 ms 7612 KB Output is correct
27 Correct 41 ms 7628 KB Output is correct
28 Correct 39 ms 7608 KB Output is correct
29 Correct 38 ms 7600 KB Output is correct
30 Correct 37 ms 7616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 5824 KB Output is correct
2 Correct 29 ms 6240 KB Output is correct
3 Correct 39 ms 6080 KB Output is correct
4 Correct 31 ms 6080 KB Output is correct
5 Correct 32 ms 6084 KB Output is correct
6 Correct 32 ms 6080 KB Output is correct
7 Runtime error 63 ms 21296 KB Execution killed with signal 11
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 456 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 33 ms 7348 KB Output is correct
9 Correct 32 ms 7360 KB Output is correct
10 Correct 29 ms 7368 KB Output is correct
11 Correct 29 ms 7232 KB Output is correct
12 Correct 29 ms 7360 KB Output is correct
13 Correct 29 ms 7368 KB Output is correct
14 Correct 29 ms 5824 KB Output is correct
15 Correct 29 ms 6240 KB Output is correct
16 Correct 39 ms 6080 KB Output is correct
17 Correct 31 ms 6080 KB Output is correct
18 Correct 32 ms 6084 KB Output is correct
19 Correct 32 ms 6080 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 1 ms 592 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 2 ms 524 KB Output is correct
24 Correct 2 ms 592 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 43 ms 7512 KB Output is correct
27 Correct 39 ms 7612 KB Output is correct
28 Correct 41 ms 7628 KB Output is correct
29 Correct 39 ms 7608 KB Output is correct
30 Correct 38 ms 7600 KB Output is correct
31 Correct 37 ms 7616 KB Output is correct
32 Runtime error 63 ms 21296 KB Execution killed with signal 11
33 Halted 0 ms 0 KB -