AtCoder Beginner Contest 128
AtCoder Beginner Contest 128
https://atcoder.jp/contests/abc128
A - Apple Pie
#include <bits/stdc++.h>
using namespace std;
int main () {
int a, b;
cin >> a >> b;
cout << (a * 3 + b) / 2;
}
B - Guidebook
#include <bits/stdc++.h>
using namespace std;
struct Node {
string name;
int id, num;
bool operator < (const Node &t) const {
if (name != t.name) return name < t.name;
return num > t.num;
}
};
int main () {
vector <Node> v;
int n; cin >> n;
for (int i = 1; i <= n; i++) {
string s;
int num;
cin >> s >> num;
v.push_back ({s, i, num});
}
sort (v.begin (), v.end ());
//for (auto i : v) cout << i.name << ' ' << i.num << endl;
for (auto i : v) cout << i.id << endl;
}
C - Switches
#include <bits/stdc++.h>
using namespace std;
const int N = 15;
int p[N], a[N][N], b[N], ans;
int n, m;
void dfs (int x) {
if (x > n) {
for (int i = 1; i <= m; i++) {
int sum = 0;
for (int j = 1; j <= a[i][0]; j++) {
if (b[a[i][j]]) sum ++;
}
if (sum % 2 != p[i]) return ;
}
ans ++;
return ;
}
b[x] = 0, dfs (x + 1);
b[x] = 1, dfs (x + 1);
}
int main () {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a[i][0];
for (int j = 1; j <= a[i][0]; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= m; i++) cin >> p[i];
dfs (1);
cout << ans;
}
//写个暴力试试
D - equeue
贪心
#include <bits/stdc++.h>
using namespace std;
const int N = 55, M = 105;
int a[N];// f[N][N][M]; //f[i][j][k]: 左边选i个,右边选j个,总代价为k
int n, m, ans, sum1[N], sum2[N];
int main () {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) sum1[i] = sum1[i-1] + a[i];
for (int i = n; i >= 1; i--) sum2[i] = sum2[i+1] + a[i];
vector <int> v;
for (int i = 0; i <= min(n, m); i++) {
v.clear ();
for (int j = 1; j <= i; j++) v.push_back (a[j]);
for (int j = 0; i + j <= min (n, m); j++) {
if (j && i != n - j + 1) v.push_back (a[n-j+1]);
sort (v.begin (), v.end ());
//for (auto kk : v) cout << kk << ' ';cout << endl;
int dx = sum1[i] + sum2[n-j+1];
if (i == n - j + 1) dx -= a[i];
for (int k = 0; k < min (m - i - j, (int)v.size ()); k++) {
if (v[k] >= 0) break;
dx -= v[k];
}
ans = max (ans, dx);
}
}
cout << ans << endl;
}
E - Roadwork
数据结构题。
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, ans[N];
priority_queue<int, vector<int>, greater<int>> add, del; //最近最小坐标
struct Node {
int op; //0:add, 1:query, 2:del
int tt, x;
bool operator<(const Node &t) const {
if (tt != t.tt) return tt < t.tt; //先找时间小的
return op < t.op;
}
};
int main () {
vector<Node> v;
cin >> n >> m;
for (int i = 0; i < n; i++) {
int st, ed, x;
cin >> st >> ed >> x;
st -= x, ed -= (x + 1);
v.push_back ({0, st, x}), v.push_back ({2, ed, x});
}
for (int i = 0; i < m; i++) {
int tt; cin >> tt;
v.push_back ({1, tt, i});
}
sort (v.begin (), v.end ());
for (auto i : v) {
int op = i.op, tt = i.tt, x = i.x;
if (op == 0) add.push (x);
else if (op == 2) del.push (x);
else {
while (!add.empty () && !del.empty () && add.top() == del.top()) {
add.pop (), del.pop ();
}
if (add.empty ()) ans[x] = -1;
else ans[x] = add.top ();
}
}
for (int i = 0; i < m; i++) cout << ans[i] << endl;
}
//[s-x,t-x-1]
F - Frog Jump
题意
\(2\) 次为一组,第一次往右移 \(A\) 步,第二次往左移 \(B\) 步,最终到达 \(n−1\),每个点最多被经过一次,求经过的点的最大和。
分析
固定步长 \(x=A-B\)
往右走:\(0, x, 2x,...\)
往左走:\(n-1, n-1-x, n-1-2x, ...\)
故枚举步长,计算差值,排除越界 + 重复经过。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, a[N], ans;
signed main () {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int x = 1; x < n; x++) { //枚举步长
int sum = 0;
for (int r = x; r < n; r += x) { //模拟奇数步往右
int l = n - 1 - r; //模拟偶数步往左
if (l <= x || (r >= l && l % x == 0)) break; //往左越过起点出界 or 奇偶重复经过
sum += a[l] + a[r], ans = max (ans, sum);
}
}
cout << ans << endl;
}
//x = A - B
//奇: 0, x, 2x, ... (往右)
//偶:n-1, n-1-x, n-1-2x, ... (往左)