#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;
cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++) cin>>a[i];
int prev = 0;
vector<pair<int,int> > arr;
for(int i=1;i<n;i++){
if(a[i]!=a[i-1]){
arr.push_back({i-prev,a[prev]});
prev = i;
}
}
arr.push_back({n-prev,a[prev]});
int N = arr.size();
int l = 0;
int r = N;
int cnt = 0;
for(int i=N-1;i>=0;i--){
if(arr[i].second==-1 && arr[i].first%2) {
l = i+1;
break;
}
}
for(int i=0;i<N;i++){
if(arr[i].second==1 && arr[i].first%2) {
r = i;
break;
}
}
for(int i=0;i<N;i++) if(arr[i].second==-1) cnt+=arr[i].first/2;
int minn = 1e9;
if(l>r){
cout<<-1<<"\n";
return 0;
}
for(int i=0;i<l;i++){
if(arr[i].second==-1) cnt-=arr[i].first/2;
else cnt+=arr[i].first/2;
}
minn = cnt;
for(int i=l;i<r;i++){
if(arr[i].second==-1) cnt-=arr[i].first/2;
else cnt+=arr[i].first/2;
minn = min(cnt,minn);
}
cout<<minn<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |