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

相关推荐
Tisfy19 小时前
LeetCode 960.删列造序 III:动态规划(最长递增子序列)
算法·leetcode·动态规划·字符串·题解·逆向思维
Tisfy2 天前
LeetCode 955.删列造序 II:模拟(O(mn)) + 提前退出
算法·leetcode·字符串·题解·遍历
rannn_1113 天前
【SQL题解】力扣高频 SQL 50题|DAY1
后端·sql·题解
滑稽的小Z4 天前
[PA 2017] Iloczyn 题解
c++·题解
cpp_25014 天前
P8723 [蓝桥杯 2020 省 AB3] 乘法表
数据结构·c++·算法·蓝桥杯·题解·洛谷
Tisfy5 天前
LeetCode 3652.按策略买卖股票的最佳时机:滑动窗口
算法·leetcode·题解·滑动窗口
Tisfy8 天前
LeetCode 2110.股票平滑下跌阶段的数目:数学(一次遍历)
数学·算法·leetcode·题解
Tisfy10 天前
LeetCode 3606.优惠券校验器:分类 + 排序
leetcode·题解·排序
Tisfy12 天前
LeetCode 3531.统计被覆盖的建筑:最大最小值
算法·leetcode·题解·桶排序
Tisfy13 天前
LeetCode 3577.统计计算机解锁顺序排列数:脑筋急转弯(组合数学)
算法·leetcode·题解·组合数学·脑筋急转弯