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 2434.使用机器人打印字典序最小的字符串:贪心(栈)——清晰题解
leetcode·机器人·字符串·题解·贪心·
Tisfy13 天前
LeetCode 2894.分类求和并作差:数学O(1)一行解决
数学·算法·leetcode·题解
Tisfy17 天前
LeetCode 3356.零数组变换 II:二分查找 + I的差分数组
算法·leetcode·二分查找·题解·差分数组
Tisfy18 天前
LeetCode 3355.零数组变换 I:差分数组
算法·leetcode·题解·差分数组
liuzhangfeiabc19 天前
[luogu12541] [APIO2025] Hack! - 交互 - 构造 - 数论 - BSGS
c++·算法·题解
liuzhangfeiabc19 天前
[luogu12542] [APIO2025] 排列游戏 - 交互 - 博弈 - 分类讨论 - 构造
c++·算法·题解
Tisfy1 个月前
LeetCode 3341.到达最后一个房间的最少时间 I:Dijkstra算法(类似深搜)-简短清晰的话描述
leetcode··最短路·dijkstra·题解·迪杰斯特拉
Tisfy1 个月前
LeetCode 1550.存在连续三个奇数的数组:遍历
算法·leetcode·题解·数组·遍历
ArmeriaLeap1 个月前
AtCoder AT_abc405_d ABC405D - Escape Route
题解·atcoder
Tisfy1 个月前
LeetCode 1295.统计位数为偶数的数字:模拟
算法·leetcode·题解