AtCoder Beginner Contest 340 题解
AtCoder Beginner Contest 340 题解
A - Arithmetic Progression
模拟即可。
// 2024/2/11 PikachuQAQ
#include <iostream>
using namespace std;
int a, b, d;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> a >> b >> d;
for (int i = a; i <= b; i += d) {
cout << i << ' ';
}
return 0;
}
B - Append
用 vector
模拟即可。
// 2024/2/11 PikachuQAQ
#include <iostream>
#include <vector>
using namespace std;
int q, t, x;
vector<int> v;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
for (cin >> q; q--; ) {
cin >> t >> x;
if (t == 1) {
v.push_back(x);
} else {
cout << v[v.size() - x] << '\n';
}
}
return 0;
}
C - Divide and Divide
丢到 OEIS 里得到规律即可。
// 2024/2/11 PikachuQAQ
#include <iostream>
using namespace std;
typedef long long ll;
ll n, s, i, z;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
s = n - 1, i = n - 1, z = 1;
while (i >= 0) {
s += i, i -= z, z += z;
}
cout << s;
return 0;
}
D - Super Takahashi Bros.
我们可以把这个问题转化为最短路问题。
对于每个点
// 2024/2/11 PikachuQAQ
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int kMaxN = 2e5 + 7;
const ll INF = 1e18;
struct E {
ll v, w;
friend bool operator > (const E& a, const E& b) {
return a.w > b.w;
}
};
int n, m;
ll dis[kMaxN];
bool vis[kMaxN];
vector<E> g[kMaxN];
priority_queue<E, vector<E>, greater<E> > q;
void Dijkstra(int s) {
fill(dis + 1, dis + n + 1, INF);
dis[s] = 0;
q.push({s, 0});
while (q.size()) {
int u = q.top().v; q.pop();
if (vis[u] == 0) {
vis[u] = 1;
for (E e : g[u]) {
ll v = e.v, w = e.w;
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
q.push({v, dis[v]});
}
}
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1, a, b, c; i < n; i++) {
cin >> a >> b >> c;
g[i].push_back({i + 1, a});
g[i].push_back({c, b});
}
Dijkstra(1);
cout << dis[n];
return 0;
}
E - Mancala 2
可以把操作转为给每个
// 2024/2/11 PikachuQAQ
#include <iostream>
using namespace std;
typedef long long ll;
const int kMaxN = 2e5 + 7;
ll n, m, c[kMaxN];
int lowbit(int x) {
return x & -x;
}
void Update(ll x, ll y) {
for (int i = x; i <= n; i += lowbit(i)) {
c[i] += y;
}
}
ll Query(int x) {
ll res = 0;
for (int i = x; i; i -= lowbit(i)) {
res += c[i];
}
return res;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1, a, x = 0; i <= n; i++) {
cin >> a;
Update(i, a - x), x = a;
}
for (ll i = 1, b, x; i <= m; i++) {
cin >> b, b++, x = Query(b);
Update(b, -x), Update(b + 1, x);
if (x > n - b) {
x -= n - b;
Update(b + 1, 1), Update(1, x / n + 1);
Update(x % n + 1, -1);
} else {
Update(b + 1, 1), Update(b + x + 1, -1);
}
}
for (int i = 1; i <= n; i++) {
cout << Query(i) << ' ';
}
return 0;
}
F - S = 1
注意到已知其中两个点坐标分别为
我们通过 exgcd 得到了一对解,为
// 2024/2/11 PikachuQAQ
#include <iostream>
using namespace std;
typedef long long ll;
ll a, b, x, y, g;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1, y = 0;
return a;
} else {
ll d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> a >> b, g = exgcd(a, b, y, x);
if (abs(g) > 2) {
cout << "-1";
return 0;
}
x *= -2 / g, y *= 2 / g;
cout << x << ' ' << y;
return 0;
}
G - Leaf Color
不会虚树。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!