Educational Codeforces Round 184 (Rated for Div. 2)(A-D1)
题目链接Educational Codeforces Round 184 (Rated for Div. 2)
A.Alice and Bob
思路
贪心,以a为分界点,如果左边的元素多余右边的就输出a - 1,否则输出a + 1,注意等于a的元素要忽略不计,赛时这里wa了一发
代码
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << "=" << x << '\n';
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a));
#define int long long
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 5;void solve() {int n, x;cin >> n >> x;vector<int> a(n);for (int i = 0; i < n; i++) cin >> a[i];sort(a.begin(), a.end());int t = 0;while (t < n && a[t] < x) {t++;}int m = n;for (int i = t; i < n; i++) {if (a[i] == x) m--;}if (t > m - t) {cout << x - 1 << endl;return;}else if (t <= n - t) {cout << x + 1 << endl;return;}return;
}signed main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int T = 1;cin >> T;while (T--) solve();return 0;
}
B.Drifting Away
思路
依旧贪心加模拟,能到达岸边的情况很少,依次枚举即可,需注意的是当是<<<*>>>这种类型时也可以到达岸边,赛事被这个卡了1个小时,呜呜呜,导致C题赛后5分钟才提交通过,痛
代码
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << "=" << x << '\n';
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a));
#define int long long
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 5;void solve() {string s;cin >> s;int n = s.size();if (n == 1) {cout << 1 << endl;return;}int l = 0, r = n - 1;if (s[0] == '*') {for (int i = 1; i < n; i++) {if (s[i] != '>') {cout << -1 << endl;return;}}cout << n << endl;return;}else if (s[0] == '>') {for (int i = 0; i < n; i++) {if (s[i] != '>') {cout << -1 << endl;return;}}cout << n << endl;return;}else {while (l + 1 < n && s[l + 1] == '<') {l++;}}if (s[n - 1] == '*') {for (int i = n - 2; i >= 0; i--) {if(s[i] != '<') {cout << -1 << endl;return;}}cout << n << endl;return;}else if (s[n - 1] == '<') {for (int i = 0; i < n; i++) {if (s[i] != '<') {cout << -1 << endl;return;}}cout << n << endl;return;}else {while (r - 1 >= 0 && s[r - 1] == '>') {r--;}}if (l + 1 == r) {cout << max(l + 1, n - r) << endl;return;}else if (r - l == 2 && s[l + 1] == '*') {int ans = max(l + 1, n - r) + 1;cout << ans << endl;return;}cout << -1 << endl;return;
}signed main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int T = 1;cin >> T;while (T--) solve();return 0;
}
C.Range Operation
思路
用到了数学思维,用数组p表示数组的前缀和,根据题目要求就是要
(l + r) * (r - l + 1) - (p[r] - p[l - 1])最大,那么就将该式子转换成求
(r * r + r - p[r]) + (l - l * l + p[l - 1])的最大值,两部分分开依次求最大值,需要注意的是r>=l,就因为我把这部分一开始给忽略了,导致赛时没ac,赛后5分钟才成功ac
代码
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << "=" << x << '\n';
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a));
#define int long long
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e18 + 5;void solve() {int n;cin >> n;vector<int> p(n + 1, 0);vector<int> a(n + 1);int ans = 0;for (int i = 1; i <= n; i++) {cin >> a[i];ans += a[i];p[i] = p[i - 1] + a[i];}int mx = -N;vector<int> f(n + 1, -N), g(n + 1, -N);for (int i = 1; i <= n; i++) {int t = i * i + i - p[i];g[i] = t;int y = i - i * i + p[i - 1];f[i] = max(f[i - 1], y);}int sum = 0;for (int i = 1; i <= n; i++) {int t = g[i] + f[i];sum = max(t, sum);}ans += sum;cout << ans << endl;return;
}signed main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int T = 1;cin >> T;while (T--) solve();return 0;
}
D1.Removal of a Sequence (Easy Version)
思路
二分答案,注意要答案要尽量小
代码
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << "=" << x << '\n';
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a));
#define int long long
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e12;int x, y, k;int check(int mid) {for (int i = 1; i <= x; i++) {mid -= mid / y;}return mid;
}void solve() {cin >> x >> y >> k;if (k < y) {cout << k << endl;return;}int ans = -1;int l = 1, r = N;while (l <= r) {int mid = (l + r) >> 1;if (check(mid) >= k) {ans = mid;r = mid - 1;//往更小的满足条件的找}else {l = mid + 1;}}cout << ans << endl;return;
}signed main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int T = 1;cin >> T;while (T--) solve();return 0;
}
D2.Removal of a Sequence (Hard Version)
补充
等我之后补
