A Medium Number
解题思路
排序即可
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
ll a[3];
void solve() {
cin >> a[0] >> a[1] >> a[2];
sort(a, a + 3);
cout << a[1] << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
T = 1;
cin >> T;
while (T--) solve();
return 0;
}
B Atilla's Favorite Problem
解题思路
不断更新出现最大的字母
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
ll n;
string s;
void solve() {
cin >> n >> s;
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, s[i] - 'a' + 1);
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
T = 1;
cin >> T;
while (T--) solve();
return 0;
}
C Advantage
解题思路
贪心,直接排序,第一个特判
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
ll n;
struct node {
ll v, i;
} a[N];
bool cmp1(node a, node b) {
if (a.v == b.v) return a.i < b.i;
return a.v > b.v;
}
bool cmp2(node a, node b) {
return a.i < b.i;
}
vector<node>ans;
void solve() {
ans.clear();
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].v, a[i].i = i;
sort(a + 1, a + n + 1, cmp1);
ans.push_back({a[1].v - a[2].v, a[1].i});
for (int i = 2; i <= n; i++) ans.push_back({a[i].v - a[1].v, a[i].i});
sort(ans.begin(), ans.end(), cmp2);
for (auto i : ans) cout << i.v << ' ';
cout << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
T = 1;
cin >> T;
while (T--) solve();
return 0;
}
D Challenging Valleys
解题思路
模拟,按题目条件来
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
ll n;
ll a[N];
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int ok = 0;
a[n + 1] = -1;
int r = 1;
for (int l = 1; l <= n; l++) {
for (int i = l; i <= n + 1; i++) {
if (a[i] != a[l]) {
r = i - 1;
break;
}
}
if ((l == 1 || a[l - 1] > a[l]) && (r == n || a[r] < a[r + 1])) ok++;
l = r;
}
if (ok == 1) cy;
else cn;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
T = 1;
cin >> T;
while (T--) solve();
return 0;
}
E Binary Inversions
解题思路
考虑前后缀,对于每个位置的变化,记录最大值
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
ll n;
ll a[N];
ll ans;
void solve() {
ans = 0;
cin >> n;
ll f1[n + 5] = {};
ll f0[n + 5] = {};
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
f1[i] = f1[i - 1];
if (a[i] == 1) f1[i]++;
}
for (int i = n; i >= 1; i--) {
f0[i] = f0[i + 1];
if (a[i] == 0) f0[i]++;
}
ll res = 0;
for (int i = 1; i <= n; i++) if (a[i] == 1) res += f0[i];
ans = res;
for (int i = 1; i <= n; i++) {
if (a[i] == 1) ans = max(ans, res + f1[i - 1] - f0[i + 1]);
if (a[i] == 0) ans = max(ans, res + f0[i + 1] - f1[i - 1]);
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
T = 1;
cin >> T;
while (T--) solve();
return 0;
}
F Quests
解题思路
发现有单调性,直接二分(注意check函数的写法)
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
const int N = 2e5 + 5;
ll T;
ll n, c, d;
ll a[N];
ll ans;
string s1 = "Infinity", s2 = "Impossible";
bool check(ll x) {
ll sum = 0;
for (int i = 1; i <= min(x + 1, n); i++) {
sum += ceil((double)(d - i + 1) / (double) (x + 1)) * a[i];
}
if (sum >= c) return 1;
else return 0;
}
ll find() {
ll l = 0, r = d + 1;
while (l <= r) {
ll mid = (l + r) >> 1;
if (check(mid)) l = mid + 1;
else r = mid - 1;
}
return l - 1;
}
bool cmp(ll a, ll b) {
return a > b;
}
void solve() {
cin >> n >> c >> d;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1, cmp);
ans = find();
if (ans == d + 1) {
cout << s1 << endl;
return;
}
if (ans == -1) {
cout << s2 << endl;
return;
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
T = 1;
cin >> T;
while (T--) solve();
return 0;
}