Atcoder ABC340 A-D题解

比赛链接:ABC340

话不多说,看题。

Problem A:

签到。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int main(){
	int a,b,d;
	cin>>a>>b>>d;
	for(int i=a;i<=b;i+=d)
		cout<<i<<endl;
	return 0;
}

Problem B:

还是签到题。一个vector就搞定了。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int main(){
    int q;
    cin>>q;
    vector<int> a;
    while(q--){
        int tp,x;
        cin>>tp>>x;
        if(tp==1)
            a.push_back(x);
        else
            cout<<a[a.size()-x]<<endl;
    }
    return 0;
}

Problem C:

记忆化搜索即可,直接拿个map维护就ok了,

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
map<int,int> mem;
int dfs(int x){
	if(x<2)
		return 0;
	if(mem.count(x))
		return mem[x];
	return mem[x]=dfs(x/2)+dfs(x/2+bool(x%2))+x; 
}
int main(){
	int n;
	cin>>n;
	cout<<dfs(n)<<endl;
	return 0;
}

Problem D:

一道图论。题目没有保证不出现环,所以建图。所以题目变成1到n的最短路径。跑一边Dijkstra即可。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=200005;
long long dis[maxn];
bool vis[maxn];
vector<long long> graph[maxn],cost[maxn];
struct node{
	long long dist;
	long long p;
	friend bool operator < (node a,node b){
		return a.dist>b.dist;
	}
};
priority_queue<node> pq;
void dijkstra(int s){
	dis[s]=0;
	pq.push((node){0,s});
	while(pq.size()){
		node tmp=pq.top();
		pq.pop();
		int u=tmp.p;
		if(vis[u])
			continue;
		vis[u]=true;
		for(int i=0;i<graph[u].size();i++){
			long long v=graph[u][i];
			long long w=cost[u][i];
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				if(!vis[v])
					pq.push((node){dis[v],v});
			}
		}
	}
}
int main(){
	int n;
	cin>>n;
    //记得初始化dis数组
	for(int i=1;i<n;i++){
		int a,b,x;
		cin>>a>>b>>x;
		graph[i].push_back(i+1);
		cost[i].push_back(a);
		graph[i].push_back(x);
		cost[i].push_back(b);
	}
	dijkstra(1);
	cout<<dis[n]<<endl;
	return 0;
}

OK,以上就是本期的全部内容了。下期更新ABC341的题解。

友情提示:本期的全部代码都有问题,请不要无脑Ctrl C+Ctrl V

相关推荐
王老师青少年编程几秒前
2025年12月GESP(C++)考级真题及详细题解(汇总版)
c++·题解·真题·gesp·csp·信奥赛·考级
Tisfy1 小时前
LeetCode 1339.分裂二叉树的最大乘积:深度优先搜索(一次DFS+存数组并遍历)
算法·leetcode·深度优先·题解
cpp_25012 天前
P1583 魔法照片
数据结构·c++·算法·题解·洛谷
Tisfy2 天前
LeetCode 1975.最大方阵和:脑筋急转弯
算法·leetcode·矩阵·题解·脑筋急转弯
cpp_25012 天前
P1957 口算练习题
数据结构·c++·算法·题解·洛谷
Tisfy5 天前
LeetCode 961.在长度 2N 的数组中找出重复 N 次的元素:5种语言x5种方法(及其变种) —— All By Hand
数据结构·数学·算法·leetcode·题解
cpp_25015 天前
P8597 [蓝桥杯 2013 省 B] 翻硬币
数据结构·c++·算法·蓝桥杯·题解
Tisfy10 天前
LeetCode 1351.统计有序矩阵中的负数:O(m+n)时间复杂度——抽象题解
算法·leetcode·矩阵·题解·遍历
Tisfy11 天前
LeetCode 2402.会议室 III:优先队列大模拟
算法·leetcode·题解·优先队列·排序·大模拟
Tisfy12 天前
LeetCode 2483.商店的最少代价:两次遍历 -> 一次遍历
算法·leetcode·题解·遍历