AtCoder Beginner Contest 340
B - Append#
难度: ⭐#
题目大意#
有两种操作, 一是在队尾添加一个数, 而是输出倒数第k个数是多少;
解题思路#
模拟就行, 没啥好说的;
神秘代码#
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl '\n'
using namespace std;
const int N = 5e5 + 10, mod = 998244353, inf = 1000000993;
typedef pair<int, int> PII;
int n, m, idx;
int p[N];
signed main(){
cin >> n;
while(n--){
int a, b;
cin >> a >> b;
if(a == 1){
p[++idx] = b;
}
else{
cout << p[idx - b + 1] << endl;
}
}
return 0;
}
C - Divide and Divide#
难度: ⭐⭐#
题目大意#
给定一个数n, 小莫可以花费n元让其分裂为n / 2(上取整)和n / 2(下取整)两个数; 然后小莫可以对其进行相同的操作; 请问小莫最少花费多少元让当前所有数都小于2;
解题思路#
因为每次都是除2, 并且分裂为相邻或者相同的两个数, 所以数不会太多, 可以用记忆化搜索来解决;
神秘代码#
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl '\n'
using namespace std;
const int N = 5e5 + 10, mod = 998244353, inf = 1000000993;
typedef pair<int, int> PII;
int n, m, res;
map<int, int> mp;
int solve(int u){
if(u < 2) return 0;
if(mp[u]) return mp[u];
mp[u] = u + solve(u / 2) + solve(u / 2 + u % 2);
return mp[u];
}
signed main(){
cin >> n;
cout << solve(n);
return 0;
}
D - Super Takahashi Bros#
难度: ⭐⭐⭐#
题目大意#
现在有n个关卡, 小莫位于第一关, 每关都有两个选择, 花费a元到达下一关, 或者花费b元直接到达第c关, 注意c可能是前面的关卡; 请问小莫最少花费多少元可以到达第n关;
解题思路#
第一眼像dp, 但是其实还算是个明显的最短路问题; 套板子即可;
神秘代码#
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, mod = 998244353, inf = 1e18;
typedef pair<int, int> PII;
int n, m, res;
int d[N];
bool st[N];
vector<PII> v[N];
void dijkstra(){
for(int i = 1; i <= n; i++) d[i] = inf;
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, 1});
d[1] = 0;
while(heap.size()){
auto t = heap.top();
heap.pop();
int id = t.second;
if(st[id]) continue;
st[id] = true;
for(auto x : v[id]){
if(d[x.first] > d[id] + x.second){
d[x.first] = d[id] + x.second;
heap.push({d[x.first], x.first});
}
}
}
cout << d[n];
}
signed main(){
cin >> n;
for(int i = 1; i < n; i++){
int a, b, c;
cin >> a >> b >> c;
v[i].push_back({i + 1, a});
v[i].push_back({c, b});
}
dijkstra();
return 0;
}
E - Mancala 2#
难度: ⭐⭐⭐#
题目大意#
现在有n个盒子, 第i个盒子有Ai个球; 现在需要进行m次操作, 每次操作先指定一个盒子i, 然后把第i个盒子里的球都取出来, 然后从第i + 1个盒子开始分配, 每次分出去一个球, 直到分完为止; 分配顺序是按序号递增, 其中n - 1和0是相邻的; 输出进行m次操作后所以盒子的球数;
解题思路#
区间修改问题可以用线段树, 记录数量和一个懒标记即可; 因为Ai的数据范围较大, 所以要先进行整除和取余, 分别往上加; 再加上n - 1和0相邻的问题, 记得也要分开处理; 总体来说也算是比较板;
神秘代码#
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, mod = 998244353, inf = 1e18;
typedef pair<int, int> PII;
int n, m, res;
int A[N], B[N];
struct node{
int l, r;
int num, add;
}tr[4 * N];
void pushdown(int u){
if(tr[u].l == tr[u].r) return;
auto& root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1];
if(root.add){
left.add += root.add;
right.add += root.add;
root.add = 0;
}
}
void build(int u, int l, int r){
if(l == r) tr[u] = {l, r, A[l], 0};
else{
tr[u] = {l, r, 0, 0};
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
}
}
void modify(int u, int l, int r, int x){
if(tr[u].l >= l && tr[u].r <= r){
tr[u].add += x;
}
else{
int mid = tr[u].l + tr[u].r >> 1;
pushdown(u);
if(l <= mid) modify(u << 1, l, r, x);
if(r > mid) modify(u << 1 | 1, l, r, x);
}
}
int query(int u, int x){
if(tr[u].l == tr[u].r){
return tr[u].num + tr[u].add;
}
else{
int mid = tr[u].l + tr[u].r >> 1;
pushdown(u);
if(x <= mid) return query(u << 1, x);
else return query(u << 1 | 1, x);
}
}
signed main(){
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> A[i];
for(int i = 1; i <= m; i++) cin >> B[i];
build(1, 0, n - 1);
for(int i = 1; i <= m; i++){
int x = query(1, B[i]);
if(!x) continue;
modify(1, B[i], B[i], -x);
int a = x % n;
int b = x / n;
modify(1, 0, n - 1, b);
if(B[i] + a >= n){
if(B[i] + 1 < n) modify(1, B[i] + 1, n - 1, 1);
modify(1, 0, (B[i] + a) % n, 1);
}
else modify(1, B[i] + 1, B[i] + a, 1);
}
for(int i = 0; i < n; i++){
cout << query(1, i) << ' ';
}
return 0;
}
F - S = 1#
难度: ⭐⭐⭐#
题目大意#
给定一个非原点坐标(a, b), 现在需要找出任一坐标(x, y), 使得(0, 0), (a, b), (x, y)三点组成的三角形面积为1; 其中x和y要求在(-1e18, 1e18)之间; 无解输出-1;
解题思路#
根据数学推算得出只要|bx - ay| = 2就可以满足题目要求; x和y可以用扩欧来求, 有解的前提是2是gcd(x, y)的倍数; 剩下的套板子即可; 别忘了y是负数, 最后要乘-1;
神秘代码#
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, mod = 998244353, inf = 1e18;
typedef pair<int, int> PII;
int n, m, res;
void exgcd(int a, int b, int& x, int& y){
if(b == 0){
x = 1, y = 0;
return;
}
exgcd(b, a % b, y, x);
y = y - (a / b) * x;
}
bool check(int x, int y){
if(x >= -inf && x <= inf && y >= -inf && y <= inf) return true;
else return false;
}
signed main(){
int a, b;
cin >> a >> b;
if(abs(gcd(a, b)) > 2){
cout << -1;
return 0;
}
int x, y;
exgcd(b, a, x, y);
x *= 2 / gcd(a, b);
y *= -2 / gcd(a, b);
if(check(x, y))cout << x << ' ' << y;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!