bibaboba12345

Untitled

Jul 18th, 2022
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <bitset>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <map>
  7. #include <set>
  8. #include <cassert>
  9.  
  10. const int N = 3e5 + 7;
  11. using namespace std;
  12. long long n, a[N], m, ans[N], dp[N][2], dp2[N][2];
  13.  
  14. bool maxim(long long& a, long long val) {
  15. if (a < val) {
  16. a = val;
  17. return 1;
  18. }
  19. return 0;
  20. }
  21.  
  22. void solve() {
  23. cin >> n;
  24. for (int i = 0; i < n; i++) {
  25. cin >> a[i];
  26. dp[i][0] = 0;
  27. dp[i][1] = 0;
  28. dp2[i][0] = 1e18;
  29. dp2[i][1] = 1e18;
  30. }
  31. dp[0][0] = 0;
  32. dp[0][1] = 0;
  33. dp2[0][0] = 0;
  34. dp2[0][1] = 0;
  35. for (int i = 1; i < n; i++) {
  36. if (maxim(dp[i][1], dp[i - 1][0] + 1)) {
  37. int mx = max(a[i - 1], a[i + 1]);
  38. int add = max(0LL, mx - a[i] + 1);
  39. dp2[i][1] = dp2[i - 1][0] + add;
  40. }
  41. if (dp[i][1] == dp[i - 1][0] + 1) {
  42. int mx = max(a[i - 1], a[i + 1]);
  43. int add = max(0LL, mx - a[i] + 1);
  44. dp2[i][1] = min(dp2[i - 1][0] + add, dp2[i][1]);
  45. }
  46. if (maxim(dp[i][0], dp[i - 1][0])) {
  47. dp2[i][0] = dp2[i - 1][0];
  48. }
  49. if (dp[i][0] == dp[i - 1][0]) {
  50. dp2[i][0] = min(dp2[i - 1][0], dp2[i][0]);
  51. }
  52. if (maxim(dp[i][0], dp[i - 1][1])) {
  53. dp2[i][0] = dp2[i - 1][1];
  54. }
  55. if (dp[i][0] == dp[i - 1][1]) {
  56. dp2[i][0] = min(dp2[i - 1][1], dp2[i][0]);
  57. }
  58. }
  59. cout << dp2[n - 1][0] << "\n";
  60. }
  61.  
  62. signed main()
  63. {
  64. #ifdef _DEBUG
  65. freopen("input.txt", "r", stdin);
  66. #else
  67. std::ios::sync_with_stdio(false);
  68. cin.tie(0);
  69. #endif
  70.  
  71. int t;
  72. cin >> t;
  73. while (t--) {
  74. solve();
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment