Submission #1106273

# Submission time Handle Problem Language Result Execution time Memory
1106273 2024-10-29T17:23:31 Z attky Nile (IOI24_nile) C++17
100 / 100
118 ms 39356 KB
#include "nile.h"
#include <bits/stdc++.h>
#define int long long 

using namespace std;

const int INF = 2e17;
const int MAX = 1e5+42;

struct Requ {
    int requete, id;

    bool operator< (Requ other) {
        return requete < other.requete;
    }
};

struct Comp {
    int pere, cout, minPair, minImpair, minSaut, taille, iPremier;

    bool operator< (Comp other) {
        return taille < other.taille;
    }
};

struct Dist {
    int ecart, id, type;

    bool operator< (Dist other) {
        return ecart < other.ecart;
    }
};

struct Reli {
    int poids, coutSeul, coutDeux, diffCout;

    bool operator< (Reli other) {
        return poids < other.poids;
    }
};

vector<Reli> relique;
vector<Requ> requete;
vector<Comp> composante(MAX);
vector<Dist> dist;
vector<int> answer;
vector<int> fils[MAX];
int sommeTotal = 0;


void merge(Dist arete) {
    int nodeA = composante[arete.id].pere;
    int nodeB = composante[arete.id + 1].pere;

    if(composante[nodeA] < composante[nodeB]) {
        swap(nodeA, nodeB);
    }
    for(int loop = 0; loop < fils[nodeB].size(); ++loop) {
        composante[fils[nodeB][loop]].pere = nodeA;
        fils[nodeA].push_back(fils[nodeB][loop]);
    }
    composante[nodeA].taille = fils[nodeA].size();
    composante[nodeB].taille = 0;
    fils[nodeB].clear();
    composante[nodeA].iPremier = min(composante[nodeA].iPremier, composante[nodeB].iPremier);
    composante[nodeA].minImpair = min(composante[nodeA].minImpair, composante[nodeB].minImpair);
    composante[nodeA].minPair = min(composante[nodeA].minPair, composante[nodeB].minPair);
    composante[nodeA].minSaut = min(composante[nodeA].minSaut, composante[nodeB].minSaut);

    sommeTotal -= composante[nodeA].cout + composante[nodeB].cout;
    if(composante[nodeA].taille % 2 == 0) {
        composante[nodeA].cout = 0;
    }
    else {
        int coutMin = composante[nodeA].minSaut;
        if(composante[nodeA].iPremier % 2 == 0) {
            composante[nodeA].cout = min(coutMin, composante[nodeA].minPair);
        }
        else {
            composante[nodeA].cout = min(coutMin, composante[nodeA].minImpair);
        }
    }
    sommeTotal += composante[nodeA].cout;
}

void update(Dist arete) {
    int node = composante[arete.id].pere;
    composante[node].minSaut = min(composante[node].minSaut, relique[arete.id].diffCout);

    sommeTotal -= composante[node].cout;
    composante[node].cout = min(composante[node].cout, composante[node].minSaut);
    sommeTotal += composante[node].cout;
}

#undef int long long 
vector<long long> calculate_costs(vector<int> poids, vector<int> coutSeul, vector<int> coutDeux, vector<int> E) {
    int n = poids.size();

    for(int loop = 0; loop < n; ++loop) {
        relique.push_back({poids[loop], coutSeul[loop], coutDeux[loop], coutSeul[loop] - coutDeux[loop]});        
    }
    sort(relique.begin(), relique.end());

    int q = E.size();
    for(int loop = 0; loop < q; ++loop) {
        answer.push_back(0);
        requete.push_back({0, 0});
    }
    for(int loop = 0; loop < q; ++loop) {
        requete[loop] = {E[loop], loop};
    }
    sort(requete.begin(), requete.end());

    for(int loop = 0; loop < n; ++loop) {
        composante[loop].pere = loop;
        composante[loop].cout = relique[loop].diffCout;
        composante[loop].minSaut = INF;
        composante[loop].taille = 1;
        composante[loop].iPremier = loop;
        if(loop % 2 == 0) {
            composante[loop].minPair = relique[loop].diffCout;
            composante[loop].minImpair = INF;
        }
        else {
            composante[loop].minPair = INF;
            composante[loop].minImpair = relique[loop].diffCout;
        }
    }

    for(int loop = 0; loop < n-1; ++loop) {
        dist.push_back({relique[loop+1].poids - relique[loop].poids, loop, 1});
    }
    for(int loop = 0; loop < n-2; ++loop) {
        dist.push_back({relique[loop+2].poids - relique[loop].poids, loop+1, 2});
    }
    dist.push_back({INF, -1, -1});
    sort(dist.begin(), dist.end());

    for(int loop = 0; loop < n; ++loop) {
        fils[loop].push_back(loop);
    }

    for(int loop = 0; loop < n; ++loop) {
        sommeTotal += relique[loop].coutSeul;
    }
    int iEcart = 0;
    for(int loop = 0; loop < q; ++loop) {
        while(dist[iEcart].ecart <= requete[loop].requete) {
            if(dist[iEcart].type == 1) {
                merge(dist[iEcart]);
            }
            else {
                update(dist[iEcart]);
            }
            iEcart++;
        }
        answer[requete[loop].id] = sommeTotal;
    }
    return answer;
}

Compilation message

nile.cpp:95:12: warning: extra tokens at end of #undef directive
   95 | #undef int long long
      |            ^~~~
nile.cpp: In function 'void merge(Dist)':
nile.cpp:58:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     for(int loop = 0; loop < fils[nodeB].size(); ++loop) {
      |                       ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8528 KB Output is correct
2 Correct 4 ms 8528 KB Output is correct
3 Correct 4 ms 8528 KB Output is correct
4 Correct 4 ms 8528 KB Output is correct
5 Correct 3 ms 8528 KB Output is correct
6 Correct 3 ms 8544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 30916 KB Output is correct
2 Correct 46 ms 31152 KB Output is correct
3 Correct 54 ms 31148 KB Output is correct
4 Correct 53 ms 31152 KB Output is correct
5 Correct 52 ms 31152 KB Output is correct
6 Correct 59 ms 31152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 28076 KB Output is correct
2 Correct 47 ms 28080 KB Output is correct
3 Correct 62 ms 33456 KB Output is correct
4 Correct 62 ms 33196 KB Output is correct
5 Correct 58 ms 31396 KB Output is correct
6 Correct 64 ms 32944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8528 KB Output is correct
2 Correct 4 ms 8528 KB Output is correct
3 Correct 4 ms 8528 KB Output is correct
4 Correct 4 ms 8528 KB Output is correct
5 Correct 3 ms 8528 KB Output is correct
6 Correct 3 ms 8544 KB Output is correct
7 Correct 5 ms 8528 KB Output is correct
8 Correct 3 ms 8544 KB Output is correct
9 Correct 6 ms 8528 KB Output is correct
10 Correct 4 ms 8528 KB Output is correct
11 Correct 3 ms 8528 KB Output is correct
12 Correct 4 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8528 KB Output is correct
2 Correct 4 ms 8528 KB Output is correct
3 Correct 4 ms 8528 KB Output is correct
4 Correct 4 ms 8528 KB Output is correct
5 Correct 3 ms 8528 KB Output is correct
6 Correct 3 ms 8544 KB Output is correct
7 Correct 46 ms 30916 KB Output is correct
8 Correct 46 ms 31152 KB Output is correct
9 Correct 54 ms 31148 KB Output is correct
10 Correct 53 ms 31152 KB Output is correct
11 Correct 52 ms 31152 KB Output is correct
12 Correct 59 ms 31152 KB Output is correct
13 Correct 46 ms 28076 KB Output is correct
14 Correct 47 ms 28080 KB Output is correct
15 Correct 62 ms 33456 KB Output is correct
16 Correct 62 ms 33196 KB Output is correct
17 Correct 58 ms 31396 KB Output is correct
18 Correct 64 ms 32944 KB Output is correct
19 Correct 5 ms 8528 KB Output is correct
20 Correct 3 ms 8544 KB Output is correct
21 Correct 6 ms 8528 KB Output is correct
22 Correct 4 ms 8528 KB Output is correct
23 Correct 3 ms 8528 KB Output is correct
24 Correct 4 ms 8528 KB Output is correct
25 Correct 51 ms 29544 KB Output is correct
26 Correct 56 ms 29516 KB Output is correct
27 Correct 67 ms 34480 KB Output is correct
28 Correct 70 ms 35260 KB Output is correct
29 Correct 76 ms 31404 KB Output is correct
30 Correct 79 ms 35248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 28076 KB Output is correct
2 Correct 47 ms 28080 KB Output is correct
3 Correct 62 ms 33456 KB Output is correct
4 Correct 62 ms 33196 KB Output is correct
5 Correct 58 ms 31396 KB Output is correct
6 Correct 64 ms 32944 KB Output is correct
7 Correct 69 ms 32696 KB Output is correct
8 Correct 76 ms 32684 KB Output is correct
9 Correct 114 ms 37552 KB Output is correct
10 Correct 112 ms 37808 KB Output is correct
11 Correct 84 ms 38288 KB Output is correct
12 Correct 89 ms 37804 KB Output is correct
13 Correct 84 ms 37804 KB Output is correct
14 Correct 70 ms 34988 KB Output is correct
15 Correct 118 ms 37820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8272 KB Output is correct
2 Correct 4 ms 8528 KB Output is correct
3 Correct 4 ms 8528 KB Output is correct
4 Correct 4 ms 8528 KB Output is correct
5 Correct 4 ms 8528 KB Output is correct
6 Correct 3 ms 8528 KB Output is correct
7 Correct 3 ms 8544 KB Output is correct
8 Correct 46 ms 30916 KB Output is correct
9 Correct 46 ms 31152 KB Output is correct
10 Correct 54 ms 31148 KB Output is correct
11 Correct 53 ms 31152 KB Output is correct
12 Correct 52 ms 31152 KB Output is correct
13 Correct 59 ms 31152 KB Output is correct
14 Correct 46 ms 28076 KB Output is correct
15 Correct 47 ms 28080 KB Output is correct
16 Correct 62 ms 33456 KB Output is correct
17 Correct 62 ms 33196 KB Output is correct
18 Correct 58 ms 31396 KB Output is correct
19 Correct 64 ms 32944 KB Output is correct
20 Correct 5 ms 8528 KB Output is correct
21 Correct 3 ms 8544 KB Output is correct
22 Correct 6 ms 8528 KB Output is correct
23 Correct 4 ms 8528 KB Output is correct
24 Correct 3 ms 8528 KB Output is correct
25 Correct 4 ms 8528 KB Output is correct
26 Correct 51 ms 29544 KB Output is correct
27 Correct 56 ms 29516 KB Output is correct
28 Correct 67 ms 34480 KB Output is correct
29 Correct 70 ms 35260 KB Output is correct
30 Correct 76 ms 31404 KB Output is correct
31 Correct 79 ms 35248 KB Output is correct
32 Correct 69 ms 32696 KB Output is correct
33 Correct 76 ms 32684 KB Output is correct
34 Correct 114 ms 37552 KB Output is correct
35 Correct 112 ms 37808 KB Output is correct
36 Correct 84 ms 38288 KB Output is correct
37 Correct 89 ms 37804 KB Output is correct
38 Correct 84 ms 37804 KB Output is correct
39 Correct 70 ms 34988 KB Output is correct
40 Correct 118 ms 37820 KB Output is correct
41 Correct 74 ms 34732 KB Output is correct
42 Correct 75 ms 34220 KB Output is correct
43 Correct 92 ms 39084 KB Output is correct
44 Correct 92 ms 39356 KB Output is correct
45 Correct 95 ms 39312 KB Output is correct
46 Correct 110 ms 39340 KB Output is correct
47 Correct 92 ms 38964 KB Output is correct
48 Correct 78 ms 36764 KB Output is correct
49 Correct 94 ms 38572 KB Output is correct