当前位置: 首页 > article >正文

AtCoder ABC373 A-D题解

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

比赛链接:ABC373

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

Problem A:

Code

#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

#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

#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

首先,由x_{v_j}-x_{u_j}=w_j可得x_{u_j}-x_{v_j}=-w_j

graph[u].push_back(make_pair(v,w));
graph[v].push_back(make_pair(u,-w));

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

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

#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


http://www.kler.cn/news/336781.html

相关文章:

  • YOLO11改进|上采样篇|引入CARAFE上采样模块
  • Leecode热题100-560.和为k的子数组
  • Golang | Leetcode Golang题解之第449题序列化和反序列化二叉搜索树
  • 如何查看NVIDIA Container Toolkit是否配置成功
  • 《数据结构》学习系列——树
  • ssh连接阿里云长连接
  • Django学习笔记十四:系统框架总结
  • 日常记账:解锁生活财务管理的秘密钥匙
  • 大模型应用新领域:探寻终端侧 AI 竞争核心|智于终端
  • Win10 IDEA连接虚拟机中的Hadoop(HDFS)
  • idea配置注释
  • 系统架构设计师-下午案例题(2021年下半年)
  • qt_c++_xml存这种复杂类型
  • ROS基础入门——实操教程
  • Python字符串操作详解
  • 深入探索Vue3组合式API
  • PCL 1.8.1 + VTK 1.8.0 + QT5.14.2+ VS2017 环境搭建
  • 【Ubuntu】Ubuntu常用命令
  • DAY26||669.修建二叉树 |108.将有序数组转换为二叉搜索树|538.把二叉搜索树转换为累加树
  • Spring Validation 参数校验框架