Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int min(int a, int b){
  6. if(a > b) return b;
  7. else return a;
  8. }
  9.  
  10.  
  11.  
  12. int main(void)
  13. {
  14. int n;
  15. scanf("%d", &n);
  16. int sR=0, fL=0, fR=0, ans=0;
  17. int kl;
  18. while(n-->0){
  19. scanf("%d", &kl);
  20. if(kl == 0){
  21. fL++;
  22. fR++;
  23. } else if(kl > 0){
  24. if(sR != 0) {
  25. ans+=min(sR, fR+1);
  26. }
  27. sR = kl;
  28. fR = 0;
  29. fL = 1;
  30. } else {
  31. fR++;
  32. if(sR == 0){
  33. ans+=min(abs(kl), fL);
  34. fL=1;
  35. fR=0;
  36. } else {
  37. if(sR >= fR && abs(kl) >= fL){
  38. ans += fR+1;
  39. fR=0;
  40. fL=0;
  41. } else if ((sR >= fR && abs(kl) < fL) || (sR < fR && abs(kl) >= fL)){
  42. ans+=fR;
  43. fL=1;
  44. fR=0;
  45. } else if(abs(kl)+sR >= fR-1){
  46. ans+=fR-1;
  47. fR=0;
  48. fL=1;
  49. } else {
  50. ans+=abs(kl)+sR;
  51. fR=0;
  52. fL=1;
  53. }
  54. sR = 0;
  55. }
  56. }
  57. }
  58. if(sR != 0 && fR != 0) ans+=min(sR, fR);
  59. printf("%d", ans);
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement