Submission #891665

# Submission time Handle Problem Language Result Execution time Memory
891665 2023-12-23T13:59:15 Z vjudge1 Mechanical Doll (IOI18_doll) C++17
100 / 100
128 ms 17472 KB
#include "doll.h"
#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;

vi X, Y;

vector<pair<int, int> > VP;

int gen(int niv, int nr) {
    int id = X.size();
    X.push_back(0);
    Y.push_back(0);
    //cout << niv << " " << nr << "\n";
    if(niv == 1 && nr == 2) {
        return -id - 1; 
    } else if(niv == 1 && nr == 1) {
        X[id] = -1;
        return -id - 1;
    }
    
    if(nr <= (1 << (niv - 1))) {
        int S = gen(niv - 1, nr);
        Y[id] = S;
        X[id] = -1;
        return -id - 1;
    } else {
        Y[id] = gen(niv - 1, (1 << (niv - 1)));
        X[id] = gen(niv - 1, nr - (1 << (niv - 1)));
        return -id - 1;
    }
    return -id - 1;

}

void create_circuit(int M, vi A) {
    A.push_back(0);
    map<int, int> Fr;
    int frmax = 0;
    for(auto it : A) {
        ++Fr[it];
        frmax = max(frmax, Fr[it]);
    }
    if(frmax <= 4) {
        int ult = 0;
        vector<vi> Urm(M + 1);
        for(int i = 0; i < A.size(); ++i) {
            Urm[ult].push_back(A[i]);
            ult = A[i];
        }
        vi C(M + 1, -1), X, Y;

        for(int i = 0; i <= M; ++i) {
            if(Urm[i].empty()) {
                C[i] = 0;
                continue;
            }
            if(Urm[i].size() == 1) {
                C[i] = Urm[i][0];
                continue;
            }
            if(Urm[i].size() == 2) {
                X.push_back(Urm[i][0]);
                Y.push_back(Urm[i][1]);
                C[i] = -X.size();
                continue;
            }
            if(Urm[i].size() == 3) {
                int id = X.size();
                X.push_back(-id-2);
                Y.push_back(-id-3);
                X.push_back(Urm[i][0]);
                Y.push_back(Urm[i][1]);
                X.push_back(-id - 1);
                Y.push_back(Urm[i][2]);
                C[i] = -id - 1;
                continue;
            }
            if(Urm[i].size() == 4) {
                int id = X.size();
                X.push_back(-id-2);
                Y.push_back(-id-3);
                X.push_back(Urm[i][0]);
                Y.push_back(Urm[i][2]);
                X.push_back(Urm[i][1]);
                Y.push_back(Urm[i][3]);
                C[i] = -id - 1;
                continue;
            }
            assert(0);
        }
       // cout << "C: ";
       // for(auto it : C)
       //     cout << it << " ";
       // cout << "\n";
       // cout << "X: ";
       // for(auto it : X)
       //     cout << it << " ";
       // cout << "\n";
       // cout << "Y: ";
       // for(auto it : Y)
       //     cout << it << " ";
       // cout << "\n";
        answer(C, X, Y);
    } else {
        int niv = 1;
        while((1 << niv) <= A.size()) ++niv;
        gen(niv, A.size());
        vi C(M + 1, -1);

        vi Stare(X.size(), 0);

        int u = -1, p = 0;
        ////cout << "ASSIGN\n";
        do {
            if(u > 0) {
                u = -1;
            }
            int id = -u - 1;
            if(!Stare[id]) {
                Stare[id] ^= 1;
                if(X[id]) u = X[id];
                else {
                    u = X[id] = A[p++];
                }
            } else {
                Stare[id] ^= 1;
                if(Y[id]) u = Y[id];
                else {
                    u = Y[id] = A[p++];
                }
            }
        } while(u != 0);

        int nr0 = 0;
        for(auto &it : X) nr0 += !it;
        for(auto &it : Y) nr0 += !it;

        //cout << "In total " << nr0 << "\n";


        //for(auto it : Stare)
            //cout << it << " ";
        //cout << "\n";

        //cout << "C: ";
        //for(auto it : C) 
            //cout << it << " ";
        //cout << "\n";
        //cout << "X: ";
        //for(auto it : X) 
            //cout << it << " ";
        //cout << "\n";
        //cout << "Y: ";
        //for(auto it : Y) 
            //cout << it << " ";
        //cout << "\n";
        answer(C, X, Y);
    }
}

Compilation message

doll.cpp: In function 'void create_circuit(int, vi)':
doll.cpp:49:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |         for(int i = 0; i < A.size(); ++i) {
      |                        ~~^~~~~~~~~~
doll.cpp:109:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |         while((1 << niv) <= A.size()) ++niv;
      |               ~~~~~~~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 40 ms 9560 KB Output is correct
3 Correct 36 ms 8268 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 8 ms 3884 KB Output is correct
6 Correct 58 ms 12492 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 40 ms 9560 KB Output is correct
3 Correct 36 ms 8268 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 8 ms 3884 KB Output is correct
6 Correct 58 ms 12492 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 67 ms 11344 KB Output is correct
9 Correct 81 ms 13644 KB Output is correct
10 Correct 110 ms 17472 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 40 ms 9560 KB Output is correct
3 Correct 36 ms 8268 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 8 ms 3884 KB Output is correct
6 Correct 58 ms 12492 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 67 ms 11344 KB Output is correct
9 Correct 81 ms 13644 KB Output is correct
10 Correct 110 ms 17472 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 115 ms 15524 KB Output is correct
15 Correct 61 ms 8776 KB Output is correct
16 Correct 101 ms 12604 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 116 ms 15948 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 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 0 ms 348 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 53 ms 5664 KB Output is correct
3 Correct 52 ms 5800 KB Output is correct
4 Correct 78 ms 8500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 53 ms 5664 KB Output is correct
3 Correct 52 ms 5800 KB Output is correct
4 Correct 78 ms 8500 KB Output is correct
5 Correct 128 ms 11604 KB Output is correct
6 Correct 121 ms 10660 KB Output is correct
7 Correct 123 ms 10972 KB Output is correct
8 Correct 115 ms 10032 KB Output is correct
9 Correct 51 ms 5744 KB Output is correct
10 Correct 93 ms 9448 KB Output is correct
11 Correct 96 ms 8792 KB Output is correct
12 Correct 61 ms 5772 KB Output is correct
13 Correct 76 ms 7028 KB Output is correct
14 Correct 80 ms 7292 KB Output is correct
15 Correct 81 ms 7664 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 61 ms 6380 KB Output is correct
18 Correct 60 ms 5876 KB Output is correct
19 Correct 60 ms 6004 KB Output is correct
20 Correct 103 ms 9308 KB Output is correct
21 Correct 96 ms 8748 KB Output is correct
22 Correct 99 ms 8940 KB Output is correct