Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // clang-format off
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <iomanip>
- #include <bitset>
- #include <vector>
- #include <algorithm>
- #include <random>
- #include <map>
- #include <string>
- #include <set>
- #include <deque>
- #include <string>
- #include <cassert>
- using namespace std;
- const int N = 5e5+7, MOD = 1e9+7, C = 1e6;
- #define int long long
- int a[N], prefsum[N];
- multiset<int> s;
- void solve() {
- int n,m;
- cin >> n >> m;
- m--;
- s.clear();
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- if (i == 0) {
- prefsum[i] = a[i];
- }
- else {
- prefsum[i] = a[i] + prefsum[i - 1];
- }
- }
- int cnt = 0;
- s.insert(a[m]);
- int add = 0;
- for (int i = m - 1; i >= 0; i--) {
- while(prefsum[i] < prefsum[m] + add) {
- auto it = s.end();
- it--;
- int b = *it;
- s.erase(s.find(b));
- add += b * -2;
- cnt++;
- }
- s.insert(a[i]);
- }
- add = 0;
- s.clear();
- for (int i = m + 1; i < n; i++) {
- s.insert(a[i]);
- while (prefsum[i] < prefsum[m] + add) {
- auto it = s.begin();
- int b = *it;
- s.erase(s.find(b));
- add += b * 2;
- cnt++;
- }
- }
- cout << cnt << "\n";
- }
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- #else
- std::ios::sync_with_stdio(false);
- cin.tie(0);
- #endif
- int t;
- cin >> t;
- while (t--) {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement