답안 #1103417

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1103417 2024-10-20T23:55:44 Z aaaaaarroz 나일강 (IOI24_nile) C++17
82 / 100
128 ms 18352 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Dsu {
    ll cnt;
    vector<ll> parent, rank, size;
    Dsu(ll N) {
        cnt = 2 * N;
        parent.resize(N);
        rank.resize(N);
        size.resize(N, 1);
        for (ll i = 0; i < N; i++) {
            parent[i] = i;
        }
    }
    bool isSameSet(ll i, ll j) {
        return findSet(i) == findSet(j);
    }
    ll findSet(ll i) { 
        if (parent[i] == i) {
            return i;
        } else {
            return parent[i] = findSet(parent[i]);
        } 
    }
    void unionSet(ll i, ll j) {
        ll x = findSet(i);
        ll y = findSet(j);
        if (x != y) {
            if((size[x]%2)==(size[y]%2)&&size[y]%2==1){
                cnt -= 2;  
            }
            if (rank[x] > rank[y]) {
                parent[y] = x;
                size[x] += size[y];
            } else if (rank[x] < rank[y]) {
                parent[x] = y;
                size[y] += size[x];
            } else {
                parent[x] = y;
                size[y] += size[x];
                rank[y]++;
            }
        }
    }
    ll res() {
        return cnt;
    }
};
vector<ll> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E) {     
    if(E.size()>10){              
        vector<pair<ll, int>> sorted_W;
        ll n = W.size();
        for (int i = 0; i < n; i++) {
            sorted_W.push_back({W[i], i});
        }
        sort(sorted_W.begin(), sorted_W.end());
        vector<int> new_A(n), new_B(n);
        for (int i = 0; i < n; i++) {
            new_A[i] = A[sorted_W[i].second];
            new_B[i] = B[sorted_W[i].second];
        }
        A = new_A;
        B = new_B;
        Dsu dsu(n);
        vector<vector<ll>> edges;
        for (ll i = 0; i < n - 1; i++) {
            edges.push_back({abs(sorted_W[i + 1].first - sorted_W[i].first), i, i + 1});
        }
        edges.push_back({INT_MAX, -1, -1});  
        sort(edges.begin(), edges.end());
        vector<pair<ll, ll>> q;
        for (ll i = 0; i < E.size(); i++) {
            q.push_back({E[i], i});
        }
        sort(q.begin(), q.end());
        vector<ll> queries(E.size());
        ll puntero = 0;
        for (auto [d, pos] : q) {
            while (edges[puntero][0] <= d && puntero < edges.size()) {
                ll u = edges[puntero][1], v = edges[puntero][2];
                if (u != -1 && v != -1 && !dsu.isSameSet(u, v)) {
                    dsu.unionSet(u, v);
                }
                puntero++;
            }
            queries[pos] = dsu.res();
        }
        return queries;
    }
    else{
        int Q = (int)E.size();
        int N=(int)W.size();
        vector<ll>ans;
        vector<tuple<int,int,int>>ordenar;
        for(int i=0;i<N;i++){
            ordenar.push_back({W[i],B[i],A[i]});
        }
        sort(ordenar.begin(),ordenar.end());
        W.clear();
        A.clear();
        B.clear();
        for(auto[w,b,a]:ordenar){
            A.push_back(a);
            B.push_back(b);
            W.push_back(w);
        }
        for(int e=0;e<Q;e++){
            int d=E[e];
            vector<ll>dp(N,LLONG_MAX);
            for(int i=0;i<N;i++){
            if(i==0){
                dp[i]=A[i];
            }
            if(i==1){
                if((W[i]-W[i-1])<=d){
                dp[i]=(B[i]+B[i-1]);
                }
                else{
                dp[i]=(dp[i-1]+A[i]);
                }
            }
            if(i>=2){
                dp[i]=((ll)A[i]+dp[i-1]);
                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)B[i-2]+(ll)A[i-1]+dp[i-3]));
                }
                else{
                    dp[i]=min(dp[i],(ll)((ll)B[i]+(ll)B[i-2]+(ll)A[i-1]));
                }
                }
            }
            }
            ans.push_back(dp[N-1]);
        }
        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:73:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |         for (ll i = 0; i < E.size(); i++) {
      |                        ~~^~~~~~~~~~
nile.cpp:80:54: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |             while (edges[puntero][0] <= d && puntero < edges.size()) {
      |                                              ~~~~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 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 504 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 4868 KB Output is correct
2 Correct 30 ms 7360 KB Output is correct
3 Correct 28 ms 7220 KB Output is correct
4 Correct 31 ms 7096 KB Output is correct
5 Correct 28 ms 7224 KB Output is correct
6 Correct 29 ms 7368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 4808 KB Output is correct
2 Correct 32 ms 4808 KB Output is correct
3 Correct 30 ms 4820 KB Output is correct
4 Correct 30 ms 4808 KB Output is correct
5 Correct 31 ms 4800 KB Output is correct
6 Correct 28 ms 4800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 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 504 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 1 ms 520 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 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 504 KB Output is correct
6 Correct 2 ms 336 KB Output is correct
7 Correct 27 ms 4868 KB Output is correct
8 Correct 30 ms 7360 KB Output is correct
9 Correct 28 ms 7220 KB Output is correct
10 Correct 31 ms 7096 KB Output is correct
11 Correct 28 ms 7224 KB Output is correct
12 Correct 29 ms 7368 KB Output is correct
13 Correct 30 ms 4808 KB Output is correct
14 Correct 32 ms 4808 KB Output is correct
15 Correct 30 ms 4820 KB Output is correct
16 Correct 30 ms 4808 KB Output is correct
17 Correct 31 ms 4800 KB Output is correct
18 Correct 28 ms 4800 KB Output is correct
19 Correct 2 ms 336 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 1 ms 520 KB Output is correct
23 Correct 2 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 39 ms 7472 KB Output is correct
26 Correct 37 ms 7616 KB Output is correct
27 Correct 39 ms 7616 KB Output is correct
28 Correct 36 ms 4996 KB Output is correct
29 Correct 40 ms 7368 KB Output is correct
30 Correct 38 ms 7360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 4808 KB Output is correct
2 Correct 32 ms 4808 KB Output is correct
3 Correct 30 ms 4820 KB Output is correct
4 Correct 30 ms 4808 KB Output is correct
5 Correct 31 ms 4800 KB Output is correct
6 Correct 28 ms 4800 KB Output is correct
7 Correct 64 ms 18100 KB Output is correct
8 Correct 64 ms 18100 KB Output is correct
9 Correct 128 ms 18352 KB Output is correct
10 Correct 70 ms 18100 KB Output is correct
11 Correct 72 ms 18240 KB Output is correct
12 Correct 73 ms 18112 KB Output is correct
13 Correct 85 ms 18100 KB Output is correct
14 Correct 68 ms 18100 KB Output is correct
15 Correct 78 ms 18184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 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 336 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 27 ms 4868 KB Output is correct
9 Correct 30 ms 7360 KB Output is correct
10 Correct 28 ms 7220 KB Output is correct
11 Correct 31 ms 7096 KB Output is correct
12 Correct 28 ms 7224 KB Output is correct
13 Correct 29 ms 7368 KB Output is correct
14 Correct 30 ms 4808 KB Output is correct
15 Correct 32 ms 4808 KB Output is correct
16 Correct 30 ms 4820 KB Output is correct
17 Correct 30 ms 4808 KB Output is correct
18 Correct 31 ms 4800 KB Output is correct
19 Correct 28 ms 4800 KB Output is correct
20 Correct 2 ms 336 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 1 ms 520 KB Output is correct
24 Correct 2 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 39 ms 7472 KB Output is correct
27 Correct 37 ms 7616 KB Output is correct
28 Correct 39 ms 7616 KB Output is correct
29 Correct 36 ms 4996 KB Output is correct
30 Correct 40 ms 7368 KB Output is correct
31 Correct 38 ms 7360 KB Output is correct
32 Correct 64 ms 18100 KB Output is correct
33 Correct 64 ms 18100 KB Output is correct
34 Correct 128 ms 18352 KB Output is correct
35 Correct 70 ms 18100 KB Output is correct
36 Correct 72 ms 18240 KB Output is correct
37 Correct 73 ms 18112 KB Output is correct
38 Correct 85 ms 18100 KB Output is correct
39 Correct 68 ms 18100 KB Output is correct
40 Correct 78 ms 18184 KB Output is correct
41 Incorrect 68 ms 18288 KB Output isn't correct
42 Halted 0 ms 0 KB -