Submission #426367

# Submission time Handle Problem Language Result Execution time Memory
426367 2021-06-13T20:16:58 Z JeanBombeur Dancing Elephants (IOI11_elephants) C++17
100 / 100
8833 ms 8552 KB
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include "elephants.h"
using namespace std;

const int INFINI = (1000 * 1000 * 1000 + 1);
const int MAX_ELEPHANTS = (150 * 1000);
const int LOG = (11);
const int SQRT = (2000);

int Positions[MAX_ELEPHANTS];

vector <int> Elephants[SQRT];

vector <pair <int, int>> Answer[SQRT];

int nbElephants, tailleIntervalle;

void UpdateBucket(int bucket) {
    int sz = Elephants[bucket].size();
    int droite = sz;
    for (int i = droite - 1; i >= 0; i --)
    {
        while (Elephants[bucket][droite - 1] > Elephants[bucket][i] + tailleIntervalle)
            droite --;
        if (droite == sz)
            Answer[bucket][i] = {1, Elephants[bucket][i] + tailleIntervalle};
        else
            Answer[bucket][i] = {1 + Answer[bucket][droite].first, Answer[bucket][droite].second};
    }
    return;
}

void Insert(int bucket, int val) {
    vector <int> nv;
    for (int a : Elephants[bucket])
    {
        if (a >= val)
        {
            nv.push_back(val);
            val = INFINI;
        }
        nv.push_back(a);
    }
    if (val < INFINI)
        nv.push_back(val);
    Elephants[bucket] = nv;
    Answer[bucket].push_back({0, 0});
    UpdateBucket(bucket);
    return;
}

void Remove(int bucket, int val) {
    vector <int> nv;
    for (int a : Elephants[bucket])
    {
        if (a == val)
            val = -1;
        else
            nv.push_back(a);
    }
    Elephants[bucket] = nv;
    UpdateBucket(bucket);
    return;
}

int FindNext(int bucket, int val) {
    int ans = -1;
    int sz = Elephants[bucket].size();
    for (int i = (1 << LOG); i > 0; i /= 2)
    {
        if (ans + i < sz && Elephants[bucket][ans + i] <= val)
            ans += i;
    }
    return ans + 1;
}

int FindAnswer() {
    int ans = 0;
    int curBucket = 0;
    int curId = 0;
    while (Elephants[curBucket].size() > 0)
    {
        ans += Answer[curBucket][curId].first;
        int saut = Answer[curBucket][curId].second;
        curId = FindNext(++ curBucket, saut);
        while (curId != 0 && curId == (int)Elephants[curBucket].size())
            curId = FindNext(++ curBucket, saut);
    }
    return ans;
}

void ResetBuckets() {
    vector <int> AllPos;
    int curBucket = 0;
    while (Elephants[curBucket].size() > 0)
    {
        for (int a : Elephants[curBucket])
            AllPos.push_back(a);
        curBucket ++;
    }
    for (int i = 0; i < curBucket; i ++)
    {
        Elephants[i].clear();
        Answer[i].clear();
    }
    curBucket = 0;
    for (int i = 0; i < nbElephants; i ++)
    {
        if ((int)Elephants[curBucket].size() > SQRT && (nbElephants - i) >= SQRT / 2)
            curBucket ++;
        Elephants[curBucket].push_back(AllPos[i]);
        Answer[curBucket].push_back({0, 0});
    }
    for (int i = 0; i <= curBucket; i ++)
    {
        UpdateBucket(i);
    }
    return;
}

void init(int N, int L, int X[]) {
    nbElephants = N;
    tailleIntervalle = L;
    for (int i = 0; i < nbElephants; i ++)
    {
        Positions[i] = X[i];
    }
    sort(X, X + nbElephants);
    int curBucket = 0;
    for (int i = 0; i < nbElephants; i ++)
    {
        if ((int)Elephants[curBucket].size() > SQRT && (nbElephants - i) >= SQRT / 2)
            curBucket ++;
        Elephants[curBucket].push_back(X[i]);
        Answer[curBucket].push_back({0, 0});
    }
    for (int i = 0; i <= curBucket; i ++)
    {
        UpdateBucket(i);
    }
    return;
}

int update(int id, int nouvPos) {
    
    int prev = Positions[id];
    Positions[id] = nouvPos;
    
    int curBucket = 0;
    bool reset = false;
    while ((int)Elephants[curBucket].size() > 0 && Elephants[curBucket].back() < prev)
        curBucket ++;
    if (Elephants[curBucket].size() == 0)
        curBucket --;
    Remove(curBucket, prev);
    if (Elephants[curBucket].size() == 1)
        reset = true;
    
    curBucket = 0;
    while ((int)Elephants[curBucket].size() > 0 && Elephants[curBucket].back() < nouvPos)
        curBucket ++;
    if (Elephants[curBucket].size() == 0)
        curBucket --;
    Insert(curBucket, nouvPos);
    if (Elephants[curBucket].size() >= 2 * SQRT)
        reset = true;
    
    if (reset)
        ResetBuckets();
    
    return FindAnswer();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1645 ms 1536 KB Output is correct
8 Correct 1528 ms 1880 KB Output is correct
9 Correct 1371 ms 2784 KB Output is correct
10 Correct 1423 ms 2500 KB Output is correct
11 Correct 1331 ms 2576 KB Output is correct
12 Correct 2378 ms 2716 KB Output is correct
13 Correct 1315 ms 2576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1645 ms 1536 KB Output is correct
8 Correct 1528 ms 1880 KB Output is correct
9 Correct 1371 ms 2784 KB Output is correct
10 Correct 1423 ms 2500 KB Output is correct
11 Correct 1331 ms 2576 KB Output is correct
12 Correct 2378 ms 2716 KB Output is correct
13 Correct 1315 ms 2576 KB Output is correct
14 Correct 1935 ms 2184 KB Output is correct
15 Correct 2099 ms 2708 KB Output is correct
16 Correct 3877 ms 3372 KB Output is correct
17 Correct 3648 ms 3868 KB Output is correct
18 Correct 3928 ms 4200 KB Output is correct
19 Correct 2017 ms 3420 KB Output is correct
20 Correct 3595 ms 4148 KB Output is correct
21 Correct 3561 ms 4424 KB Output is correct
22 Correct 1995 ms 3460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1645 ms 1536 KB Output is correct
8 Correct 1528 ms 1880 KB Output is correct
9 Correct 1371 ms 2784 KB Output is correct
10 Correct 1423 ms 2500 KB Output is correct
11 Correct 1331 ms 2576 KB Output is correct
12 Correct 2378 ms 2716 KB Output is correct
13 Correct 1315 ms 2576 KB Output is correct
14 Correct 1935 ms 2184 KB Output is correct
15 Correct 2099 ms 2708 KB Output is correct
16 Correct 3877 ms 3372 KB Output is correct
17 Correct 3648 ms 3868 KB Output is correct
18 Correct 3928 ms 4200 KB Output is correct
19 Correct 2017 ms 3420 KB Output is correct
20 Correct 3595 ms 4148 KB Output is correct
21 Correct 3561 ms 4424 KB Output is correct
22 Correct 1995 ms 3460 KB Output is correct
23 Correct 5914 ms 6464 KB Output is correct
24 Correct 7090 ms 6500 KB Output is correct
25 Correct 4592 ms 6628 KB Output is correct
26 Correct 6127 ms 6864 KB Output is correct
27 Correct 6138 ms 6936 KB Output is correct
28 Correct 6474 ms 4092 KB Output is correct
29 Correct 6491 ms 4016 KB Output is correct
30 Correct 6608 ms 4080 KB Output is correct
31 Correct 6520 ms 4156 KB Output is correct
32 Correct 4691 ms 6904 KB Output is correct
33 Correct 5113 ms 6968 KB Output is correct
34 Correct 4931 ms 6852 KB Output is correct
35 Correct 5393 ms 8552 KB Output is correct
36 Correct 4005 ms 7344 KB Output is correct
37 Correct 6639 ms 7100 KB Output is correct
38 Correct 4817 ms 7076 KB Output is correct
39 Correct 4897 ms 6824 KB Output is correct
40 Correct 4801 ms 6896 KB Output is correct
41 Correct 8576 ms 8232 KB Output is correct
42 Correct 8833 ms 7852 KB Output is correct