-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1955C.cpp
More file actions
68 lines (61 loc) · 1.58 KB
/
1955C.cpp
File metadata and controls
68 lines (61 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#define MAXN 2000000000
#define llu unsigned long long
#include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
int main() {
#ifdef DEBUG
freopen("in.in", "r", stdin);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
ll n, k;
cin >> n >> k;
vector<ll> v(n, 0);
for (auto &vv : v)
cin >> vv;
ll ans = 0;
ll left = 0, right = v.size() - 1;
while (left < right && k > 0) {
ll to_remove = min(v[left], v[right]);
ll victim = to_remove == v[left] ? left : right;
bool both = v[left] == v[right];
if (k - 2 * to_remove >= 0) {
if (both) {
left++;
right--;
ans += 2;
} else if (victim == left) {
v[right] -= to_remove;
left++;
ans++;
} else {
v[left] -= to_remove;
right--;
ans++;
}
k -= 2 * to_remove;
} else {
// k < 2 * to_remove
if (k + 1 == 2 * to_remove && victim == left)
ans++;
break;
}
}
bool only_one = left == right;
if (only_one && k >= v[left]) {
ans++;
}
cout << ans << endl;
}
return 0;
}