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

相关推荐
Tisfy5 天前
LeetCode 3341.到达最后一个房间的最少时间 I:Dijkstra算法(类似深搜)-简短清晰的话描述
leetcode··最短路·dijkstra·题解·迪杰斯特拉
Tisfy6 天前
LeetCode 1550.存在连续三个奇数的数组:遍历
算法·leetcode·题解·数组·遍历
ArmeriaLeap7 天前
AtCoder AT_abc405_d ABC405D - Escape Route
题解·atcoder
Tisfy17 天前
LeetCode 1295.统计位数为偶数的数字:模拟
算法·leetcode·题解
满怀101524 天前
【LeetCode】7.整数反转
leetcode·题解
Tisfy1 个月前
LeetCode 2563.统计公平数对的数目:排序 + 二分查找
python·算法·leetcode·二分查找·题解·排序
Tisfy1 个月前
LeetCode 2843.统计对称整数的数目:字符串数字转换
算法·leetcode·字符串·题解
Tisfy1 个月前
LeetCode 3396.使数组元素互不相同所需的最少操作次数:O(n)一次倒序遍历
算法·leetcode·题解·数组·遍历·哈希表
Tisfy2 个月前
LeetCode 2360.图中的最长环:一步一打卡(不撞南墙不回头) - 通过故事讲道理
算法·leetcode··题解
Lyrella2 个月前
luogu-P5320题解
数学·题解