CSES - Datatähti 2017 alku - Results
Submission details
Task:Järjestys
Sender:yhyy
Submission time:2016-10-03 15:48:44 +0300
Language:C++
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED19
#2ACCEPTED37
#3ACCEPTED44
Test results
testverdicttimegroup
#1ACCEPTED0.05 s1details
#2ACCEPTED0.05 s2details
#3ACCEPTED0.19 s3details

Code

#include <bits/stdc++.h>

using namespace std;

struct node{
  int prior, val, size, m;
  bool c;
  node* left;
  node* right;
  node(int v){
    val = v, size = 1, c = 0, m=v;
    left=nullptr;
    right=nullptr;
    prior=rand();
  }
};

int cnt(node* t){
  if(t==nullptr) return 0;
  else return t->size;
}
int M(node* t){
  if(t==nullptr) return 0;
  else return t->m;
}
void upd(node* t){
  if(t==nullptr) return;
  t->size = cnt(t->left) + 1 + cnt(t->right);
  t->m = max(t->val, M(t->left));
  t->m = max(t->m, M(t->right));
}
void push(node* t){
  if(t==nullptr) return;
  if(t->c) {
    swap(t->left, t->right);
    if(t->left != nullptr) t->left->c = !t->left->c;
    if(t->right != nullptr) t->right->c = !t->right->c;
    t->c=0;
  }
}
void split(node* t, node*& left, node*& right, int k){
  if(t==nullptr){
    left=nullptr, right=nullptr;
    return;
  }
  push(t);
  if(cnt(t->left) >= k){
    split(t->left, left, t->left, k);
    right=t;
  }else{
    split(t->right, t->right, right, k-cnt(t->left)-1);
    left=t;
  }
  upd(left);
  upd(right);
}
void merge(node*& t, node* left, node* right){
  push(left);
  push(right);
  if(left==nullptr) t=right;
  else if(right==nullptr) t=left;
  else{
    if(left->prior > right->prior){
      merge(left->right, left->right, right);
      t=left;
    }else{
      merge(right->left, left, right->left);
      t=right;
    }
  }
  upd(t);
}
int etsi(node* t){
  push(t);
  if(t->val==t->m) return cnt(t->left)+1;
  else if(M(t->left) == t->m) return etsi(t->left);
  else return cnt(t->left) + 1 + etsi(t->right);
  upd(t);
}
vector<int> v;
int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  node* tree = nullptr;
  for(int i=0; i<n; ++i){
    int a; cin >> a;
    node* t = new node(a);
    merge(tree, tree, t);
  }
  for(int i=n; i>1; --i){
    int k=etsi(tree);
    v.push_back(k);
    node* left = nullptr;
    node* right = nullptr;
    split(tree, left, right, k);
    if(left!=nullptr) left->c = !left->c;
    merge(tree, left, right);
    v.push_back(i);
    tree->c = !tree->c;
    split(tree, tree, right, i-1);
  }
  cout << v.size() << "\n";
  for(unsigned int i=0; i<v.size(); ++i) cout << v[i] << " ";
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
10
9 3 4 7 6 5 10 2 8 1

correct output
32
10 10 9 10 9 8 7 9 4 2 1 4 5 2...

user output
18
7 10 4 9 7 8 4 7 6 6 5 5 3 4 3...

Test 2

Group: 2

Verdict: ACCEPTED

input
1000
650 716 982 41 133 1000 876 92...

correct output
3984
207 207 206 207 128 127 126 12...

user output
1998
6 1000 904 999 964 998 333 997...

Test 3

Group: 3

Verdict: ACCEPTED

input
100000
94703 47808 62366 31885 7091 8...

correct output
399956
98676 98676 98675 98676 62994 ...

user output
199998
35855 100000 67405 99999 23170...