Advertisement
bibaboba12345

Untitled

Jan 3rd, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. // clang-format off
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <bitset>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <random>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. #include <deque>
  13. #include <string>
  14. #include <cassert>
  15.  
  16. using namespace std;
  17. const int N = 5e5+7, MOD = 1e9+7, C = 1e6;
  18. #define int long long
  19.  
  20. int a[N], prefsum[N];
  21.  
  22. multiset<int> s;
  23.  
  24. void solve() {
  25. int n,m;
  26. cin >> n >> m;
  27. m--;
  28. s.clear();
  29. for (int i = 0; i < n; i++) {
  30. cin >> a[i];
  31. if (i == 0) {
  32. prefsum[i] = a[i];
  33. }
  34. else {
  35. prefsum[i] = a[i] + prefsum[i - 1];
  36. }
  37. }
  38. int cnt = 0;
  39. s.insert(a[m]);
  40. int add = 0;
  41. for (int i = m - 1; i >= 0; i--) {
  42. while(prefsum[i] < prefsum[m] + add) {
  43. auto it = s.end();
  44. it--;
  45. int b = *it;
  46. s.erase(s.find(b));
  47. add += b * -2;
  48. cnt++;
  49. }
  50. s.insert(a[i]);
  51. }
  52. add = 0;
  53. s.clear();
  54. for (int i = m + 1; i < n; i++) {
  55. s.insert(a[i]);
  56. while (prefsum[i] < prefsum[m] + add) {
  57. auto it = s.begin();
  58. int b = *it;
  59. s.erase(s.find(b));
  60. add += b * 2;
  61. cnt++;
  62. }
  63. }
  64. cout << cnt << "\n";
  65. }
  66.  
  67. signed main() {
  68. #ifdef _DEBUG
  69. freopen("input.txt", "r", stdin);
  70. #else
  71. std::ios::sync_with_stdio(false);
  72. cin.tie(0);
  73. #endif
  74. int t;
  75. cin >> t;
  76. while (t--) {
  77. solve();
  78. }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement