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

相关推荐
Tisfy2 天前
LeetCode 2839.判断通过操作能否让字符串相等 I:if-else(两两判断)
算法·leetcode·字符串·题解
Tisfy4 天前
LeetCode 2946.循环移位后的矩阵相似检查:模拟(左即是右)
算法·leetcode·矩阵·题解
cpp_25015 天前
P1796 汤姆斯的天堂梦
数据结构·c++·算法·题解·洛谷·线性dp
Tisfy5 天前
LeetCode 3548.等和矩阵分割 II:矩阵旋转 + 哈希表
leetcode·矩阵·散列表·题解·哈希表·矩阵旋转
cpp_25016 天前
P8395 [CCC 2022 S1] Good Fours and Good Fives
数据结构·c++·算法·动态规划·图论·题解·洛谷
cpp_25016 天前
P1968 [CHCI 2001 Regional Competition Seniors] 美元汇率
数据结构·c++·算法·题解·洛谷·线性dp
Tisfy7 天前
LeetCode 2906.构造乘积矩阵:前后缀分解
算法·leetcode·前缀和·矩阵·题解·前后缀分解
王老师青少年编程8 天前
2026年3月GESP真题及题解(C++五级):有限不循环小数
c++·题解·真题·gesp·csp·五级·有限不循环小数
王老师青少年编程9 天前
2026年3月GESP真题及题解(C++七级):物流网络
c++·题解·真题·gesp·csp·七级·物流网络
Tisfy9 天前
LeetCode 1886.判断矩阵经轮转后是否一致:模拟
算法·leetcode·矩阵·题解·模拟