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

相关推荐
cpp_25013 小时前
P2347 [NOIP 1996 提高组] 砝码称重
数据结构·c++·算法·题解·洛谷·noip·背包dp
Tisfy6 小时前
LeetCode 3783.整数的镜像距离:数学
数学·算法·leetcode·题解
cao23331 天前
poj1845 sumdiv 题解
题解
Tisfy1 天前
LeetCode 3761.镜像对之间最小绝对距离:哈希表(维护左,枚举右)
算法·leetcode·散列表·题解
Tisfy3 天前
LeetCode 2515.到目标字符串的最短距离:从中间往两边遍历
算法·leetcode·字符串·题解·数组·遍历
Tisfy5 天前
LeetCode 1848.到目标元素的最小距离:数组遍历(附python一行版)
python·leetcode·题解·遍历
Tisfy7 天前
LeetCode 3740.三个相等元素之间的最小距离 I:今日先暴力,“明日“再哈希
算法·leetcode·哈希算法·题解·模拟·遍历·暴力
cpp_25017 天前
P1910 L 国的战斗之间谍
数据结构·c++·算法·题解·洛谷·背包dp
Billlly8 天前
ABC 453 个人题解
算法·题解·atcoder
cpp_250113 天前
P10108 [GESP202312 六级] 闯关游戏
数据结构·c++·算法·动态规划·题解·洛谷·gesp六级