A
记录每道题的最多选项个数
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 5;
int a[maxn], op[maxn][10];
char in[maxn];
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
int n, m; cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> in;
for (int j = 0; j < m; ++j)
op[j][in[j] - 'A']++;
}
for (int j = 0; j < m; ++j)
for (int k = 0; k < 5; ++k)
a[j] = max(a[j], op[j][k]);
int ans = 0;
for (int i = 0; i < m; ++i) {
int x; cin >> x;
ans += x * a[i];
}
cout << ans << endl;
}
B
找规律:和要为偶数且最大值要小于等于和的一半
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
ll a[maxn];
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
ll sum = 0, maxx = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i]; sum += a[i];
maxx = max(a[i], maxx);
}
if (sum & 1) cout << "NO" << endl;
else if (maxx > sum / 2) cout << "NO" << endl;
else cout << "YES" << endl;
}
C
贪心,对原始序列排序,直接从中值位置开始向后加值
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
ll a[maxn];
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
ll n, k; cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
int mid = n / 2;
ll cnt = 1, pre = a[mid];
for (int i = mid + 1; i < n; ++i) {
if (!k) break;
if ((a[i] - pre) * cnt > k) {
pre += k / cnt;
k = 0;
break;
}
else {
k -= (a[i] - pre) * cnt;
pre = a[i]; cnt++;
}
}
pre += k / cnt;
cout << pre << endl;
}
D
贪心dp,每一排就只考虑最左边和最右边两个位置的转移才是最优答案。转移即为从上一行的最左边到该行的最左边、最右边;上一行的最右边到该行的最左边、最右边。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
vector<int> a[maxn];
int b[maxn];
int l[maxn], r[maxn];
ll dp[maxn][4];
ll calc(int y1, int y2, int line) { return (ll)abs(y1 - line) + abs(y2 - line); }
ll dis(int x1, int y1, int x2, int y2)
{
ll d = (ll)abs(x1 - x2);
return d + (ll)(min(min(calc(y1, y2, l[y1]), calc(y1, y2, r[y1])), min(calc(y1, y2, l[y2]), calc(y1, y2, r[y2]))));
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
int n, m, k, q; cin >> n >> m >> k >> q;
for (int i = 0; i < k; ++i) {
int x, y; cin >> x >> y;
a[x].push_back(y);
}
for (int i = 1; i <= q; ++i) cin >> b[i];
sort(b + 1, b + q + 1);
b[0] = -inf, b[q + 1] = inf;
for (int i = 1; i <= m; ++i) {
l[i] = b[lower_bound(b + 1, b + q + 1, i) - b - 1];
r[i] = b[lower_bound(b + 1, b + q + 1, i) - b];
}
sort(a[1].begin(), a[1].end());
if (a[1].empty()) {
dp[1][0] = dp[1][1] = 0;
a[1].push_back(1);
}
else {
dp[1][0] = dp[1][1] = a[1].back() - 1;
a[1][0] = a[1].back();
}
int pre = 1;
for (int i = 2; i <= n; ++i) {
if (!a[i].empty()) {
sort(a[i].begin(), a[i].end());
ll l2l = dp[pre][0] + dis(pre, a[pre].front(), i, a[i].front());
ll l2r = dp[pre][0] + dis(pre, a[pre].front(), i, a[i].back());
ll r2l = dp[pre][1] + dis(pre, a[pre].back(), i, a[i].front());
ll r2r = dp[pre][1] + dis(pre, a[pre].back(), i, a[i].back());
ll tmp = a[i].back() - a[i].front();
dp[i][0] = tmp + min(l2r, r2r);
dp[i][1] = tmp + min(l2l, r2l);
pre = i;
}
}
cout << min(dp[pre][0], dp[pre][1]) << endl;
}