AtCoder ABC373 A-D题解

ABC372 的题解没写是因为 D 是单调栈我不会(⊙︿⊙)

比赛链接:ABC373

总结:wssb。听说 E 很水?有时间我看看。

Problem A:

Code

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int mian(){
	int ans=0;
	for(int i=1;i<=12;i++){
		string S;
		cin>>S;
		if(S.size()==i)
			ans++;
	}
	cout<<ans<<endl;
	return 0;
}

Problem B:

甚至是先过的 C。wssb 题目没审清导致 WA 了 n 多次。。。

Sol

用一个 map 表示键盘即可。

Code

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
string alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int main(){
	string S;
	cin>>S;
	map<char,int> keyboard;
	for(int i=0;i<S.size();i++)
		keyboard[S[i]]=i+1;
	int ans=0;
	for(int i=0;i<25;i++)
		ans+=(keyboard[alphabet[i]]-keyboard[alphabet[i+1]]);//abs
	cout<<ans<<endl;
	return 0;
}

Problem C:

好水的 C。放在 B 都不为过。

Code

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
	int N;
	cin>>N;
	int a=0,b=0;//-2e9
	for(int i=1;i<=N;i++){
		int A;
		cin>>A;
		a=max(a,A);
	}
	for(int i=1;i<=N;i++){
		int B;
		cin>>B;
		b=max(b,B);
	}
	cout<<a+b<<endl;
	return 0;
}

Problem D:

水 D 但 wssb 把 dfs 的类型 void 写成了 int 导致一直 RE 最后到比赛结束才发现。。。乐

Sol

首先,由可得

cpp 复制代码
graph[u].push_back(make_pair(v,w));
graph[v].push_back(make_pair(u,-w));

然后我们用一个 vis 数组标记一个结点是否访问过。如果访问过就跳过,否则 dfs 计算该节点的值即可。

cpp 复制代码
void dfs(int u){
	vis[u]=true;
	for(auto p:graph[u]){
		if(!vis[p.first]){
			val[p.first]=val[u]+p.second;
			dfs(p.first);
		}
	}
}

Code

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=200005;
vector<pair<int,int>> graph[maxn];
long long val[maxn];
bool vis[maxn];
int dfs(int u){
	vis[u]=true;
	for(auto p:graph[u]){
		if(!vis[p.first]){
			val[p.first]=val[u]+p.second;
			dfs(p.first);
		}
	}
}
int main(){
	int N,M;
	cin>>N>>M;
	for(int i=1;i<=M;i++){
		int u,v,w;
		cin>>u>>v>>w;
		graph[u].push_back(make_pair(v,w));
		graph[v].push_back(make_pair(u,-w));
	}
	for(int i=1;i<=N;i++){
		if(!vis[i])
			dfs(i);
	}
	for(int i=1;i<=N;i++)
		cout<<val[i]<<' ';
	return 0;
}

友情提醒:不要无脑Ctrl C+Ctrl V

相关推荐
Tisfy2 天前
LeetCode 2411.按位或最大的最小子数组长度:一次倒序遍历
数据结构·算法·leetcode·题解·位运算·遍历
Tisfy11 天前
LeetCode 1695.删除子数组的最大得分:滑动窗口(哈希表)
算法·leetcode·散列表·题解·双指针·滑动窗口·哈希表
Tisfy14 天前
LeetCode 3202.找出有效子序列的最大长度 II:取模性质(动态规划)
算法·leetcode·动态规划·题解·模运算
WebGoC开发者16 天前
C++题解(37) 信息学奥赛一本通1318:【例5.3】自然数的拆分
c++·算法·青少年编程·题解
XuYueming16 天前
圆方树学习笔记 —— 一种关于点双连通分量的思考方式
题解·lca·缩点·tarjan·圆方树·点双连通分量·树链剖分·记录 & 心得·仙人掌·理论 / 算法·bzoj·hydro·图的连通性·动态 dp / ddp
syzyc22 天前
[ABC267F] Exactly K Steps
数据结构·动态规划·题解
Tisfy2 个月前
LeetCode 2434.使用机器人打印字典序最小的字符串:贪心(栈)——清晰题解
leetcode·机器人·字符串·题解·贪心·
Tisfy2 个月前
LeetCode 2894.分类求和并作差:数学O(1)一行解决
数学·算法·leetcode·题解
Tisfy2 个月前
LeetCode 3356.零数组变换 II:二分查找 + I的差分数组
算法·leetcode·二分查找·题解·差分数组
Tisfy2 个月前
LeetCode 3355.零数组变换 I:差分数组
算法·leetcode·题解·差分数组