fork download
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. using ll = long long;
  7. const int MOD = 1000000007;
  8. const int MOD2 = 998244353;
  9. const ll INF = 1e18;
  10. const int MX = 1000001; //check the limits, dummy
  11.  
  12.  
  13. ll modExp(ll base, ll power) {
  14. if (power == 0) {
  15. return 1;
  16. } else {
  17. ll cur = modExp(base, power / 2); cur = cur * cur; cur = cur % MOD;
  18. if (power % 2 == 1) cur = cur * base;
  19. cur = cur % MOD;
  20. return cur;
  21. }
  22. }
  23.  
  24. ll inv(ll base) {
  25. return modExp(base, MOD-2);
  26. }
  27.  
  28.  
  29. ll mul(ll A, ll B) {
  30. return (A*B)%MOD;
  31. }
  32.  
  33. ll add(ll A, ll B) {
  34. return (A+B)%MOD;
  35. }
  36.  
  37. ll dvd(ll A, ll B) {
  38. return mul(A, inv(B));
  39. }
  40.  
  41. ll sub(ll A, ll B) {
  42. return (A-B+MOD)%MOD;
  43. }
  44.  
  45. ll* facs = new ll[MX];
  46. ll* facInvs = new ll[MX];
  47.  
  48. ll choose(ll a, ll b) {
  49. if (b > a) return 0;
  50. if (a < 0) return 0;
  51. if (b < 0) return 0;
  52. ll cur = facs[a];
  53. cur = mul(cur, facInvs[b]);
  54. cur = mul(cur, facInvs[a-b]);
  55. return cur;
  56. }
  57.  
  58. void initFacs() {
  59. facs[0] = 1;
  60. facInvs[0] = 1;
  61. for (int i = 1 ; i < MX ; i ++ ) {
  62. facs[i] = (facs[i-1] * i) % MOD;
  63. facInvs[i] = inv(facs[i]);
  64. }
  65. }
  66. int main() {
  67. ios_base::sync_with_stdio(0); cin.tie(0);
  68.  
  69. int t ; cin >> t;
  70. while (t --) {
  71. int n ; cin >> n;
  72. vector<int> arr(n);
  73. for (int i = 0 ; i < n; i ++) {
  74. cin >> arr[i];
  75. }
  76. int buildfirst = 0;
  77. for (int i = 1 ; i < n - 1 ; i += 2) {
  78. int diff = (arr[i] - (max(arr[i - 1],arr[i + 1])));
  79. if (diff <= 0) {
  80. buildfirst += (-1 * diff + 1);
  81. }
  82. }
  83.  
  84. if ((n % 2) == 1) {
  85. cout << buildfirst << '\n';
  86. continue;
  87. }
  88. int buildsecond = 0;
  89.  
  90. for (int i = 2 ; i < n - 1 ; i += 2) {
  91. int diff = (arr[i] - (max(arr[i - 1],arr[i + 1])));
  92. if (diff <= 0) {
  93. buildsecond += (-1 * diff + 1);
  94. }
  95. }
  96. cout << min(buildfirst, buildsecond) << '\n';
  97. }
  98. return 0;
  99. }
  100.  
Success #stdin #stdout 0s 5320KB
stdin
6
3
2 1 2
5
1 2 1 4 3
6
3 1 4 5 5 2
8
4 2 1 3 5 3 6 1
6
1 10 1 1 10 1
8
1 10 11 1 10 11 10 1
stdout
2
0
3
3
10
4