This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "doll.h"
#include <bits/stdc++.h>
using namespace std;
void answer(std::vector<int> C, std::vector<int> X, std::vector<int> Y);
void create_circuit(int m, vector<int>a) {
const int n = (int)a.size();
a.push_back(0);
vector<vector<int>>v(m+1);
vector<int>C(m+1);
C[0] = a[0];
vector<int>X,Y;
for(int i = 0 ; i < n ; i++) {
v[a[i]].push_back(a[i+1]);
}
int j = 1;
for(int i = 1 ; i <= m ; i++) {
//cout << i << ' ';
if((int)v[i].size() == 0)
continue;
if(v[i].size() == 1) {
C[i] = v[i][0];
continue;
}
vector<int>w;
if(1) {
int cnt = (v[i].size()&1) + 2*(((v[i].size()+1)/2)&1);
for(int ii = 0 ; ii < v[i].size()/2 ; ii++)
w.push_back(v[i][ii]);
for(int ii = 0 ; ii < cnt ; ii++)
w.push_back(-j);
for(int ii = v[i].size() / 2 ; ii < v[i].size() ; ii++)
w.push_back(v[i][ii]);
v[i] = w;
}
int p = 0;
while((1 << p) < (v[i].size())/2)
p++;
vector<int>st(8 * (v[i].size()+1)/2, 0);
vector<int>rev(8 * (v[i].size()+1)/2);
C[i] = -j;
//cout << i << ' ' << v[i].size() << '\n';
bool r = (v[i].size()/2)&1; // !!
for(int l = 0 ; l < v[i].size()/2 ; l++) {
//cerr << l << ' ';
int pos = 1, d = 0;
while(d <= p) {
if(d < p) {
if(!st[pos]) {
st[pos] = 1;
rev[pos] = j-1;
j++;
X.push_back(INT_MAX);
Y.push_back(INT_MAX);
}
if(st[pos] == 1) {
if(!st[2*pos]) {
X[rev[pos]] = (-(j));
}
st[pos] = 2;
pos *= 2;
} else {
if(!st[2*pos+1]) {
Y[rev[pos]] = (-(j));
}
st[pos] = 1;
pos *= 2;
pos++;
}
d++;
} else {
d++;
if(!st[pos]) {
st[pos] = 1;
rev[pos] = j-1;
j++;
X.push_back(INT_MAX);
Y.push_back(INT_MAX);
}
if(r && l == v[i].size()/2-1) {
X[rev[pos]] = -rev[pos]-1;
Y[rev[pos]] = v[i][l];
continue;
}
if(st[pos] == 1) {
X[rev[pos]] = v[i][l];
st[pos] = 2;
} else {
Y[rev[pos]] = v[i][l];
st[pos] = 1;
}
}
}
}
// probeer toch EEN baan te maken en dan vooraleer te beantwoorden alles te checken dat nog geen verbinding heeft en die naar zichzelf laten gaan ofwel X en Y switchen
bool t = (v[i].size()/2)&1;
for(int l = v[i].size()/2 ; l < v[i].size() ; l++) {
int pos = 1, d = 0;
while(d <= p) {
if(d < p) {
d++;
if(l == v[i].size()-1 && t && st[pos] == 0) {
st[pos] = 1;
rev[pos] = j-1;
j++;
X.push_back(-j);
Y.push_back(-rev[pos]-1);
pos *= 2;
continue;
}
if(l == v[i].size()-1 && t && st[2*pos+1] == 0) {
st[pos] = 1;
Y.push_back(-(j+1));
pos *= 2;
pos++;
continue;
}
if(st[pos] == 1) {
st[pos] = 2;
pos *= 2;
} else {
if(!st[2*pos+1]) {
st[pos] = 1;
Y[rev[pos]] = -rev[pos] - 1; // watch
pos *= 2;
continue;
}
st[pos] = 1;
pos *= 2;
pos++;
}
} else {
d++;
if(!st[pos]) {
st[pos] = 1;
rev[pos] = j-1;
j++;
X.push_back(-rev[pos]-1);
Y.push_back(v[i][l]);
continue;
}
if(st[pos] == 1) {
X[rev[pos]] = -rev[pos]-1;
st[pos] = 2;
}
st[pos] = 1;
Y[rev[pos]] = v[i][l];
}
}
}
}
for(int i = 0 ; i < (int)X.size() ; i++) {
if(Y[i] == INT_MAX)
Y[i] = -i-1, swap(X[i], Y[i]);
}
/*cerr << '\n';
for(auto z : a)
cerr << z << ' ';
cerr << '\n';
for(auto z : C)
cerr << z << ' ';
cerr << '\n';
for(auto z : X)
cerr << z << ' ';
cerr << '\n';
for(auto z : Y)
cerr << z << ' ';
cerr << '\n';*/
answer(C, X, Y);
}
Compilation message (stderr)
doll.cpp: In function 'void create_circuit(int, std::vector<int>)':
doll.cpp:39:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int ii = 0 ; ii < v[i].size()/2 ; ii++)
| ~~~^~~~~~~~~~~~~~~
doll.cpp:45:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int ii = v[i].size() / 2 ; ii < v[i].size() ; ii++)
| ~~~^~~~~~~~~~~~~
doll.cpp:51:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | while((1 << p) < (v[i].size())/2)
| ~~~~~~~~~^~~~~~~~~~~~~~~~~
doll.cpp:62:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int l = 0 ; l < v[i].size()/2 ; l++) {
| ~~^~~~~~~~~~~~~~~
doll.cpp:100:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | if(r && l == v[i].size()/2-1) {
| ~~^~~~~~~~~~~~~~~~~~
doll.cpp:120:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for(int l = v[i].size()/2 ; l < v[i].size() ; l++) {
| ~~^~~~~~~~~~~~~
doll.cpp:125:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | if(l == v[i].size()-1 && t && st[pos] == 0) {
| ~~^~~~~~~~~~~~~~~~
doll.cpp:134:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | if(l == v[i].size()-1 && t && st[2*pos+1] == 0) {
| ~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |