Submission #4215418


Source Code Expand

Copy
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
const long long INF = 1e18;

struct SegmentTree {
private:
  int n;
  vector< long long > node;//min,max
public:
  SegmentTree(vector<long long> v, vector<long long> u){
    int sz = v.size();
    n = 1;
    while(n < sz) n *= 2;
    node.resize(2*n-1,INF);
    for(int i = 0; i < sz; ++i){
      node[i+n-1] = v[i];
      if(i > 0) node[i+n-1] += u[i-1];
    }
    for(int i = n-2; i >= 0; --i)
      node[i] = min(node[2*i+1],node[2*i+2]);
  }
  void update(int idx, int val){
    idx += n-1;
    node[idx] = val;
    while(idx > 0){
      idx = (idx-1)/2;
      node[idx] = min(node[2*idx+1],node[2*idx+2]);
    }
  }
  long long get_min(int a, int b, int k=0, int l=0, int r=-1){
    if(r < 0) r = n;
    if(r <= a or b <= l) return INF;
    if(a <= l and r <= b) return node[k];
    long long vl = get_min(a,b,2*k+1,l,(l+r)/2);
    long long vr = get_min(a,b,2*k+2,(l+r)/2,r);
    return min(vl,vr);
  }
};

int main(){
  int L;
  cin >> L;
  L += 2;
  vector<long long> A(L,0), F(L+1,0), B(L+1,0), C(L+1,0), O(L+1,0);
  for(int i = 0; i < L-2; ++i) cin >> A[i+1];
  for(int i = 0; i < L; ++i) C[i+1] += A[i] + C[i];
  for(int i = 0; i < L; ++i) O[i+1] += 1-(A[i]%2) + O[i];
  for(int i = 0; i < L; ++i){
    long long t = A[i]%2;
    if(A[i] < 2) t = 2-A[i];
    F[i+1] = min(F[i]+t,C[i+1]);
  }
  for(int i = L-1; i >= 0; --i){
    long long t = A[i]%2;
    if(A[i] < 2) t = 2-A[i];
    B[i] = min(B[i+1]+t,C[L]-C[i]);
  }
  // fprintf(stderr,"F : "); for(int i = 0; i <= L; ++i) cerr << F[i] << " "; cerr << endl;
  // fprintf(stderr,"B : "); for(int i = 0; i <= L; ++i) cerr << B[i] << " "; cerr << endl;
  // fprintf(stderr,"O : "); for(int i = 0; i <= L; ++i) cerr << O[i] << " "; cerr << endl;
  SegmentTree st(B,O);
  long long ans = min(F[L],O[L]);
  for(int i = 0; i <= L; ++i){
    long long t = F[i] + st.get_min(i+1,L) - O[i];
    // fprintf(stderr,"F[%d] = %lld, st.get_min(%d) = %lld\n",i,F[i],i+1,st.get_min(i+1,L));
    // fprintf(stderr,"O[%d] = %lld\n",i,O[i]);
    // cerr << i << " " << t << endl;
    ans = min(ans,t);
  }
  assert(ans > 0);
  cout << ans << endl;
}

Submission Info

Submission Time
Task D - Ears
User TAB
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2246 Byte
Status

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 s1.txt, s2.txt, s3.txt
All 0 / 600 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt 125 ms 15232 KB
02.txt 118 ms 15232 KB
03.txt 125 ms 15232 KB
04.txt 124 ms 15360 KB
05.txt 79 ms 15232 KB
06.txt 72 ms 15232 KB
07.txt 80 ms 15232 KB
08.txt 78 ms 15232 KB
09.txt 80 ms 15232 KB
10.txt 73 ms 15232 KB
11.txt 80 ms 15232 KB
12.txt 79 ms 15232 KB
13.txt 80 ms 15232 KB
14.txt 73 ms 15232 KB
15.txt 81 ms 15232 KB
16.txt 80 ms 15232 KB
17.txt
18.txt
19.txt 80 ms 15232 KB
20.txt 79 ms 15232 KB
21.txt
22.txt
23.txt
24.txt
25.txt
26.txt
27.txt 81 ms 15232 KB
28.txt 80 ms 15232 KB
29.txt 88 ms 15232 KB
30.txt 88 ms 15232 KB
31.txt 84 ms 15232 KB
32.txt 97 ms 15232 KB
33.txt 80 ms 15232 KB
34.txt 73 ms 15232 KB
35.txt 80 ms 15232 KB
36.txt 79 ms 15232 KB
37.txt 1 ms 256 KB
38.txt 1 ms 256 KB
39.txt
40.txt
41.txt
42.txt
s1.txt 1 ms 256 KB
s2.txt 1 ms 256 KB
s3.txt 1 ms 256 KB