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

相关推荐
Tisfy7 天前
LeetCode 3289.数字小镇中的捣蛋鬼:哈希表O(n)空间 / 位运算O(1)空间
算法·leetcode·散列表·题解·位运算·哈希表
Tisfy8 天前
LeetCode 3346.执行操作后元素的最高频率 I:滑动窗口(正好适合本题数据,II再另某他法)
算法·leetcode·题解·滑动窗口·哈希表
王老师青少年编程12 天前
AtCoder真题及详细题解 ABC427C: Bipartize
c++·题解·1024程序员节·atcoder·csp·abc·信奥赛
Tisfy15 天前
LeetCode 3461.判断操作后字符串中的数字是否相等 I:简单题简单做的时候到了
leetcode·题解·模拟·1024程序员节
Juan_201216 天前
P1041题解
c++·算法·题解·搜索
Juan_201220 天前
P1040题解
c++·算法·动态规划·题解
linruicong21 天前
题解:洛谷-P8548 小挖的买花
题解
Juan_20121 个月前
P1447题解
c++·数学·算法·题解
Juan_20121 个月前
P3051题解
c++·数学·算法·题解
Tisfy1 个月前
LeetCode 0611.有效三角形的个数:双指针
算法·leetcode·题解·双指针