Problem:
E: 凸凹数列
問題
長さ $N$ の数列 $A$ が与えられる。$A$ の $i$ 項目 は $A_i$ である。
  あなたは、この数列に対して以下の操作を行うことができる。
$1 \leq i \leq N - 1$ なる整数 i を選ぶ。 $A_i$ の値と $A_{i + 1}$ の値を入れ替える。
$A$ を凸凹数列にするために必要な操作の最小回数を求めよ。
以下の条件を満たす長さ $N$ の数列を凸凹数列と定義する。
$1 < i < N$ を満たす任意の $i$ について、 $A_{i + 1}, A_{i - 1} > A_i$ または $A_{i + 1}, A_{i - 1} < A_i$ を満たす。
直感的に言えば、 $1,\ 10,\ 2,\ 30,\ \dots (10,\ 1,\ 30,\ 2,\ \dots )$ のような、増加、減少、増加…（減少、増加、減少…）を繰り返す数列である。
制約
入力値は全て整数である。
$3 \leq N \leq 10^5$
$i \neq j$ ならば $A_i \neq A_j$
$-10^9 \leq A_i \leq 10^9$
入力形式
入力は以下の形式で与えられる。
$N$
$A_1 \dots A_N$
出力
凸凹数列にするために必要な操作の最小回数を出力せよ。また、末尾に改行も出力せよ。
サンプル
サンプル入力 1
5
1 2 3 4 5
サンプル出力 1
2
$2$ と $3$ 、 $4$ と $5$ に操作を行えば、 $1\ 3\ 2\ 5\ 4$ という凸凹数列になる。
サンプル入力 2
3
1 2 3
サンプル出力 2
1
$1$ と $2$ に操作を行う、あるいは $2$ と $3$ に操作を行うことで、凸凹数列になる。
サンプル入力 3
12
5 9 1 38 100 -23 4 16 -2 -10 -17 8
サンプル出力 3
2

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

bool check(std::vector<int> &a, int i) {
    if (i <= 0 || i >= a.size() - 1) return true;
    return (a[i - 1] > a[i] && a[i] < a[i + 1]) || (a[i - 1] < a[i] && a[i] > a[i + 1]);
}

signed main() {
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    int ans = 0;
    for (int i = 1; i < n - 1; i++) {
        if (!check(a, i)) {
            if (i < n - 2) {
                std::swap(a[i + 1], a[i + 2]);
                if (check(a, i)) {
                    ans++;
                    continue;
                }
                std::swap(a[i + 1], a[i + 2]);
            }
            std::swap(a[i + 1], a[i]);
            ans++;
        }
    }
    std::cout << ans << "\n";
}
accuracy: 1
time_ms: 30
memory_kb: 3220
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Функция проверки, зигзагообразна ли последовательность
bool isZigzag(const vector<int>& A, int i) {
    if (i <= 0 || i >= A.size() - 1) return true;
    return (A[i - 1] > A[i] && A[i] < A[i + 1]) || (A[i - 1] < A[i] && A[i] > A[i + 1]);
}

// Основная функция для подсчёта минимального числа перестановок
int minSwapsToZigzag(vector<int> A) {
    int N = A.size();
    int swaps = 0;

    for (int i = 1; i < N - 1; ++i) {
        if (!isZigzag(A, i)) {
            if (i < N - 2) {
                swap(A[i + 1], A[i + 2]);
                if (isZigzag(A, i)) {
                    swaps++;
                    continue;
                }
                swap(A[i + 1], A[i + 2]);
            }
            swap(A[i + 1], A[i]);
            if (isZigzag(A, i)) {
                swaps++;
                continue;
            }
            swap(A[i + 1], A[i]);
            swap(A[i - 1], A[i]);
            if (isZigzag(A, i)) {
                swaps++;
                continue;
            }
            swap(A[i - 1], A[i]);
            std::cout << "HMM\n";
        }
    }
    return swaps;
}

int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }

    int result = minSwapsToZigzag(A);
    cout << result << endl;

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3608
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define ll long long

using namespace std;

signed main() {
    int n;
    cin >> n;
    int arr[n];
    for(int i = 0 ; i < n ; i++){
        cin >> arr[i];
    }
    ll ans = 0;
    bool change = 0;
    for(int i = 0 ; i + 2 < n ;i++){
        if(arr[i] < arr[i+1] && arr[i+1] < arr[i+2]){
            if(change)
            {
                swap(arr[i], arr[i + 1]);
                swap(arr[i + 1], arr[i + 2]);
            }else {
                swap(arr[i + 1], arr[i + 2]);
                ans++;
            }
            change = 1;
        }else if(arr[i] > arr[i+1] && arr[i+1] > arr[i+2]){
            if(change)
            {
                swap(arr[i], arr[i + 1]);
                swap(arr[i + 1], arr[i + 2]);
            }else {
                swap(arr[i + 1], arr[i + 2]);
                ans++;
            }
            change = 1;
        }else{
            change = 0;
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3500
final_rank: 5

code_snippet: #include <bits/stdc++.h>

int main() {
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) std::cin >> a[i];

    auto solve = [](std::vector<int> a) {
        int n = a.size();
        int ans = 0;
        for (int i = 0; i + 1 < n; i++) {
            if (i % 2 == 0) {
                // a[i] < a[i + 1]にしたい
                if (a[i] < a[i + 1]) {
                    continue;
                } else if (i + 2 == n || (a[i + 1] < a[i])) {
                    // a[i + 1] < a[i] < a[i + 2]
                    // a[i + 1] < a[i + 2] < a[i]
                    // a[i + 2] < a[i + 1] < a[i]
                    std::swap(a[i], a[i + 1]);
                    ans++;
                } else {
                    std::swap(a[i + 1], a[i + 2]);
                    ans++;
                }
            } else {
                // a[i] > a[i + 1]にしたい
                if (a[i] > a[i + 1]) {
                    continue;
                } else if (i + 2 == n || (a[i] < a[i + 2])) {
                    // a[i] < a[i + 1] < a[i + 2]
                    // a[i] < a[i + 2] < a[i + 1]
                    // a[i + 2] < a[i] < a[i + 1]
                    ans++;
                    std::swap(a[i], a[i + 1]);
                } else {
                    ans++;
                    std::swap(a[i + 1], a[i + 2]);
                }
            }
        }
        for (int i = 0; i + 1 < n; i++) {
            if (i % 2 == 0) {
                assert(a[i] < a[i + 1]);
            } else {
                assert(a[i] > a[i + 1]);
            }
        }
        return ans;
    };

    long long lhs = solve(a);
    for (auto& v: a) v = -v;
    long long rhs = solve(a);
    std::cout << std::min(lhs, rhs) << std::endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3876
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
struct SegTree{
    vector<int>dat;
    SegTree(int n){
        int z=1;
        while(z<n)z<<=1;
        dat.resize(z*2,0);
    }
    void add(int i,int x){
        i+=dat.size()>>1;
        dat[i]+=x;
        i>>=1;
        while(i){
            dat[i]=dat[i*2]+dat[i*2+1];
            i>>=1;
        }
    }
    int sum(int l,int r){
        int ret=0;
        l+=dat.size()>>1,r+=dat.size()>>1;
        while(l<r){
            if(l&1)ret+=dat[l++];
            if(r&1)ret+=dat[--r];
            l>>=1;
            r>>=1;
        }
        return ret;
    }
};
// int main(){
//     int n;
//     cin>>n;
//     vector<int>a(n);
//     for(int i=0;i<n;i++)cin>>a[i];
//     vector<int>z(a);
//     sort(z.begin(),z.end());
//     for(int i=0;i<n;i++)a[i]=lower_bound(z.begin(),z.end(),a[i])-z.begin();
//     ll ans=1e9;
//     auto inversion=[&](vector<int>ids)->ll {
//         SegTree seg(ids.size());
//         ll ret=0;
//         for(int i=0;i<ids.size();i++){
//             ret+=seg.sum(0,ids[i]);
//             seg.add(ids[i],1);
//         }
//         return ret;
//     };
//     int border=a[n/2];
//     vector<bool>big(n,false);
//     for(int i=0;i<n;i++)big[i]=a[i]>=border;
//     int bid=0,sid=1;
//     vector<int>idx(n);
//     for(int i=0;i<n;i++){
//         if(big[i]){
//             idx[i]=bid;
//             bid+=2;
//         }
//         else{
//             idx[i]=sid;
//             sid+=2;
//         }
//     }
//     ans=inversion(idx);
//     border=a[(n-1)/2];
//     big.assign(n,false);
//     for(int i=0;i<n;i++)big[i]=a[i]>border;
//     bid=1,sid=0;
//     for(int i=0;i<n;i++){
//         if(big[i]){
//             idx[i]=bid;
//             bid+=2;
//         }
//         else{
//             idx[i]=sid;
//             sid+=2;
//         }
//     }
//     for(int i=0;i<n;i++)cerr<<idx[i]<<endl;
//     ans=min(ans,inversion(idx));
//     cout<<ans<<endl;
// }
bool chmin(int &x,int y){
    if(x>y){
        x=y;
        return true;
    }
    return false;
}
int main(){
    int n;
    cin>>n;
    vector<int>a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    int ans=0;
    vector<int>p;
    for(int i=0;i<n-1;i++)p.push_back(a[i]<a[i+1]);
    p.push_back(-1);
    int now=0,pre=-2;
    for(int i=0;i<p.size();i++){
        if(p[i]==pre)now++;
        else{
            if(now>=2)ans+=now-1;
            pre=p[i];
            now=1;
        }
    }
    cout<<(ans+1)/2<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4132
final_rank: 3

code_snippet: #include <bits/stdc++.h>

#define all(c) begin(c), end(c)
#define rep(i, a, b) for (int i = a; i < int(b); ++i)
#define sz(c) int(size(c))

using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vi a(n);
  for (auto&& e : a) cin >> e;
  int ans = n;
  rep(phase, 0, 2) {
    int cur = 0;
    int i = 0;
    while (i < n - 1) {
      if (~i & 1) {
        if (a[i] < a[i + 1]) {
          ++i;
          continue;
        }
        if (i + 1 < n - 1 and a[i] < a[i + 2]) {
          swap(a[i + 1], a[i + 2]);
          ++cur;
          i += 2;
        } else {
          swap(a[i], a[i + 1]);
          ++cur;
          ++i;
        }
      } else {
        if (a[i] > a[i + 1]) {
          ++i;
          continue;
        }
        if (i + 1 < n - 1 and a[i] > a[i + 2]) {
          swap(a[i + 1], a[i + 2]);
          ++cur;
          i += 2;
        } else {
          swap(a[i], a[i + 1]);
          ++cur;
          ++i;
        }
      }
    }
    ans = min(ans, cur);
    for (auto&& e : a) e = -e;
  }
  cout << ans << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 3680
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

constexpr int INF = 1<<30;

int main(){
    int n;
    cin >> n;
    vector<int> a, b(n);
    for(int i=0;i<n;i++){
        cin >> b[i];
    }
    int ans = INF;
    for(int t=0;t<2;t++){
        int cnt = 0;
        a = b;
        for(int i=0;i<n-2;i++){
            if((i+t)%2){
                if(a[i]<a[i+1]){
                    cnt++;
                    if(a[i]<a[i+2]) swap(a[i],a[i+1]);
                    else swap(a[i+1],a[i+2]);
                }
            }
            else{
                if(a[i]>a[i+1]){
                    cnt++;
                    if(a[i]>a[i+2]) swap(a[i],a[i+1]);
                    else swap(a[i+1],a[i+2]);
                }
            }
        }
        if((n-2+t)%2){
            if(a[n-2]<a[n-1]) cnt++;
        }else{
            if(a[n-2]>a[n-1]) cnt++;
        }
        ans = min(ans, cnt);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3680
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n;
vector<int> a;

int solve(int x, int now);

int main() {
  cin >> n;
  for(int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  cout << min(solve(0, 1), solve(1, 1)) << endl;
  return 0;
}

int solve(int x, int now) {
  int ans = 0;
  while(now < a.size() &&
        (a[now - 1] < a[now]) == (now + x) % 2)
    ++now;
  if(now == a.size()) return 0;
  swap(a[now], a[now - 1]);
  ans = solve(x, now + 1);
  swap(a[now], a[now - 1]);
  if(now + 1 < a.size() &&
     (a[now] < a[now + 1]) != (now + 1 + x) % 2 &&
     (a[now - 1] < a[now + 1]) == (now + x) % 2) {
    swap(a[now], a[now + 1]);
    ans = min(ans, solve(x, now + 1));
    swap(a[now], a[now + 1]);
  }
  return ans + 1;
}
accuracy: 1
time_ms: 30
memory_kb: 6560
final_rank: 18

code_snippet: #include <bits/stdc++.h>

#define rep(i, n) for(int i = 0 ; i < (int)(n) ; i++)
typedef long long ll;
typedef long double Double;

namespace stream {
    template<class T>
    class Stream {
        std::vector<T> instance;
    public:

        Stream(const std::vector<T> &instance) : instance(instance) {}


        template<class F>
        auto filter(const F &func) {
            return filterPrivate([&](const T &t, int &idx) { return func(t); });
        }

        template<class F>
        auto filterWithIndex(const F &func) {
            return filterPrivate(func);
        }

        template<class F>
        auto map(const F &func) {
            return mapPrivate([&](const T &t, int &idx) { return func(t); });
        }

        template<class F>
        auto mapWithIndex(const F &func) {
            return mapPrivate(func);
        }

        template<class F, class R>
        R reduce(const F &func, const R &init_value) {
            R ans = init_value;
            for (int i = 0; i < instance.size(); i++) {
                ans = func(ans, instance[i]);
            }
            return ans;
        }

        template<class F>
        Stream<T> sort(F cmp) {
            std::vector<T> v = instance;
            std::sort(v.begin(), v.end(), cmp);
            return Stream<T>(v);
        }

        Stream<T> sort() {
            std::vector<T> v = instance;
            std::sort(v.begin(), v.end());
            return Stream<T>(v);
        }

        Stream<T> sort_reversed() {
            std::vector<T> v = instance;
            std::sort(v.rbegin(), v.rend());
            return Stream<T>(v);
        }

        Stream<T> unique() {
            std::unordered_set<T> existing;
            std::vector<T> ans;
            for (auto &&i : instance) {
                if (existing.count(i)) { continue; }
                existing.insert(i);
                ans.push_back(i);
            }
            return Stream<T>(ans);
        }

        unsigned long count() {
            return instance.size();
        }

        T sum() {
            return std::accumulate(instance.begin(), instance.end(), T());
        }

        std::vector<T> collect() {
            return instance;
        }

        std::set<T> toSet() {
            return std::set<T>(instance.begin(), instance.end());
        }

    private:
        template<class F>
        Stream<T> filterPrivate(const F &func) {
            std::vector<T> ans;

            for (int i = 0; i < instance.size(); i++) {
                if (func(instance[i], i)) {
                    ans.push_back(instance[i]);
                }
            }
            return Stream<T>(ans);
        }

        template<class F>
        auto mapPrivate(const F &func) {
            using R = typename std::result_of<F(T, int)>::type;
            std::vector<R> ans;
            for (int i = 0; i < instance.size(); i++) {
                ans.push_back(func(instance[i], i));
            }
            return Stream<R>(ans);
        }

    };

    template<class T>
    Stream<T> _(const std::vector<T> &instance) {
        return Stream<T>(instance);
    };

    template<class T>
    Stream<T> _(const std::set<T> &instance) {
        return Stream<T>(std::vector<T>(instance.begin(), instance.end()));
    };

    template<class T, class U>
    Stream<std::pair<T, U>> _(const std::vector<T> &a, std::vector<U> &b) {
        const unsigned long &size = std::min(a.size(), b.size());
        std::vector<std::pair<T, U>> zipped;
        return _(zipped);
    }

    Stream<int> range(int a, int b) {
        if (a >= b) return Stream<int>(std::vector<int>());
        std::vector<int> ans(b - a);
        for (int i = 0; i < b - a; i++)
            ans[i] = a + i;
        return Stream<int>(ans);
    }

    Stream<int> range(int n) {
        return range(0, n);
    }
}

namespace io {
    template<class T>
    void read(std::vector<T> &v) {
        for (int i = 0; i < v.size(); i++) std::cin >> v[i];
    }

    std::vector<int> readIntArray(int n) {
        std::vector<int> a(n);
        read(a);
        return a;
    }

    long long readLong() {
        long long x;
        std::cin >> x;
        return x;
    }

    int readInt() {
        long long x = readLong();
        assert(x >= INT_MIN && x <= INT_MAX);
        return (int) x;
    }

    std::string readWord() {
        std::string x;
        std::cin >> x;
    }

    template<class T>
    void output(const std::vector<T> &v, char delimiter = ' ') {
        if (v.size() > 0) {
            std::cout << v[0];
            for (int i = 1; i < v.size(); i++) {
                std::cout << delimiter << v[i];
            }
        }
        std::cout << std::endl;
    }

    template<class T>
    void output(const T &v) {
        std::cout << v << std::endl;
    }

    void output(double x) {
        printf("%.10lf\n", x);
    }
};

using namespace std;
using namespace stream;
using namespace io;


int main() {
    int n;
    cin >> n;
    auto a = readIntArray(n);
    auto b = a;

    int ans = 12345678;
    for (int p = 0; p < 2; p++) {
        a = b;
        int subAns = 0;
        for (int i = 0; i + 1 < n; i++) {
            if ((a[i] > a[i + 1]) != (i % 2 == p)) {
//                cout << "from:";
//                output(a);
                if (i + 2 < n && (a[i] > a[i + 2]) == (i % 2 == p)) {
                    swap(a[i + 1], a[i + 2]);
                } else {
                    swap(a[i], a[i + 1]);
                }
                subAns++;
//                cout << "to  :";
//                output(a);


            }
        }
        ans = min(subAns, ans);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3648
final_rank: 9

code_snippet: #include <bits/stdc++.h>

                                            //    #include <boost/multiprecision/cpp_int.hpp>
 
                               	       #define int long long
                                                #define inf  1000000007
                                              #define pa pair<int,int>
                                               #define pad pair<double,double>
                                                #define ll long long
                                                #define pal pair<double,double>
                                                #define ppap pair<pa,int>
                                          #define PI 3.14159265358979323846
                                                #define paa pair<pa,pa>
                                                #define  mp make_pair
                                                #define  pb push_back
                                                #define EPS (1e-10)
                                                #define equals(a,b) (fabs((a)-(b))<EPS)
                                            //    #define double long double
        int dx[4]={0,-1,0,1};
        int dy[4]={1,0,-1,0};
                                                using namespace std;
                                       			class pa3{
                                                	public:
                                                	int x,y,z;
                                                	pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {}
                                                	bool operator < (const pa3 &p) const{
                                                		if(x!=p.x) return x<p.x;
                                                		if(y!=p.y) return y<p.y;
                                                		 return z<p.z;
                                                		//return x != p.x ? x<p.x: y<p.y;
                                                	}
                                       				bool operator > (const pa3 &p) const{
                                                		if(x!=p.x) return x>p.x;
                                                		if(y!=p.y) return y>p.y;
                                                		 return z>p.z;
                                                		//return x != p.x ? x<p.x: y<p.y;
                                                	}
                                                	bool operator == (const pa3 &p) const{
                                                		return x==p.x && y==p.y && z==p.z;
                                                	}
                                                		bool operator != (const pa3 &p) const{
                                                			return !( x==p.x && y==p.y && z==p.z);
                                                	}
                                                
                                                };
                                                
                                                class pa4{
                                                	public:
                                                	int x;
                                                	int y,z,w;
                                                	pa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {}
                                                	bool operator < (const pa4 &p) const{
                                                		if(x!=p.x) return x<p.x;
                                                		if(y!=p.y) return y<p.y;
                                                		if(z!=p.z)return z<p.z;
                                                		return w<p.w;
                                                		//return x != p.x ? x<p.x: y<p.y;
                                                	}
                                                	bool operator > (const pa4 &p) const{
                                                		if(x!=p.x) return x>p.x;
                                                		if(y!=p.y) return y>p.y;
                                                		if(z!=p.z)return z>p.z;
                                                		return w>p.w;
                                                		//return x != p.x ? x<p.x: y<p.y;
                                                	}
                                                	bool operator == (const pa4 &p) const{
                                                		return x==p.x && y==p.y && z==p.z &&w==p.w;
                                                	}
                                                	
                                                	bool operator != (const pa4 &p) const{
                                                		return !(x==p.x && y==p.y && z==p.z &&w==p.w);
                                                	}
                                                		
                                                
                                                };
                                                class pa2{
                                                	public:
                                                	int x,y;
                                                	pa2(int x=0,int y=0):x(x),y(y) {}
                                                	pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}
                                                	pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);}
                                                	bool operator < (const pa2 &p) const{
                                                		return x!= p.x ? x<p.x: y<p.y;
                                                	}
                                                	bool operator > (const pa2 &p) const{
                                                		return x != p.x ? x>p.x: y>p.y;
                                                	}
                                                	bool operator == (const pa2 &p) const{
                                                		return abs(x-p.x)==0 && abs(y-p.y)==0;
                                                	}
                                                	bool operator != (const pa2 &p) const{
                                                		return !(abs(x-p.x)==0 && abs(y-p.y)==0);
                                                	}
                                                		
                                                
                                                };
                                                
                                              
                                                
                                                       #define ppa pair<int,pas>
                                                class Point{
                                                	public:
                                                	double x,y;
                                                	Point(double x=0,double y=0):x(x),y(y) {}
                                                	Point operator + (Point p) {return Point(x+p.x,y+p.y);}
                                                	Point operator - (Point p) {return Point(x-p.x,y-p.y);}
                                                	Point operator * (double a) {return Point(x*a,y*a);}
                                                	Point operator / (double a) {return Point(x/a,y/a);}
                                                	double absv() {return sqrt(norm());}
                                                	double norm() {return x*x+y*y;}
                                                	bool operator < (const Point &p) const{
                                                		return x != p.x ? x<p.x: y<p.y;
                                                	}
                                                	bool operator == (const Point &p) const{
                                                		return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
                                                	}
                                                };
                                                typedef Point Vector;
                                         #define pl pair<int,pas>
                                                struct Segment{
                                                Point p1,p2;
                                                };
                                                 double dot(Vector a,Vector b){
                                                	return a.x*b.x+a.y*b.y;
                                                }
                                                double cross(Vector a,Vector b){
                                                	return a.x*b.y-a.y*b.x;
                                                }
                                            
                    bool parareru(Point a,Point b,Point c,Point d){
                    //	if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl;
                    	return abs(cross(a-b,d-c))<EPS;
                    }
                    double distance_ls_p(Point a, Point b, Point c) {
                      if ( dot(b-a, c-a) < EPS ) return (c-a).absv();
                      if ( dot(a-b, c-b) < EPS ) return (c-b).absv();
                      return abs(cross(b-a, c-a)) / (b-a).absv();
                    }
                    bool is_intersected_ls(Segment a,Segment b) {
                    	if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return 1;
                    	if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){
                    //		cout<<"sss"<<endl;
                    		if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true;
                    		if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true;
                    		if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true;
                    		if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true;
                    		return false;
                    	}
                      else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS );
                    }
                     
                    double segment_dis(Segment a,Segment b){
                    	if(is_intersected_ls(a,b))return 0;
                    	double r=distance_ls_p(a.p1, a.p2, b.p1);
                    	r=min(r,distance_ls_p(a.p1, a.p2, b.p2));
                    	r=min(r,distance_ls_p(b.p1, b.p2, a.p2));
                    	r=min(r,distance_ls_p(b.p1, b.p2, a.p1));
                    	return r;
                    }
                    Point intersection_ls(Segment a, Segment b) {
                      Point ba = b.p2-b.p1;
                      double d1 = abs(cross(ba, a.p1-b.p1));
                      double d2 = abs(cross(ba, a.p2-b.p1));
                      double t = d1 / (d1 + d2);
                     
                      return a.p1 + (a.p2-a.p1) * t;
                    }
                     
                                    string itos( int i ) {
                                    ostringstream s ;
                                    s << i ;
                                    return s.str() ;
                                    }
                                     
                                    int gcd(int v,int b){
                                    	if(v==0) return b;
                                    	if(v>b) return gcd(b,v);
                                    	if(v==b) return b;
                                    	if(b%v==0) return v;
                                    	return gcd(v,b%v);
                                    }
                     
                                    double distans(double x1,double y1,double x2,double y2){
                                    	double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
                                    	return sqrt(rr);
                                    	
                                    }
                                                        /*
        double bekid(double w,int r){
        	if(r==0) return 1.0;
        	if(r==1) return w;
        	if(r%2) return bekid(w,r-1)*w;
        	double f=bekid(w,r/2);
        	return f*f;
        }*/
                                    int mod;
                    int beki(int wa,int rr,int warukazu){
                    	if(rr==0) return 1%warukazu;
                    	if(rr==1) return wa%warukazu;
                    	if(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu;
                    	int zx=beki(wa,rr/2,warukazu);
                    	return (zx*zx)%warukazu;
                    }
                          /*          
                    int pr[401000];
                    int inv[401000];
                   
                   

                  
        			int comb(int nn,int rr){
        				int r=pr[nn]*inv[rr];
        				r%=mod;
        				r*=inv[nn-rr];
        				r%=mod;
        				
        				return r;
        			}
                  
                    void gya(int ert){
                    	pr[0]=1;
                    	for(int i=1;i<ert;i++){
                    		pr[i]=(pr[i-1]*i)%mod;
                    	}
                    	for(int i=0;i<ert;i++) inv[i]=beki(pr[i],mod-2,mod);
                    	
                    }
                    */
                    
                  //   cin.tie(0);
        		//	ios::sync_with_stdio(false);
        			//priority_queue<pa3,vector<pa3>,greater<pa3>> pq;            
                     //sort(ve.begin(),ve.end(),greater<int>());


///////////////////////////////////////////////////////////



int a[100020];
int b[100020];
map<int,int>ma;
set<int> se;
struct Segmin{
	//       1
	//   2        3
	// 4   5   6    7
	
	
	private:
	public:
	
	// (1<<15)=32768
	// 1<<16 = 65536
	// 1<<17 = 131072
	// 1<<18 = 262144
	
	int cor=(1<<17);
	
	vector<pa> vec;
	
	void shoki1(){
		
		vec.resize(2*cor+3, mp(0,0));
		for(int i=cor;i<2*cor;i++)vec[i].second=i-cor;
	}
		void shoki2(){
			for(int i=cor-1;i>0;i--) {
				if(vec[2*i].first<=vec[2*i+1].first) vec[i]=vec[2*i];
				else vec[i]=vec[2*i+1];
			}
		}
	void clear(){
		for(int i=0;i<2*cor;i++)vec[i]=mp(0,0);
		for(int i=cor;i<2*cor;i++)vec[i].second=i-cor;
	}
	void updadd(int x,int w){
		//x 項目に w加算
		x+=cor;
		vec[x].first+=w;
		while(1){
			x/=2;
			if(x==0) break;
			if(vec[2*x].first<=vec[2*x+1].first) vec[x]=vec[2*x];
			else vec[x]=vec[2*x+1];
		}
		
	}
	
	void updchan(int x,int w){
		//x項目をwに変更
		x+=cor;
		vec[x].first=w;
		while(1){
			x/=2;
			if(x==0) break;
			if(vec[2*x].first<=vec[2*x+1].first) vec[x]=vec[2*x];
			else vec[x]=vec[2*x+1];
		}
		
	}
	// [a,b)
	// k-th node
	// k no kukanha [l,r)
	pa segmin(int a,int b,int k=1,int l=0,int r=-10){
		if(r<0)r=cor;
		
	//	cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
		
		if(a<=l && r<=b){
			return vec[k];
		}
		if(r<=a || b<=l){
			return mp((1ll<<31)-1,-1);
		}
		
		pa v1=segmin(a,b,k*2,l,(l+r)/2),v2=segmin(a,b,k*2+1,(l+r)/2,r);
		
		if(v1.first<=v2.first)return v1;
		else return v2;
	}
	
};
Segmin sm;

int solve(vector<int> ve){
//	for(auto v:ve)cout<<v<<" ";
//	cout<<endl;
	int ans=0;
	int n=ve.size();
	sm.shoki1();
	for(int i=0;i<n;i++)sm.vec[sm.cor+i].first=ve[i];
	
	sm.shoki2();
	
	while(1){
		pa z=sm.segmin(0,n+1);
		if(z.first>=0)break;
		
		if(z.second-1>=0)sm.updadd(z.second-1,-z.first);
		if(z.second+1<n)sm.updadd(z.second+1,-z.first);
		sm.updchan(z.second,-z.first);
		ans++;
	}
	return ans;
}
vector<int> q1,q2;
  signed main(){
 	              cin.tie(0);
   		ios::sync_with_stdio(false);

  	int n;
  	cin>>n;
  	for(int i=0;i<n;i++){
  		cin>>a[i];
  		se.insert(a[i]);
  //	b[i]=a[i];
  	}
  	int oc=1;
  	for(auto it=se.begin();it!=se.end();it++){
  		ma[*it]=oc;
  		oc++;
  	}
  	for(int i=0;i<n;i++){
  		a[i]=ma[a[i]];
  		b[i]=a[i];
  	}
	
  	for(int i=1;i<n;i++){
  		q1.pb((a[i]-a[i-1])*(i%2?1:-1));
  		q2.pb((a[i]-a[i-1])*(i%2==0?1:-1));
  	}
  	sm.shoki1();
  	cout<<min(solve(q1),solve(q2))<<endl;
  	return 0;
  	
               }
accuracy: 1
time_ms: 70
memory_kb: 22756
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

const int inf = 1<<25;

int n;
int a[100010];
int dp[100010][2][2];

int chmin(int& a, int b) { return a = min(a, b); }

int main() {
  cin >> n;
  for(int i = 0; i < n; ++i) cin >> a[i];
  for(int i = 0; i < 100010; ++i) {
    for(int j = 0; j < 2; ++j) {
      for(int k = 0; k < 2; ++k) dp[i][j][k] = inf;
    }
  }
  dp[0][0][0] = 0;
  dp[0][1][0] = 0;
  for(int i = 0; i < n-1; ++i) {
    if(a[i+1] > a[i]) chmin(dp[i+1][0][0], dp[i][1][0]);
    if(i && a[i+1] > a[i-1]) chmin(dp[i+1][0][0], dp[i][1][1]);
    for(int j = 0; j < 2; ++j) {
      if(i && a[i+1] < a[i] && a[i+1] < a[i-1]) chmin(dp[i+1][0][1], dp[i-1][0][j]+1);
    }
    if(a[i+1] < a[i]) chmin(dp[i+1][1][0], dp[i][0][0]);
    if(i && a[i+1] < a[i-1]) chmin(dp[i+1][1][0], dp[i][0][1]);
    for(int j = 0; j < 2; ++j) {
      if(i && a[i+1] > a[i] && a[i+1] > a[i-1]) chmin(dp[i+1][1][1], dp[i-1][1][j]+1);
    }
  }
  int ans = inf;
  for(int i = 0; i < 2; ++i) {
    for(int j = 0; j < 2; ++j) chmin(ans, dp[n-1][i][j]);
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5016
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++) 
#define Rep(i, n) for (int i = 1; i < n; i++)
#define pb emplace_back
#define vi vector<int>
#define in(a, b, c) a <= b && b < c


signed main()
{
    int n; cin >> n;
    vi data(n);
    vi funami(n);
    rep(i, n) cin >> data[i];
    rep(i, n-1) {
        funami[i] = data[i+1] - data[i];
    }

 /*   rep(i, n) {
    cout << funami[i] << " ";
    }
    cout << endl;*/

    int ans = 0;
    rep(i, n-1) {
        if (i % 2) {
            if (funami[i] > 0) {
                if (i + 1 < n-1 && funami[i] < -funami[i+1]) {
                    funami[i+2] += funami[i+1];
                    funami[i+1] *= -1;
                } else {
                    funami[i+1] += funami[i];
                    funami[i] *= -1;
                }
                ans++;
            }
        } else {
            if (funami[i] < 0) {
                if (i + 1 < n-1 && funami[i] > -funami[i+1]) {
                    funami[i+2] += funami[i+1];
                    funami[i+1] *= -1;
                } else {
                    funami[i+1] += funami[i];
                    funami[i] *= -1;
                }
                ans++;
            }
        }
    }

    //cout << ans << endl;

    rep(i, n-1) {
        funami[i] = data[i+1] - data[i];
    }
    int cnt = 0;
    rep(i, n-1) {
        if (i % 2) {
            if (funami[i] < 0) {
                funami[i+1] += funami[i];
                funami[i] *= funami[i]-1;
                cnt++;
            }
        } else {
            if (funami[i] > 0) {
                funami[i+1] += funami[i];
                funami[i] *= funami[i]-1;
                cnt++;
            }
        }
    }

//    cout << cnt << endl;

    cout << min(ans, cnt) << endl;

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3604
final_rank: 6

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;

int values[100010];
pair<int, int> dp[100010][2];

const int INF = 1 << 29;
const pair<int, int> INIT_UP(INF, INF);
const pair<int, int> INIT_DN(INF, -INF);

void update(pair<int, int>& A, pair<int, int> B, int id)
{
    if (A.first > B.first)
        A = B;
    else if (A.first == B.first) {
        if (id == 0)
            A.second = min(A.second, B.second);
        else
            A.second = max(A.second, B.second);
    }
}

int main()
{
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> values[i];
    }
    for (int i = 0; i < N + 2; i++) {
        dp[i][0] = INIT_UP;
        dp[i][1] = INIT_DN;
    }
    dp[1][0] = dp[1][1] = make_pair(0, values[0]);

    for (int i = 1; i < N; i++) {
        // next: up
        if (dp[i][0].second < values[i]) {
            update(dp[i + 1][1], make_pair(dp[i][0].first, values[i]), 1);
        } else {
            // swap (i - 1 and i)
            update(dp[i + 1][1], make_pair(dp[i][0].first + 1, dp[i][0].second), 1);

            if (i + 1 < N) {
                // swap (i and i + 1)
                if (dp[i][0].second < values[i + 1] and values[i + 1] > values[i]) {
                    update(dp[i + 2][0], make_pair(dp[i][0].first + 1, values[i]), 0);
                }
            }
        }

        // next: down
        if (dp[i][1].second > values[i]) {
            update(dp[i + 1][0], make_pair(dp[i][1].first, values[i]), 0);
        } else {
            // swap (i - 1 and i)
            update(dp[i + 1][0], make_pair(dp[i][1].first + 1, dp[i][1].second), 0);

            if (i + 1 < N) {
                // swap (i and i + 1)
                if (dp[i][1].second > values[i + 1] and values[i + 1] < values[i]) {
                    update(dp[i + 2][1], make_pair(dp[i][1].first + 1, values[i]), 1);
                }
            }
        }
    }

    /*
    for (int i = 0; i <= N; i++) {
        for (int k = 0; k < 2; k++) {
            if (dp[i][k] == INIT_UP or dp[i][k] == INIT_DN) continue;
            fprintf(stderr, "dp[%d][%d] = (%d, %d)\n", i, k, dp[i][k].first, dp[i][k].second);
        }
    }
    */
    cout << min(dp[N][0].first, dp[N][1].first) << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5032
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long
const int INF = 1e9;

signed main() {
  int N;
  cin >> N;

  vector<int> A(N);
  for ( int i = 0; i < N; i++ ) {
    cin >> A[i];    
  }

  vector<int> a = A;
  int ans = INF;
  int cnt = 0;
  bool now = 0;
  for ( int i = 0; i < N-1; i++ ) {    
    if ( now ) { // 増加
      if ( a[i] > a[i+1] ) {
	if ( i < N-2 && a[i] < a[i+2] ) {
	  swap(a[i+1], a[i+2]);	  
	} else {
	  swap(a[i], a[i+1]);	 
	}
	cnt++;	
      }
    } else { // 減少
      if ( a[i] < a[i+1] ) {
	if ( i < N-2 && a[i] > a[i+2] ) {
	  swap(a[i+1], a[i+2]);	  
	} else {
	  swap(a[i], a[i+1]);	 
	}
	cnt++;	
      }
    }
    now = !now;    
  }
  
  ans = cnt;

  a = A;
  cnt = 0;
  now = 1;
  for ( int i = 0; i < N-1; i++ ) {
    if ( now ) { // 増加
      if ( a[i] > a[i+1] ) {
	if ( i < N-2 && a[i] < a[i+2] ) {
	  swap(a[i+1], a[i+2]);	  
	} else {
	  swap(a[i], a[i+1]);	 
	}
	cnt++;	
      }
    } else { // 減少
      if ( a[i] < a[i+1] ) {
	if ( i < N-2 && a[i] > a[i+2] ) {
	  swap(a[i+1], a[i+2]);	  
	} else {
	  swap(a[i], a[i+1]);	 
	}
	cnt++;	
      }
    }
    now = !now;    
  }

  cout << min(ans, cnt) << endl;
  
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4436
final_rank: 14

code_snippet: #include <algorithm>
#include <iostream>
#include <vector>
const int INF = (1e9);
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  for(int i = 0; i < N; ++i) cin >> A[i];
  int ans = INF;
  for(int i = 0; i < 4; ++i){
    int t = 0;
    vector<int> B = A;
    if(i >= 2) reverse(B.begin(), B.end());
    for(int j = 0; j < N-1; ++j){
      if(j%2 == i%2){
        if(B[j] < B[j+1] && (j >= N-2 || (j < N-2 && B[j+1] < B[j+2]))){
          ++t;
          B[j+1] = B[j];
        }
      }else{
        if(B[j] > B[j+1] && (j >= N-2 || (j < N-2 && B[j+1] > B[j+2]))){
          ++t;
          B[j+1] = B[j];
        }
      }
    }
    ans = min(ans, t);
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3660
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main(){
  int N;
  cin>>N;
  std::vector<int> v(N),w(N);
  for(int i=0;i<N;i++){
    cin>>v[i];
    w[i]=v[i];
  }
  int ans=0;
  for(int i=0;i<N-2;i++){
    if(i%2){//i is odd so v[i] is already bigger than v[i-1] & i wish v[i] is bigger than v[i+1]
      if(v[i]<v[i+1]){
        if(v[i+1]<v[i+2])swap(v[i],v[i+1]);
        else{
          if(v[i+2]>v[i])swap(v[i],v[i+1]);
          else swap(v[i+1],v[i+2]);
        }
        ans++;
      }
    }
    else{
      if(v[i]>v[i+1]){
        if(v[i+1]>v[i+2])swap(v[i],v[i+1]);
        else{
          if(v[i+2]<v[i])swap(v[i],v[i+1]);
          else swap(v[i+1],v[i+2]);
        }
        ans++;
      }
    }
  }
  //cout<<ans<<endl;
  if(N%2&&v[N-2]<v[N-1])ans++;
  if(N%2==0&&v[N-2]>v[N-1])ans++;
  int fans=ans;
  ans=0;
  for(int i=0;i<N-2;i++){
    if(i%2==0){//i is odd so v[i] is already bigger than v[i-1] & i wish v[i] is bigger than v[i+1]
      if(w[i]<w[i+1]){
        if(w[i+1]<w[i+2])swap(w[i],w[i+1]);
        else{
          if(w[i+2]>w[i])swap(w[i],w[i+1]);
          else swap(w[i+1],w[i+2]);
        }
        ans++;
      }
    }
    else{
      if(w[i]>w[i+1]){
        if(w[i+1]>w[i+2])swap(w[i],w[i+1]);
        else{
          if(w[i+2]<w[i])swap(w[i],w[i+1]);
          else swap(w[i+1],w[i+2]);
        }
        ans++;
      }
    }
  }
  //cout<<ans<<endl;
  if(N%2==0&&w[N-2]<w[N-1])ans++;
  if(N%2&&w[N-2]>w[N-1])ans++;
  //cout<<fans<<" "<<ans<<endl;
  fans=min(ans,fans);
  cout<<fans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3648
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
using namespace std;

const int DOWN = 0;
const int UP = 1;

int main() {
    int N; cin >> N;
    vector<long long> A(N);
    for (int i = 0; i < N; ++i) cin >> A[i];
    vector<long long> nA = A;

    int res = 1<<29;
    for (int iter = 0; iter < 2; ++iter) {
        A = nA;
        int cur = DOWN;
        if (iter == 1) cur = UP;
        int con = 0;
        for (int i = 0; i + 1 < N; ++i) {
            if (cur == DOWN) {
                if (A[i] < A[i+1]) {
                    if (i + 2 < N && A[i+1] > A[i+2] && A[i] > A[i+2]) {
                        ++con;
                        ++i;
                        swap(A[i], A[i+1]);
                        cur = 1 - cur;
                        //cout << iter << ", " << i << ";; " << A[i] << ", " << A[i+1] << endl;
                    }
                    else {
                        ++con;
                        swap(A[i], A[i+1]);
                        //cout << iter << ", " << i << ";; " << A[i] << ", " << A[i+1] << endl;
                    }
                }
            }
            else {
                if (A[i] > A[i+1]) {
                    if (i + 2 < N && A[i+1] < A[i+2] && A[i] < A[i+2]) {
                        ++con;
                        ++i;
                        swap(A[i], A[i+1]);
                        cur = 1 - cur;
                        //cout << iter << ", " << i << ";; " << A[i] << ", " << A[i+1] << endl;
                    }
                    else {
                        ++con;
                        swap(A[i], A[i+1]);
                        //cout << iter << ", " << i << ";; " << A[i] << ", " << A[i+1] << endl;
                    }
                }
            }
            cur = 1 - cur;
        }
        res = min(res, con);
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4452
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;

int n;
vector<int> t,u;


int solve(){
  int ans=0;
  for(int i=0;i<n;i++){
    if(i==0)continue;
    if(i==n-1)continue;
    
    if( t[i-1] < t[i] && t[i] < t[i+1] ){

      if(i+2<n && t[i] > t[i+2] ){
        swap(t[i+1],t[i+2]);
      }else{
        swap(t[i],t[i+1]);
      }
      ans++;
      

      
    }else if( t[i-1] > t[i] && t[i] > t[i+1] ){
      
      if(i+2<n && t[i] < t[i+2] ){
        swap(t[i+1],t[i+2]);
      }else{
        swap(t[i],t[i+1]);
      }
      ans++;

    }

  }
  return ans;
}

int solve2(){
    int ans=0;
  for(int i=0;i<n;i++){
    if(i==0)continue;
    if(i==n-1)continue;
    if( t[i-1] < t[i] && t[i] < t[i+1] ){
      swap(t[i],t[i+1]);
      ans++;
    }else if( t[i-1] > t[i] && t[i] > t[i+1] ){
      swap(t[i],t[i+1]);
      ans++;
    }
  }
  return ans;
}

int main(){
  cin>>n;
  t.resize(n);
  for(int i=0;i<n;i++){
    cin>>t[i];
  }
  u=t;
  
  int ans = solve();

  t=u;
  swap(t[0],t[1]);
  ans  = min( solve() + 1 , ans);

  t=u;
  ans = min( solve2(), ans );
  
  t=u;
  swap(t[0],t[1]);
  ans = min( solve2()+1 , ans);
  
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3656
final_rank: 11

code_snippet: #include<bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define eb pb
#define rep(X,Y) for(int X=0;X<(Y);++X)
#define reps(X,O,Y) for(int X=O;X<(Y);++X)
#define all(X) (X).begin(),(X).end()
using namespace std;
using ll=long long;
const ll MOD=1e9+7;

map<int,int> dp[112345];

int main(){
	int n;
	cin>>n;
	vector<int> a(n);
	rep(i,n) cin>>a[i];
	dp[0][-MOD]=dp[0][MOD]=0;
	auto upd=[&](map<int,int> &nxt,int a,int b){
						 if(nxt.count(a)==0 || nxt[a]>b) nxt[a]=b;
					 };
	auto ok=[](int a,int b,int c){ return(a<b)!=(b<c);};
	rep(i,n-1){
		assert(dp[i].size()<10);
		for(auto p:dp[i]){
			if(ok(p.X,a[i],a[i+1])){
				upd(dp[i+1],a[i],p.Y);
			}
			if((p.X<a[i])==(p.X<a[i+1]) && ok(p.X,a[i+1],a[i])){
				if(i+2==n || ok(a[i+1],a[i],a[i+2])) upd(dp[i+2],a[i],p.Y+1);
			}
			if(i+2<n && ok(p.X,a[i],a[i+2]) && ok(a[i],a[i+2],a[i+1])){
				if(i+3==n || ok(a[i+2],a[i+1],a[i+3])) upd(dp[i+3],a[i+1],p.Y+1);
			}
		}
	}
	int re=MOD;
	for(auto p:dp[n-1]) re=min(re,p.Y);
	for(auto p:dp[n]) re=min(re,p.Y);
	cout<<re<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 13092
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<int>a(n);
    for(int i=0;i<n;i++)cin >> a[i];
    vector<int>b(n);
    b = a;
    int cnt = 0;
    for(int i=1;i<n;i++){
        if(i%2==1){
            if(b[i-1]<b[i]){
                continue;
            }else{
                cnt++;
                if(i!=n-1){
                   if(b[i-1]<b[i+1]&&b[i+1]>b[i]){
                       swap(b[i],b[i+1]);
                   }else{
                       swap(b[i],b[i-1]);
                   }
                }else{
                    swap(b[i],b[i-1]);
                }
            }
        }else{
            if(b[i-1]<b[i]){
                cnt++;
                if(i!=n-1){
                   if(b[i-1]>b[i+1]&&b[i+1]<b[i]){
                       swap(b[i],b[i+1]);
                   }else{
                       swap(b[i],b[i-1]);
                   }
                }else{
                    swap(b[i],b[i-1]);
                }
            }else{
                continue;
            }
        }
    }
    // for(auto x:b){
    //     cout << x << " ";
    // }
    // cout << endl;
    b = a;
    int cnt2 =  0;
    for(int i=1;i<n;i++){
        if(i%2==0){
            if(b[i-1]<b[i]){
                continue;
            }else{
                cnt2++;
                if(i!=n-1){
                   if(b[i-1]<b[i+1]&&b[i+1]>b[i]){
                       swap(b[i],b[i+1]);
                   }else{
                       swap(b[i],b[i-1]);
                   }
                }else{
                    swap(b[i],b[i-1]);
                }
            }
        }else{
            if(b[i-1]<b[i]){
                cnt2++;
                if(i!=n-1){
                   if(b[i-1]>b[i+1]&&b[i+1]<b[i]){
                       swap(b[i],b[i+1]);
                   }else{
                       swap(b[i],b[i-1]);
                   }
                }else{
                    swap(b[i],b[i-1]);
                }
            }else{
                continue;
            }
        }
    }
    // for(auto x:b){
    //     cout << x << " ";
    // }
    // cout << endl;
    cout << min(cnt,cnt2) << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3612
final_rank: 8