A. Robin Helps
顺着题意做一遍模拟即可,代码如下:
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
| #include <bits/stdc++.h> #define endl '\n' using ll = long long; using namespace std;
int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; ll temp = 0; ll ans = 0; vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] >= k) { temp += a[i]; } if (a[i] == 0) { if (temp > 0) { ans++; temp--; } } } cout << ans << endl; } return 0; }
|
B. Robin Hood and the Major Oak
此题硬写会超时,判断奇偶性即可,代码如下:
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
| #include <bits/stdc++.h> #define endl '\n' using ll = long long; using namespace std; const ll MOD = 1e9 + 5; int t;
int main() { cin >> t; while (t--) { ll cnt = 0; ll n, k; cin >> n >> k; if (n % 2 == 0) { if ((k / 2) % 2 == 0) { cout << "YES" << endl; } else { cout << "NO" << endl; } } else { if ((ll)ceil(k * 1.0 / 2) % 2 == 0) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } return 0; }
|
C. Robin Hood in Town
先输入钱数并进行排序,找到满足题意的临界点的那个人,将他的钱数 * 2 * n得到满足题目的条件的sum,然后将以输入的实际钱数求和,与sum作比较,得出答案。(需要注意的是,当n == 1 || n == 2时, 需要输出-1, 此为特判)
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
| #include <bits/stdc++.h> #define endl '\n' using ll = long long; using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; vector<ll> a(n); ll sum2 = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum2 += a[i]; } if (n <= 2) { cout << -1 << endl; continue; } sort(a.begin(), a.end()); ll mid = n / 2; ll sum1 = a[mid] * 2 * n + 1; if (sum1 <= sum2) { cout << 0 << endl; } else { cout << sum1 - sum2 << endl; } } return 0; }
|
D. Robert Hood and Mrs Hood
使用差分数组来计算区间贡献,但需要注意的是,访客探访的时间是d,因此在计算贡献时,应计算l - d + 1。
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
| #include <bits/stdc++.h> #define endl '\n' using ll = long long; using namespace std; vector<ll> diff(200005);
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n, d, k; cin >> n >> d >> k; for (int i = 1; i <= k; i++) { ll l, r; cin >> l >> r; l = max(1ll, l - d + 1); diff[l]++; diff[r + 1]--; } for (int i = 1; i <= n; i++) { diff[i] += diff[i - 1]; } ll maxx = -1, minn = 1e10; ll mum, bro; for (int i = 1; i <= n - d + 1; i++) { if (diff[i] > maxx) { maxx = diff[i]; bro = i; } if (diff[i] < minn) { minn = diff[i]; mum = i; } } cout << bro << " " << mum << endl; for (int i = 0; i <= n + 1; i++) { diff[i] = 0; } } return 0; }
|