Submission #303461

# Submission time Handle Problem Language Result Execution time Memory
303461 2020-09-20T10:31:35 Z ryansee Mechanical Doll (IOI18_doll) C++14
100 / 100
243 ms 17860 KB
#include "doll.h"
#include <bits/stdc++.h>
 
using namespace std;
 
typedef pair<int,int> pi;
 
int mlevel;
 
int cnt = 1;
int pow2[25];
int bigN;
int sequence[200005];
 
vector<int> X, Y;
 
struct switcher{
    switcher *left = nullptr, *right = nullptr;
    int level, id, lnode, rnode, state = 0;
    switcher(int n, int lv, int offset){
        id = cnt;
        cnt++;
        if (lv == 0){
            if (n == 2){
                lnode = 1;
                rnode = 1;
            }
            else{
                lnode = 1;
                rnode = -1;
            }
        }
        else{
            if (n > pow2[lv]){
                left = new switcher(n-pow2[lv],lv-1,offset);
                right = new switcher(pow2[lv],lv-1,offset+n-pow2[lv]);
                lnode = -left->id;
                rnode = -right->id;
            }
            else{
                right = new switcher(n,lv-1,offset);
                lnode = -1;
                rnode = -right->id;
            }
            
        } 
    }
    void revisit(switcher *r){
        if (lnode == -1){
            left = r;
        }
        if (rnode == -1){
            right = r;
        }
 
        if (left != nullptr && left != r){
            left->revisit(r);
        }
        if (right != r && right != nullptr){
            right->revisit(r);
        }
    }
}*root;
 
void dfs(switcher *cur, int nx){
    if (nx > bigN) return;
    if (cur->state == 0){
        cur->state = 1;
        if (cur->left == nullptr){
            cur->lnode = sequence[nx];
            X[cur->id-1] = sequence[nx];
            dfs(root,nx+1);
        }
        else{
            X[cur->id-1] = -cur->left->id;
            dfs(cur->left,nx);
        }
    }
    else{
        cur->state = 0;
        if (cur->right == nullptr){
            cur->lnode = sequence[nx];
            Y[cur->id-1] = sequence[nx];
            dfs(root,nx+1);
        }
        else{
            Y[cur->id-1] = -cur->right->id;
            dfs(cur->right,nx);
        }
    }
}
 
void create_circuit(int M, vector<int> A) {
    int N = A.size();
    bigN = N;
    vector<int> C;
    C.resize(M+1);
    if (N == 1){
        C[0] = A[0];
        C[1] = 0;
        answer(C,X,Y);
        return;
    }
    pow2[0] = 1;
    for (int i = 1; i < 25; ++i){
        pow2[i] = pow2[i-1]*2;
    }
    C[0] = A[0];
    for (int i = 1; i <= M; ++i){
        C[i] = -1;
    }
    for (int i = 0; i < A.size(); ++i){
        if (i == A.size()-1){
            sequence[i+1] = 0;
        }
        else sequence[i+1] = A[i+1];
    }
    //cout << "YEET\n" << flush;
    int x = 1, p = 0;
    while (x < N){
        p++;
        x *= 2;
    }
    root = new switcher(N,p-1,1);
    //cout << "YEET\n" << flush;
    X.resize(cnt);
    Y.resize(cnt);
    //cout << "YEET\n" << flush;
    root->revisit(root);
    //cout << "YEET\n" << flush;
    switcher *a = root;
    //cout << "YEET\n" << flush;
    dfs(a,1);
    //cout << "YEET\n" << flush;
    answer(C, X, Y);
}

Compilation message

doll.cpp: In function 'void create_circuit(int, std::vector<int>)':
doll.cpp:112:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |     for (int i = 0; i < A.size(); ++i){
      |                     ~~^~~~~~~~~~
doll.cpp:113:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |         if (i == A.size()-1){
      |             ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 52 ms 6876 KB Output is correct
3 Correct 58 ms 6488 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 12 ms 1356 KB Output is correct
6 Correct 93 ms 9660 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 52 ms 6876 KB Output is correct
3 Correct 58 ms 6488 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 12 ms 1356 KB Output is correct
6 Correct 93 ms 9660 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 106 ms 11828 KB Output is correct
9 Correct 108 ms 12232 KB Output is correct
10 Correct 185 ms 17860 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 52 ms 6876 KB Output is correct
3 Correct 58 ms 6488 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 12 ms 1356 KB Output is correct
6 Correct 93 ms 9660 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 106 ms 11828 KB Output is correct
9 Correct 108 ms 12232 KB Output is correct
10 Correct 185 ms 17860 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 200 ms 17504 KB Output is correct
15 Correct 123 ms 11464 KB Output is correct
16 Correct 240 ms 17288 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 4 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 199 ms 17632 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 5 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 129 ms 10976 KB Output is correct
3 Correct 143 ms 11076 KB Output is correct
4 Correct 190 ms 16668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 129 ms 10976 KB Output is correct
3 Correct 143 ms 11076 KB Output is correct
4 Correct 190 ms 16668 KB Output is correct
5 Correct 198 ms 17228 KB Output is correct
6 Correct 226 ms 17300 KB Output is correct
7 Correct 243 ms 17384 KB Output is correct
8 Correct 205 ms 17052 KB Output is correct
9 Correct 110 ms 11076 KB Output is correct
10 Correct 231 ms 16904 KB Output is correct
11 Correct 179 ms 16744 KB Output is correct
12 Correct 108 ms 11096 KB Output is correct
13 Correct 127 ms 11372 KB Output is correct
14 Correct 135 ms 11416 KB Output is correct
15 Correct 133 ms 11552 KB Output is correct
16 Correct 4 ms 588 KB Output is correct
17 Correct 103 ms 11064 KB Output is correct
18 Correct 110 ms 11076 KB Output is correct
19 Correct 111 ms 11064 KB Output is correct
20 Correct 178 ms 16764 KB Output is correct
21 Correct 231 ms 16676 KB Output is correct
22 Correct 201 ms 16708 KB Output is correct