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

相关推荐
满怀10153 天前
【LeetCode】7.整数反转
leetcode·题解
Tisfy7 天前
LeetCode 2563.统计公平数对的数目:排序 + 二分查找
python·算法·leetcode·二分查找·题解·排序
Tisfy15 天前
LeetCode 2843.统计对称整数的数目:字符串数字转换
算法·leetcode·字符串·题解
Tisfy18 天前
LeetCode 3396.使数组元素互不相同所需的最少操作次数:O(n)一次倒序遍历
算法·leetcode·题解·数组·遍历·哈希表
Tisfy24 天前
LeetCode 2360.图中的最长环:一步一打卡(不撞南墙不回头) - 通过故事讲道理
算法·leetcode··题解
Lyrella1 个月前
luogu-P5320题解
数学·题解
炒鸡码力1 个月前
一道原创OI题(普及-)——ZCS的随机游走
c++·算法·题解·模拟·题目
Tisfy1 个月前
LeetCode 2272.最大波动的子字符串:转为多次的最大子数组和 - 一步步思考推导
算法·leetcode·动态规划·字符串·题解
Tisfy1 个月前
LeetCode 3110.字符串的分数:模拟(注意一个小细节)
算法·leetcode·字符串·题解·模拟
Tisfy2 个月前
LeetCode 0132.分割回文串 II:动态规划
leetcode·动态规划·字符串·题解·回文