fork download
  1. #include <bits/stdc++.h>
  2. #include <string>
  3. using namespace std;
  4. #define ll long long
  5. #define dd double
  6. #define ld long double
  7. #define pi 3.14159265359
  8. #define ull unsigned long long
  9. #define yes cout<<"YES\n"
  10. #define no cout<<"NO\n"
  11. #define el "\n"
  12. #define Arwa ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  13. #define fix(x) cout<<fixed<<setprecision(x)
  14. ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
  15. ll lcm(int a,int b) {return a/gcd(a,b)*b;}
  16. //here we go again
  17. int main()
  18. {
  19. Arwa
  20. int t=1;
  21. //cin>>t;
  22. while(t--)
  23. {
  24. int n;
  25. cin>>n;
  26. int arr[n],c=-1,min=1000000000;
  27. int dx[]={-1,-1,-1,0,0,0,1,1,1},dy[]={-1,0,1,-1,0,1,-1,0,1};
  28. for(int i=0;i<n;i++)
  29. cin>>arr[i];
  30. for(int i=0;i<9;i++)
  31. {
  32. c=0;
  33. int diff=(arr[1]+dy[i])-(arr[0]+dx[i]);
  34. if(dy[i]!=0) c++;
  35. if(dx[i]!=0) c++;
  36. for(int j=2;j<n;j++)
  37. {
  38. if(arr[j]-arr[j-1]==diff)
  39. continue;
  40. else if((arr[j]+dx[i])-(arr[j-1]+dy[i])==diff)
  41. {
  42. if(dy[i]!=0) c++;
  43. j++;
  44. }
  45. else
  46. if((arr[j]+dy[i])-(arr[j-1]+dx[i])==diff)
  47. {
  48. if(dx[i]!=0) c++;
  49. j++;
  50. }
  51. else
  52. {
  53. c=-1;
  54. break;
  55. }
  56. }
  57. if(c!=-1&&min>c)
  58. min=c;
  59. }
  60. if(min==1000000000)
  61. cout<<-1<<el;
  62. else
  63. cout<<min<<el;
  64. }
  65. return 0;
  66. }
  67. /*
  68. 4
  69. 24 21 14 10
  70. 21 24
  71. */
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
-1