AtCoder ABC237题解
ABC237
对应地址,https://atcoder.jp/contests/abc237/tasks。
A - Not Overflow
链接地址
https://atcoder.jp/contests/abc237/tasks/abc237_a。
题目要求
给定一个整数XXX,判断 XXX 是否在 int\text{int}int 范围内。
简易题解
签到题。
我们可以使用 C++ 的 INT_MAX 和 INT_MIN。如果不知道这两个常数,可以自己定义。
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);LL n;cin>>n;if (n>=INT_MIN && n<=INT_MAX){cout<<"Yes\n";}else{cout<<"No\n";}return 0;
}
时间复杂度
O(1)O(1)O(1)。
空间复杂度
O(1)O(1)O(1)。
B - Matrix Transposition
链接地址
https://atcoder.jp/contests/abc237/tasks/abc237_b。
题目要求
输出 H×WH \times WH×W 矩阵的转秩矩阵。
简单题解
签到题。
本题的难点在于数据太大。根据题目要求
1≤H,W≤1051 \leq H,W \leq 10^51≤H,W≤105
W×H≤105W \times H \leq 10^5W×H≤105
我们没法直接定义出一个二维数组,因为这样定义,我们必然会得到 MLE\text{MLE}MLE,本题只能用二维 vector\text{vector}vector 来实现。
解决了数据存储问题,代码自然非常简单。
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);LL h,w;cin>>h>>w;vector<vector<LL>> a(h,vector<LL>(w));for (auto &x:a){for (auto &y:x){cin>>y;}}for (LL i=0; i<w; i++){for (LL j=0; j<h; j++){cout<<a[j][i]<<" ";}cout<<"\n";}return 0;
}
时间复杂度
O(H∗W)O(H*W)O(H∗W)。
空间复杂度
O(H∗W)O(H*W)O(H∗W)。
C - kasaka
链接地址
https://atcoder.jp/contests/abc237/tasks/abc237_c。
题目要求
给一个长度为 nnn 的字符串 sss,判断能否在开头增加若干(包含 000)个字符 aaa,使得字符串 sss 变成回文字符串。
简单题解
我们可以使用双指针来解决这个问题。
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);string s;cin>>s;LL n=s.size();LL l=0, r=n-1;LL tl=0, tr=0;//定义第一个awhile (l<n && s[l]=='a') {l++;tl++;}//定位最后一个awhile (r>=0 && s[r]=='a') {r--;tr++;}if (tr<tl) {cout<<"No\n";return 0;}while (l<r) {if (s[l]==s[r]) {l++;r--;} else {break;}}if (l<r) {cout<<"No\n";} else {cout<<"Yes\n";}return 0;
}
时间复杂度
O(n)O(n)O(n)。
空间复杂度
O(n)O(n)O(n)。
D - LR insertion
链接地址
https://atcoder.jp/contests/abc237/tasks/abc237_d。
题目要求
给一个字符串 SSS,按照要求将数据 iii 插入到数列 AAA 中。
简单题解
数据结构题。可以使用双向队列,也可以使用双指针来解决。
通过观察输入样例 222,我们可以得到如下结论:
- 碰到 ‘L’,我们从右边(队尾)插入数据。
- 碰到 ‘R’,我们从左边(队头)插入数据。
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);LL n;cin>>n;string s;cin>>s;
#if 1vector<LL> a(n+1,0);LL l=0;LL r=n;for (LL i=0; i<n; i++){if (s[i]=='L'){a[r]=i;r--;}else{a[l]=i;l++;}}a[l]=n;
#elsedeque<LL> a;a.push_back(n);for (LL i=n-1; i>=0; i--){if (s[i]=='L'){a.push_back(i);}else{a.push_front(i);}}
#endiffor (const auto &x:a){cout<<x<<" ";}cout<<"\n";return 0;
}
时间复杂度
O(n)O(n)O(n)。
空间复杂度
O(n)O(n)O(n)。
E - Skiing
链接地址
https://atcoder.jp/contests/abc237/tasks/abc237_e。
题目要求
给一个无向图,高桥从顶点 111 出发,他能得到的最大快乐。
简单题解
图论的基础题,思路参考单源最短路问题。基本步骤如下:
- 建边。
- 从起点 111 出发,遍历所有边。并更新距离。
- 查找所有距离中的最大值。
特别说明
AtCoder 在竞赛后,加强了数据,导致原来 AC 代码有一个样例数据出现超时。
原来代码使用朴素版本的 dij 算法。因此我们需要使用堆对齐优化。
但是标准的 dij 算法不支持负权,而本题出现 h[u]<h[v]h[u]<h[v]h[u]<h[v] 的时候,将有负权。我们需要对数据进行预处理,其目的是保证边权为正。完成所有 dis 计算后,我们再还原真实的数据。
当然,理论上应该也可以用 Belleman-ford 算法。
竞赛中 AC代码
数组版本
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF=0x3f3f3f3f3f3f3f3f;
const int N=2e5+10;
const int M=2*N;
LL h[N];//头节点
LL dis[N];//距离
LL vis[N];//可见性控制
LL happy[N];//幸福度
//下面数据结构来描述图
LL e[M];
LL ne[M];
LL idx;void add(LL u, LL v) {//建边 u->ve[idx]=v;ne[idx]=h[u];h[u]=idx;idx++;
}int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);//初始化memset(dis, -0x3f, sizeof dis);memset(h, -1, sizeof h);LL n,m;//n个顶点,m条边cin>>n>>m;for (LL i=1; i<=n; i++) {cin>>happy[i];}for (LL i=1; i<=m; i++) {LL u,v;cin>>u>>v;add(u,v);add(v,u);}queue<LL> que;dis[1]=0;//起点距离为0que.push(1);while (que.size()) {LL x=que.front();que.pop();vis[x]=false;//以x为起点,遍历x的所有边for (LL i=h[x]; i!=-1; i=ne[i]) {LL y=e[i];//x->yLL c=happy[x]-happy[y];//从x->y的幸福度if (c<0) {c*=2;}if (dis[y]<dis[x]+c) {dis[y]=dis[x]+c;if (false==vis[y]) {que.push(y);vis[y]=true;}}}}//遍历查找最大值LL ans=0;for (LL i=1; i<=n; i++) {ans=max(ans, dis[i]);}cout<<ans<<"\n";return 0;
}
vector版本
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);LL n,m;cin>>n>>m;vector<vector<LL>> adj(n+1);vector<LL> dis(n+1, -9e18);//距离vector<bool> vis(n+1, false);//可见性vector<LL> happy(n+1);//幸福度//读取幸福度for (LL i=1; i<=n; i++) {cin>>happy[i];}//读取图for (LL i=0; i<m; i++) {LL u,v;cin>>u>>v;adj[u].push_back(v);adj[v].push_back(u);}queue<LL> que;dis[1]=0;//起点距离为0que.push(1);while (que.size()) {LL x=que.front();que.pop();vis[x]=false;//以x为起点,遍历x的所有边for (const auto &y:adj[x]) {LL c=happy[x]-happy[y];//从x->y的幸福度if (c<0) {c*=2;}if (dis[y]<dis[x]+c) {dis[y]=dis[x]+c;if (false==vis[y]) {que.push(y);vis[y]=true;}}}}//遍历查找最大值LL ans=0;for (LL i=1; i<=n; i++) {ans=max(ans, dis[i]);}cout<<ans<<"\n";return 0;
}
P.S.
数组版本时间为 85ms。vector版本时间为 104ms。
加强数据据后 AC 代码
使用标准的 dij 算法,利用堆优化。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
const LL INF=0x3f3f3f3f3f3f3f3f;vector<vector<PLL>> adj;
vector<LL> dis;//距离
vector<bool> vis;//可见性
vector<LL> happy;//幸福度void dij(LL st) {priority_queue<PLL, vector<PLL>, greater<PLL>> que;dis[st]=0;//起点距离为0que.push({0, st});while (que.size()) {auto x=que.top();que.pop();LL u=x.second;if (vis[u]) {continue;}vis[u]=true;//以u为起点,遍历u的所有边for (const auto &node:adj[u]) {LL v=node.first;LL w=node.second;if (dis[v]>dis[u]+w) {dis[v]=dis[u]+w;que.push({dis[v], v});}}}
}int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);LL n,m;cin>>n>>m;//初始化数据adj.resize(n+1);dis.resize(n+1, INF);vis.resize(n+1, false);happy.resize(n+1);//读取幸福度for (LL i=1; i<=n; i++) {cin>>happy[i];}//读取图for (LL i=0; i<m; i++) {LL u,v;cin>>u>>v;if (happy[u]>happy[v]) {swap(u,v);}adj[u].push_back({v, happy[v]-happy[u]});adj[v].push_back({u, 0});}dij(1);//遍历查找最大值LL ans=0;for (LL i=1; i<=n; i++) {ans=max(ans, happy[1]-happy[i]-dis[i]);}cout<<ans<<"\n";return 0;
}
时间复杂度
O(mlogn)O(mlogn)O(mlogn)。
空间复杂度
O(m)O(m)O(m)。
F - |LIS| = 3
链接地址
https://atcoder.jp/contests/abc237/tasks/abc237_f。
题目要求
给定整数 n,mn,mn,m,求满足以下所有条件的数列总数。
- 长度为 nnn。
- 数字是 1∼m1 \sim m1∼m 构成。
- ∣LIS∣=3|LIS|=3∣LIS∣=3。
简单题解
读完题目,就知道是一个动态规划问题。
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;const LL MOD=998244353;int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);LL n,m;cin>>n>>m;//找出所有位置vector<vector<LL>> pos;for (LL i=0; i<=m; i++) {for (LL j=0; j<=m; j++) {for (LL k=0; k<=m; k++) {if (i>j || (i==j && i!=m)) {continue;}if (j>k || (j==k && j!=m)) {continue;}pos.push_back({i,j,k});}}}LL dpn = pos.size();vector<PLL> trans;for (LL i=0; i<dpn; i++) {auto from = pos[i];for (LL j=0; j<m; j++) {auto tmp = from;LL updp = lower_bound(tmp.begin(), tmp.end(), j)-tmp.begin();if (updp==3) {continue;}tmp[updp] = j;LL to = lower_bound(pos.begin(), pos.end(), tmp)-pos.begin();trans.push_back({i, to});}}vector<LL> dp(dpn, 0);dp.back()=1;for (LL i=0; i<n; i++) {vector<LL> tmp(dpn, 0);for (const auto &x:trans) {tmp[x.second]=(tmp[x.second]+dp[x.first])%MOD;}swap(dp, tmp);}//遍历答案LL ans=0;for (LL i=0; i<dpn; i++) {auto st = pos[i];if (st.back()!=m) {ans=(ans+dp[i])%MOD;}}cout<<ans<<"\n";return 0;
}
时间复杂度
O(m3)O(m^3)O(m3)。
空间复杂度
O(n)O(n)O(n)。
G - Range Sort Query
链接地址
https://atcoder.jp/contests/abc237/tasks/abc237_g。
题目要求
给一个 NNN 个数构成的排列,一个整数 XXX。
另外 QQQ 次查询,一下搞一个升序,一下搞一个降序。
最终的操作结果是什么。
简单题解
使用线段树吧。这么复杂的操作。
麻烦的是,我特么忘记了线段树的实现。
哎,杀人诛心啊。太难过了。让我查查过去的代码。
AtCoder ABC237题解相关推荐
- Atcoder 284题解
A签到 #include <iostream> #include <cstring> #include <algorithm>using namespace std ...
- AtCoder ABC238 题解
个人评论 昨天的 ABC 直接吧我打懵了,数学题多.
- Atcoder题解与视频集
开启Atcoder之路 开启Atcoder之路_sortmin的博客-CSDN博客_atcoder怎么用 atcoder心得 atcoder心得_404REN的博客-CSDN博客_atcoder怎么用 ...
- AtCoder Beginner Contest 196 A~E题解
ABC196 A~E [A - Difference Max](https://atcoder.jp/contests/abc196/tasks/abc196_a) 题目大意 输入格式 输出格式 样例 ...
- AT2370 Piling Up
https://www.luogu.org/jump/atcoder/2370 题解 答案不是\(2^{2m}\)因为每轮的第一次取球可能会不够. 我们可以设\(dp[i][j]\)表示到了第\(i\ ...
- 【每日亿题#12】AtCoder Grand Contest 021 (A ~ F)全部题解
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 AtCoder Grand Contest 021 题解 A. Digit Sum 2 B. ...
- AtCoder Grand Contest 021完整题解
提示:如果公式挂了请多刷新几次,MathJex的公式渲染速度并不是那么理想. 总的来说,还是自己太弱了啊.只做了T1,还WA了两发.今天还有一场CodeForces,晚上0点qwq... 题解还是要好 ...
- AtCoder题解 —— AtCoder Beginner Contest 182 —— D - Wandering
题目相关 题目链接 AtCoder Beginner Contest 182 D 题,https://atcoder.jp/contests/abc182/tasks/abc182_d. Proble ...
- AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S
题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...
- AtCoder题解——Beginner Contest 170——F - Pond Skater
题目相关 题目链接 AtCoder Beginner Contest 170 F题,https://atcoder.jp/contests/abc170/tasks/abc170_f. Problem ...
最新文章
- Rabbitmq基本框架和安装(1)
- 通用机器学习流程与问题解决架构模板
- c语言小数点进制转换,新手求教,关于含小数的二进制转换成十进制
- Oracle:select 或 inactive 会话语句产生锁?
- 打不死的redis集群
- 安卓mysql插入数据_【11-25求助】关于Android 的SQLite数据库插入数据报错问题
- python与javascript的区别_python与js区别有哪些
- thumbnails 变黑_phpcms v9图片生成缩略图变成黑色解决方法
- 20190811:只出现一次的数字(四种解法)
- C++: 对字符串转换字符集(编码)
- 融云即时通讯SDK集成 – 国内厂商推送集成踩坑篇(Android平台)
- 上位机使用C++通过ADS协议与倍福PLC通信例程-通过变量名方式读写浮点数
- APP测试点总结(表格形式)
- Gimp去除图片背景色方法
- 尚硅谷韩顺平Linux教程学习笔记
- 2017年看的tracking论文
- python爬虫和医学数据_【爬虫】(八)Python之爬虫和数据小解析
- Kanzi Shader入门
- RTSP流访问海康摄像头
- 反应-扩散方程(Reaction-diffusion system)
热门文章
- 计算机二级考试加油作文,为中考而加油作文(精选10篇)
- pod2g宣布A5的Sandbox破解成功
- Elasticsearch Java虚拟机配置详解
- php文件显示文字乱码怎么解决,php遍历到的文件是中文文件名 显示为乱码 该如何解决...
- 默认暴露,分别暴露,整体暴露的再次学习及常用知识
- 冰箱味道很臭?那你真的该学学这些除臭妙招
- 小工具--浏览器主页被挟持,svchost.exe占用网速,treeSizeFree,桌面日历,WIN自带哈希校验
- 《大明王朝的七张面孔》——海瑞
- reflections歌词翻译_花木兰主题曲Reflection翻译成中文的准确歌词
- 前端vue实现图片压缩并且将其转换为jpg格式图片;前端转换图片格式;前端使用js转换图片格式;前端使用canvas将png格式图片转成jpg格式