Submission #1022073

# Submission time Handle Problem Language Result Execution time Memory
1022073 2024-07-13T09:36:27 Z ZanP Mechanical Doll (IOI18_doll) C++17
85.5534 / 100
78 ms 19088 KB
#include "doll.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
void answer(vector<int> C, vector<int> X, vector<int> Y);
struct xyswitch {
    int x, y;
    int par = 0;
    bool gox = true;
    bool active = true;
    xyswitch() {}
    xyswitch(int x, int y) { this->x = x; this->y = y; }
};
vector<xyswitch> xyswitches;

void make_switches(int m, int filler, int base_switch) {
    int f1 = min(filler, (m + filler) / 2), f2 = filler - f1;
    int s1 = ((m + filler) / 2) - f1, s2 = ((m + filler) / 2) - f2;

    xyswitches.push_back(xyswitch());
    int sn = xyswitches.size() - 1;
    if (s1 == 0) { xyswitches[sn].x = -base_switch; }
    else if (s1 == 1 && f1 == 0) {
        xyswitches[sn].x = 0;
    }
    else {
        xyswitches[sn].x = -((int)xyswitches.size() + 1);
        make_switches(s1, f1, base_switch);
        xyswitches[-(xyswitches[sn].x) - 1].par = -(sn + 1);
    }

    if (s2 == 1 && f2 == 0) {
        xyswitches[sn].y = 0;
    }
    else {
        xyswitches[sn].y = -((int)xyswitches.size() + 1);
        make_switches(s2, f2, base_switch);
        xyswitches[-(xyswitches[sn].y) - 1].par = -(sn + 1);
    }
}

void connect(int val, int swid)
{
    xyswitch & sw = xyswitches[-(swid)-1];
    if (sw.gox)
    {
        sw.gox = !sw.gox;
        if (sw.x == 0) { sw.x = val; }
        else { connect(val, sw.x); }
    }
    else
    {
        sw.gox = !sw.gox;
        if (sw.y == 0) { sw.y = val; }
        else { connect(val, sw.y); }
    }
}


    

void create_circuit(int M, std::vector<int> A) {
    int n = A.size();
    vector<vector<int>> repetitions;
    repetitions.resize(M+1);
    vector<int> c(M+1,0);
    for (int i = 0; i < n - 1; i++) {
        repetitions[A[i]].push_back(A[i + 1]);
    }
    repetitions[A[n - 1]].push_back(0);

    for (int i = 1; i <= M; i++)
    {
        int m = repetitions[i].size(), mxtwo = 1;
        while (mxtwo < m) { mxtwo *= 2; }
        if (m == 0) { continue; }
        if (m == 1) {
            c[i] = repetitions[i][0];
        }
        else {
            c[i] = -(int)xyswitches.size() - 1;
            make_switches(repetitions[i].size(), mxtwo - m, xyswitches.size() + 1);
            for (int j = 0; j < repetitions[i].size(); j++) {
                connect(repetitions[i][j], c[i]);
            }
        }
    }

    vector<int> sums(xyswitches.size()); int sum = 0;
    for (int i = xyswitches.size() - 1; i >= 0; i--) {
        xyswitch & sw = xyswitches[i];
        if (sw.x == sw.y) {
            sw.active = false;
            sum++;
            if (sw.par != 0) {
                xyswitch& swpar = xyswitches[-(sw.par) - 1];
                if (swpar.x == -(i + 1)) { swpar.x = sw.x; }
                if (swpar.y == -(i + 1)) { swpar.y = sw.y; }
            }
        }
        sums[i] = sum;
    }

    for (int i = xyswitches.size() - 1; i >= 0; i--) {
        xyswitch& sw = xyswitches[i];
        if (sw.x < 0) {
            int xid = -(sw.x) - 1;
            int xdif = sum - sums[xid];
            sw.x = -(xid - xdif) - 1;
        }
        if (sw.y < 0) {
            int yid = -(sw.y) - 1;
            int ydif = sum - sums[yid];
            sw.y = -(yid - ydif) - 1;
        }

    }

    for (int i = 0; i < M+1; i++) {
        if (c[i] < 0) {
            int cid = -(c[i]) - 1;
            if (!xyswitches[cid].active) { c[i] = xyswitches[cid].x; }
            else {
                int cdif = sum - sums[cid];
                c[i] = -(cid - cdif) - 1;
            }
        }
    }

    int s = xyswitches.size();
    vector<int> x, y;
    x.reserve(s);
    y.reserve(s);
    c[0] = A[0];
    for (int i = 0; i < s; i++) {
        if (!xyswitches[i].active)continue;
        x.push_back(xyswitches[i].x);
        y.push_back(xyswitches[i].y);
    }
    answer(c, x, y);
}

Compilation message

doll.cpp: In function 'void create_circuit(int, std::vector<int>)':
doll.cpp:85:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |             for (int j = 0; j < repetitions[i].size(); j++) {
      |                             ~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 19 ms 6828 KB Output is correct
3 Correct 14 ms 5724 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 10 ms 3832 KB Output is correct
6 Correct 21 ms 8440 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 19 ms 6828 KB Output is correct
3 Correct 14 ms 5724 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 10 ms 3832 KB Output is correct
6 Correct 21 ms 8440 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 35 ms 9460 KB Output is correct
9 Correct 33 ms 10452 KB Output is correct
10 Correct 48 ms 14384 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 19 ms 6828 KB Output is correct
3 Correct 14 ms 5724 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 10 ms 3832 KB Output is correct
6 Correct 21 ms 8440 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 35 ms 9460 KB Output is correct
9 Correct 33 ms 10452 KB Output is correct
10 Correct 48 ms 14384 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 58 ms 17028 KB Output is correct
15 Correct 37 ms 8764 KB Output is correct
16 Correct 49 ms 13144 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 54 ms 15096 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 29 ms 5356 KB Output is correct
3 Correct 34 ms 7100 KB Output is correct
4 Correct 51 ms 8272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 29 ms 5356 KB Output is correct
3 Correct 34 ms 7100 KB Output is correct
4 Correct 51 ms 8272 KB Output is correct
5 Partially correct 70 ms 19088 KB Output is partially correct
6 Partially correct 73 ms 18840 KB Output is partially correct
7 Partially correct 78 ms 18956 KB Output is partially correct
8 Partially correct 57 ms 17816 KB Output is partially correct
9 Correct 42 ms 7556 KB Output is correct
10 Correct 62 ms 14692 KB Output is correct
11 Partially correct 51 ms 15500 KB Output is partially correct
12 Partially correct 42 ms 10272 KB Output is partially correct
13 Partially correct 41 ms 12340 KB Output is partially correct
14 Partially correct 42 ms 12388 KB Output is partially correct
15 Partially correct 41 ms 12632 KB Output is partially correct
16 Partially correct 1 ms 604 KB Output is partially correct
17 Partially correct 30 ms 9916 KB Output is partially correct
18 Partially correct 38 ms 9912 KB Output is partially correct
19 Partially correct 32 ms 9924 KB Output is partially correct
20 Partially correct 51 ms 15408 KB Output is partially correct
21 Partially correct 47 ms 15032 KB Output is partially correct
22 Partially correct 47 ms 14768 KB Output is partially correct