Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. //#pragma GCC optimize("Ofast")
  2. //#pragma GCC optimize("unroll-loops")
  3. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. #include <ext/pb_ds/assoc_container.hpp>
  8. #include <ext/pb_ds/tree_policy.hpp>
  9. #include <ext/pb_ds/detail/standard_policies.hpp>
  10.  
  11. #define pb push_back
  12. #define F first
  13. #define S second
  14. #define lll long long
  15. #define lld long double
  16.  
  17. using namespace std;
  18. using namespace __gnu_pbds;
  19.  
  20. template <typename T>
  21. using ordered_set = tree <T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
  22.  
  23. mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
  24.  
  25. const int N = 1e6 + 11;
  26. const lll M = 1e9 + 7;
  27. const lll M2 = 1e9 + 9;
  28. const int mod = 998244353;
  29. const int rx[8] = {1, 1, -1, -1, -2, -2, 2, 2};
  30. const int ry[8] = {-2, 2, -2, 2, 1, -1, 1, -1};
  31. const lld eps = 1e-7;
  32. const double pi = acos(-1.0);
  33.  
  34. lll a[N];
  35.  
  36. lll dp[N];
  37.  
  38. int k[N];
  39.  
  40. int ans[N];
  41.  
  42. lll pref[N];
  43.  
  44. lll get(int l, int r) {
  45. lll ans = pref[r];
  46. if (l) {
  47. ans -= pref[l - 1];
  48. }
  49. return ans;
  50. }
  51.  
  52. signed main() {
  53. ios_base::sync_with_stdio(0);
  54. cin.tie(0);
  55. cout.tie(0);
  56. srand(time(0));
  57. #ifdef LOCAL
  58. freopen("input.txt", "r", stdin);
  59. freopen("output.txt", "w", stdout);
  60. #else
  61. // freopen("input.txt", "r", stdin);
  62. // freopen("output.txt", "w", stdout);
  63. #endif
  64. int n;
  65. cin >> n;
  66. for (int i = 0; i < n; ++i) {
  67. cin >> a[i];
  68. pref[i] = a[i];
  69. ans[i] = 1;
  70. dp[i] = a[i];
  71. k[i] = 1;
  72. if (i) {
  73. dp[i] += dp[i - 1];
  74. k[i] += k[i - 1];
  75. pref[i] += pref[i - 1];
  76. }
  77. }
  78. int l, r;
  79. for (int i = 0; i + 1 < n; ++i) {
  80. if (dp[i + 1] > dp[i] + a[i + 1]) {
  81. dp[i + 1] = dp[i] + a[i + 1];
  82. k[i + 1] = k[i] + 1;
  83. ans[i + 1] = ans[i];
  84. }
  85. l = i + 1;
  86. r = n - 1;
  87. while (l + 1 < r) {
  88. int mid = (l + r) / 2;
  89. if (get(i + 1, mid) >= dp[i]) {
  90. r = mid;
  91. } else {
  92. l = mid;
  93. }
  94. }
  95. if (get(i + 1, l) < dp[i]) {
  96. l = r;
  97. }
  98. if (get(i + 1, l) >= dp[i]) {
  99. if (dp[l] > get(i + 1, l)) {
  100. dp[l] = get(i + 1, l);
  101. k[l] = l - i;
  102. ans[l] = ans[i] + 1;
  103. }
  104. }
  105. }
  106. cout << ans[n - 1];
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement