#include <bits/stdc++.h>
using namespace std;
long long n,c,res;
const long long MAX=1e8;
const long long mMAX=-1e8;
long long tab[50007],ress[2];
int op[50007];
int main(){
cin>>n;
tab[1]=2;
for(int i=0;i<n;i++)
cin>>tab[i];
// > - 2
// < - 1
bool y=0;
for(int i=1;i<n;i++){
if(tab[i]>tab[i-1])
op[i]=2;
else if(tab[i]<tab[i-1])
op[i]=1;
else{
op[i]=1+(op[i-1]+2)%2;
if(op[i]==1)
tab[i]=mMAX;
if(op[i]==2)
tab[i]=MAX;
res++;
}
if(op[i]==op[i-1])
y=(y+1)%2;
if(tab[i]!=mMAX && tab[i]!=MAX)
ress[y]++;
//cout<<op[i];
}
cout<<res+min(ress[0],ress[1]);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <bits/stdc++.h> using namespace std; long long n,c,res; const long long MAX=1e8; const long long mMAX=-1e8; long long tab[50007],ress[2]; int op[50007]; int main(){ cin>>n; tab[1]=2; for(int i=0;i<n;i++) cin>>tab[i]; // > - 2 // < - 1 bool y=0; for(int i=1;i<n;i++){ if(tab[i]>tab[i-1]) op[i]=2; else if(tab[i]<tab[i-1]) op[i]=1; else{ op[i]=1+(op[i-1]+2)%2; if(op[i]==1) tab[i]=mMAX; if(op[i]==2) tab[i]=MAX; res++; } if(op[i]==op[i-1]) y=(y+1)%2; if(tab[i]!=mMAX && tab[i]!=MAX) ress[y]++; //cout<<op[i]; } cout<<res+min(ress[0],ress[1]); } |
English