Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- int min(int a, int b){
- if(a > b) return b;
- else return a;
- }
- int main(void)
- {
- int n;
- scanf("%d", &n);
- int sR=0, fL=0, fR=0, ans=0;
- int kl;
- while(n-->0){
- scanf("%d", &kl);
- if(kl == 0){
- fL++;
- fR++;
- } else if(kl > 0){
- if(sR != 0) {
- ans+=min(sR, fR+1);
- }
- sR = kl;
- fR = 0;
- fL = 1;
- } else {
- fR++;
- if(sR == 0){
- ans+=min(abs(kl), fL);
- fL=1;
- fR=0;
- } else {
- if(sR >= fR && abs(kl) >= fL){
- ans += fR+1;
- fR=0;
- fL=0;
- } else if ((sR >= fR && abs(kl) < fL) || (sR < fR && abs(kl) >= fL)){
- ans+=fR;
- fL=1;
- fR=0;
- } else if(abs(kl)+sR >= fR-1){
- ans+=fR-1;
- fR=0;
- fL=1;
- } else {
- ans+=abs(kl)+sR;
- fR=0;
- fL=1;
- }
- sR = 0;
- }
- }
- }
- if(sR != 0 && fR != 0) ans+=min(sR, fR);
- printf("%d", ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement