CSES - APIO 2007 - Results
Submission details
Task:Mobiles
Sender:henrikaalto
Submission time:2019-03-07 15:35:04 +0200
Language:C++
Status:READY
Result:0
Feedback
groupverdictscore
#10
Test results
testverdicttime
#1ACCEPTED0.02 sdetails
#2ACCEPTED0.02 sdetails
#3ACCEPTED0.02 sdetails
#4ACCEPTED0.03 sdetails
#5ACCEPTED0.03 sdetails
#6ACCEPTED0.03 sdetails
#7ACCEPTED0.02 sdetails
#8ACCEPTED0.02 sdetails
#9ACCEPTED0.02 sdetails
#10ACCEPTED0.04 sdetails
#11ACCEPTED0.02 sdetails
#12ACCEPTED0.02 sdetails
#13ACCEPTED0.02 sdetails
#14ACCEPTED0.02 sdetails
#15ACCEPTED0.02 sdetails
#16ACCEPTED0.01 sdetails
#17ACCEPTED0.02 sdetails
#180.02 sdetails
#190.01 sdetails
#200.01 sdetails
#210.01 sdetails
#22ACCEPTED0.02 sdetails
#230.02 sdetails
#240.04 sdetails
#250.08 sdetails
#26ACCEPTED0.01 sdetails
#27ACCEPTED0.01 sdetails
#280.03 sdetails
#29ACCEPTED0.02 sdetails
#30ACCEPTED0.03 sdetails
#31ACCEPTED0.03 sdetails
#32ACCEPTED0.04 sdetails
#33ACCEPTED0.08 sdetails
#34ACCEPTED0.07 sdetails
#35ACCEPTED0.07 sdetails
#36ACCEPTED0.07 sdetails
#37ACCEPTED0.07 sdetails
#38ACCEPTED0.06 sdetails
#39ACCEPTED0.05 sdetails
#40ACCEPTED0.06 sdetails
#41ACCEPTED0.06 sdetails
#42ACCEPTED0.05 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:76:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=1;i<w.size();i++) {
               ~^~~~~~~~~

Code

#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
void fail(){
  cout<<-1;
  exit(0);
}
pair<int,int> comb(pair<int,int> a,pair<int,int> b) {
  return (pair<int,int>){min(a.f,b.f),max(a.s,b.s)};
}

const int N = 101010;
vector<int> v[N];
int p[N];
pair<int,int> mm[N];
int steps;

void h(int s,int dep=1) {
  pair<int,int> c = (pair<int,int>){1e9,0};
  for(auto u:v[s]) {
    if(u==-1) {
      c = comb(c,(pair<int,int>){dep+1,dep+1});
      continue;
    }
    h(u,dep+1);
    c = comb(c,mm[u]);
  }
  mm[s] = c;
}

void h1(int s,int dep=1) {
  pair<int,int> left = (pair<int,int>){dep+1,dep+1};
  if(v[s][0] != -1) left = mm[v[s][0]];
  pair<int,int> right = (pair<int,int>){dep+1,dep+1};
  if(v[s][1] != -1) right = mm[v[s][1]];
  if(right.f>left.f || left.s < right.s){
    swap(v[s][0],v[s][1]);
    steps++;
  }
  for(auto u:v[s]) {
    if(u==-1) {
      continue;
    }
    h1(u,dep+1);
  }
}
vector<int> w;
void h2(int s,int dep=1) {
  for(auto u:v[s]) {
    if(u==-1) {
      w.push_back(dep+1);
      continue;
    }
    h2(u,dep+1);
  }
}


int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  int n; cin >> n;
  for(int i=1;i<=n;i++) {
    int a,b; cin >> a >> b;
    v[i].push_back(a);
    v[i].push_back(b);
  }
  h(1);
  for(int i=1;i<=n;i++) {
    if(p[i]) {
      if(!(p[i-1] || p[i+1]))
        fail();
    }
  }
  h1(1); h2(1);
  for(int i=1;i<w.size();i++) {
    if(w[i-1]<w[i]) fail();
  }
  cout << steps << "\n";
}

Test details

Test 1

Verdict: ACCEPTED

input
1
-1 -1

correct output
0

user output
0

Test 2

Verdict: ACCEPTED

input
2
2 -1
-1 -1

correct output
0

user output
0

Test 3

Verdict: ACCEPTED

input
2
-1 2
-1 -1

correct output
1

user output
1

Test 4

Verdict: ACCEPTED

input
4
2 3
4 -1
-1 -1
-1 -1

correct output
0

user output
0

Test 5

Verdict: ACCEPTED

input
4
2 3
-1 4
-1 -1
-1 -1

correct output
1

user output
1

Test 6

Verdict: ACCEPTED

input
4
2 3
-1 -1
4 -1
-1 -1

correct output
1

user output
1

Test 7

Verdict: ACCEPTED

input
4
2 3
-1 -1
-1 4
-1 -1

correct output
2

user output
2

Test 8

Verdict: ACCEPTED

input
5
2 3
4 5
-1 -1
-1 -1
...

correct output
0

user output
0

Test 9

Verdict: ACCEPTED

input
5
2 3
4 -1
5 -1
-1 -1
...

correct output
-1

user output
-1

Test 10

Verdict: ACCEPTED

input
5
2 3
4 -1
-1 5
-1 -1
...

correct output
-1

user output
-1

Test 11

Verdict: ACCEPTED

input
5
2 3
-1 4
5 -1
-1 -1
...

correct output
-1

user output
-1

Test 12

Verdict: ACCEPTED

input
5
2 3
-1 4
-1 5
-1 -1
...

correct output
-1

user output
-1

Test 13

Verdict: ACCEPTED

input
5
2 3
-1 -1
4 5
-1 -1
...

correct output
1

user output
1

Test 14

Verdict: ACCEPTED

input
6
2 3
4 5
6 -1
-1 -1
...

correct output
0

user output
0

Test 15

Verdict: ACCEPTED

input
6
2 3
4 5
-1 6
-1 -1
...

correct output
1

user output
1

Test 16

Verdict: ACCEPTED

input
6
2 3
4 -1
5 6
-1 -1
...

correct output
1

user output
1

Test 17

Verdict: ACCEPTED

input
6
2 3
-1 4
5 6
-1 -1
...

correct output
2

user output
2

Test 18

Verdict:

input
19
2 3
6 5
4 7
-1 -1
...

correct output
-1

user output
0

Test 19

Verdict:

input
9
3 2
4 5
-1 -1
6 9
...

correct output
-1

user output
1

Test 20

Verdict:

input
4
-1 2
3 4
-1 -1
-1 -1

correct output
-1

user output
1

Test 21

Verdict:

input
12
3 2
4 5
-1 6
7 9
...

correct output
-1

user output
2

Test 22

Verdict: ACCEPTED

input
10
3 2
7 5
4 6
8 10
...

correct output
-1

user output
-1

Test 23

Verdict:

input
1000
2 -1
-1 3
4 -1
5 -1
...

correct output
-1

user output
507

Test 24

Verdict:

input
10000
2 -1
-1 3
4 -1
5 -1
...

correct output
-1

user output
5086

Test 25

Verdict:

input
100000
2 -1
-1 3
4 -1
5 -1
...

correct output
-1

user output
50244

Test 26

Verdict: ACCEPTED

input
10
2 3
6 5
7 4
-1 -1
...

correct output
2

user output
2

Test 27

Verdict: ACCEPTED

input
18
2 3
7 6
5 4
11 9
...

correct output
3

user output
3

Test 28

Verdict:

input
13
3 2
4 5
7 6
-1 -1
...

correct output
-1

user output
4

Test 29

Verdict: ACCEPTED

input
660
3 2
5 6
7 4
10 8
...

correct output
-1

user output
-1

Test 30

Verdict: ACCEPTED

input
1250
2 3
6 7
5 4
10 14
...

correct output
7

user output
7

Test 31

Verdict: ACCEPTED

input
5000
2 3
6 7
5 4
11 14
...

correct output
4

user output
4

Test 32

Verdict: ACCEPTED

input
32767
2 3
5 4
7 6
13 14
...

correct output
0

user output
0

Test 33

Verdict: ACCEPTED

input
100000
2 3
5 7
4 6
15 14
...

correct output
7

user output
7

Test 34

Verdict: ACCEPTED

input
98348
3 2
7 5
6 4
10 8
...

correct output
-1

user output
-1

Test 35

Verdict: ACCEPTED

input
100000
2 3
5 7
4 6
15 14
...

correct output
7

user output
7

Test 36

Verdict: ACCEPTED

input
99999
3 2
7 5
6 4
10 8
...

correct output
5

user output
5

Test 37

Verdict: ACCEPTED

input
98348
3 2
7 5
6 4
10 8
...

correct output
-1

user output
-1

Test 38

Verdict: ACCEPTED

input
98303
3 2
5 4
7 6
9 8
...

correct output
1

user output
1

Test 39

Verdict: ACCEPTED

input
98304
3 2
5 4
7 6
9 8
...

correct output
16

user output
16

Test 40

Verdict: ACCEPTED

input
99989
3 2
5 4
7 6
9 8
...

correct output
15

user output
15

Test 41

Verdict: ACCEPTED

input
99989
2 3
4 5
6 7
8 9
...

correct output
15

user output
15

Test 42

Verdict: ACCEPTED

input
100000
3 2
5 4
7 6
9 8
...

correct output
16

user output
16