CSES - Datatähti 2017 alku - Results
Submission details
Task:Maalarit
Sender:ollpu
Submission time:2016-10-05 23:05:59 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:165:9: error: 'p3used' was not declared in this scope
         p3used = 1;
         ^

Code

#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

#define F first
#define S second
const int N = 1<<17;

pair<int,int> mint[N<<1];
int p3t[N<<1];

void build_mint() {
  for (int i = N-1; i > 0; --i) {
    if (mint[i*2].F < mint[i*2+1].F || mint[i*2+1].F == 0) {
      mint[i] = mint[i*2];
    } else {
      mint[i] = mint[i*2+1];
    }
  }
}

int mint_find(int a, int b) {
  a += N; b += N;
  int minv = mint[a].F, mini = mint[a].S;
  while (a <= b) {
    if (a&1 && mint[a++].F < minv) {
      minv = mint[a-1].F;
      mini = mint[a-1].S;
    }
    if (!(b&1) && mint[b--].F < minv) {
      minv = mint[b+1].F;
      mini = mint[b+1].S;
    }
    a >>= 1; b >>= 1;
  }
  return mini;
}

void p3t_add(int i) {
  i += N;
  while (i) {
    p3t[i]++;
    i >>= 1;
  }
}

void p3t_decr(int i) {
  i += N;
  while (i) {
    p3t[i]--;
    i >>= 1;
  }
}

int p3t_sum(int i) {
  i += N;
  int a = N;
  int sum = 0;
  while (a <= i) {
    if (a&1) sum += p3t[a++];
    if (!(i&1)) sum += p3t[i--];
    a >>= 1; i >>= 1;
  }
  return sum;
}


void ole() {
  while (true) cout << "asd";
}


int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int t[n];
  pair<int, int> s[n];
  for (int i = 0; i < n; ++i) {
    cin >> t[i];
    s[i] = {t[i], i};
    mint[N+i] = {t[i], i};
  }
  build_mint();
  sort(s, s+n, greater<pair<int,int>>());
  int resultA = 0, resultB = 0, result;
  for (int i = 0; i < n; ++i) {
    if (i%2) resultA = max(resultA, t[i]);
    else resultB = max(resultB, t[i]);
  }
  result = resultA + resultB;
  bool m2improves = 0;
  int p1_v = s[0].F, p2_v = 0, p3_v = 0;
  set<int> sp_locs;
  int sp_n_p3[n];
  int is_p3[n];
  int msp_loc = s[0].S;
  sp_locs.insert(msp_loc);
  // clog << msp_loc << endl;
  for (int si = 1; si < n; ++si) {
    int i = s[si].S, next_v = s[si+1].F;
    // clog << i << endl;
    if ((i+p3t_sum(i))%2 != (msp_loc+p3t_sum(msp_loc))%2) {
      set<int>::iterator it = sp_locs.upper_bound(i);
      if (it == sp_locs.end()) {
        it--;
        int psp_loc = *it;
        if (abs(psp_loc-i) <3) break;
        int p3_loc = mint_find(psp_loc+1, i-1);
        p3_v = max(p3_v, t[p3_loc]);
        sp_locs.insert(i);
        p3t_add(p3_loc);
        p2_v = next_v;
        sp_n_p3[psp_loc] = p3_loc;
      } else if (it == sp_locs.begin()) {
        int nsp_loc = *it;
        if (abs(nsp_loc-i) <3) break;
        int p3_loc = mint_find(i+1, nsp_loc-1);
        // clog << p3_loc << endl;
        p3_v = max(p3_v, t[p3_loc]);
        sp_locs.insert(i);
        p3t_add(p3_loc);
        p2_v = next_v;
        sp_n_p3[i] = p3_loc;
      } else {
        set<int>::iterator bit = it;
        bit--;
        int psp_loc = *bit, nsp_loc = *it;
        if (abs(psp_loc-i) <3 || abs(nsp_loc-i) <3) break;
        int pp3_loc = mint_find(psp_loc+1, i-1),
            np3_loc = mint_find(i+1, nsp_loc-1);
        p3_v = max(t[pp3_loc], t[np3_loc]);
        sp_locs.insert(i);
        if (sp_n_p3[psp_loc] != -1) p3t_decr(sp_n_p3[psp_loc]);
        sp_n_p3[psp_loc] = pp3_loc;
        sp_n_p3[i] = np3_loc;
        p3t_add(pp3_loc); p3t_add(np3_loc);
        p2_v = next_v;
      }
    } else {
      sp_locs.insert(i);
      sp_n_p3[i] = -1;
      p2_v = next_v;
    }
    if (p1_v + p2_v + p3_v < result) {
      m2improves = 1;
      result = p1_v + p2_v + p3_v;
      for (int j = 0; j < n; ++j) {
        is_p3[j] = p3t[j+N];
      }
    }
    // for (int j = 0; j < n; ++j) {
    //   clog << p3t[j+N] << ' ';
    // }
    // clog << endl;
  }
  // clog << endl;
  if (m2improves) {
    cout << result << (p3_v ? " 3\n" : " 2\n");
    int invs = 0;
    for (int i = 0; i < n; ++i) {
      if (is_p3[i]) {
        p3used = 1;
        invs++;
        cout << "3 ";
      } else if ((i+invs)%2) {
        cout << "2 ";
      } else {
        cout << "1 ";
      }
    }
  } else {
    cout << resultA+resultB << " 2" << endl;
    for (int i = 0; i < n; ++i) {
      if (i%2) cout << "2 "; else cout << "1 ";
    }
  }
  
}