Codeforces Round #604 (Div. 2) 练习A,B题解
2024-09-06 09:25:59
A题
思路分析:
因为只需要做到相邻的不相同,利用三个不同的字母是肯定可以实现的,
所以直接先将所有的问号进行替换,比如比前一个大1,如果与后面的冲突,则再加一代码(写的很烂):
#include <bits/stdc++.h>
using namespace std;
int check( string a)
{
for (int i = 0; i < a.length(); ++i)
{
if (i==0)
{
if (a[i]==a[i+1])
{
return 0;
}
}
else if (a[i]==a[i-1])
{
return 0;
}
else{
if (a[i-1]==a[i]||a[i+1]==a[i])
{
return 0;
}
}
}
return 1;
}
int main(int argc, char const *argv[])
{
int t;
cin>>t;
string a;
while(t--)
{
cin>>a;
for (int i = 0; i < a.length(); ++i)
{
if (i==0)
{
if (a[i]=='?')
{
a[i]=((a[i+1]+1)%97)%3 + 97;
if (a[i]==a[i+1])
{
a[i] = ((a[i]+1)%97)%3+97;
}
}
}
else if (i==a.length()-1 )
{
if (a[i]=='?')
{
a[i]=((a[i-1]+1)%97)%3 + 97;
}
}
else
{
if (a[i]=='?')
{
a[i]=((a[i-1]+1)%97)%3 +97;
if (a[i] == a[i+1])
{
a[i]=((a[i]+1)%97)%3 +97;
}
}
}
}
if (check(a))
{
cout<<a<<endl;
}
else
cout<<"-1"<<endl;
}
return 0;
}
B题
思路分析:
如果有1到m的一个排列,那么肯定1到m的所有的数的位置最大值减去最小值的差是m-1
所以可以利用一个数组将pos信息存起来,从小到大遍历就可以代码:
#include <bits/stdc++.h>
using namespace std;
int pos[200001];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int t;
int n;
cin>>t;
int k;
while(t--)
{
cin>>n;
for (int i = 0; i < n; ++i)
{
cin>>k;
pos[k-1] = i;
}
int maxpos = 0;
int minpos = n-1;
for (int i = 0; i < n; ++i)
{
minpos = min(minpos, pos[i]);
maxpos = max(maxpos, pos[i]);
cout<<(maxpos-minpos==i?1:0);
}
cout<<endl;
}
return 0;
}
最新文章
- Android 笔记 文件存取 day5
- linux mail利用外部邮箱地址发邮件
- [tmp] hu60@所有人插件
- UWP开发笔记——嵌套式页面的实现
- 使用SharedPreferences进行简单的储存
- C/C++ 跨平台交叉编译、静态库/动态库编译、MinGW、Cygwin、CodeBlocks使用原理及链接参数选项
- 遍历nsarray
- phpcms V9 首页模板文件解析(转)
- struts2-core-2.1.6.jar!/struts-default.xml无法加载的问题
- Android通过HTTP协议实现上传文件数据
- 学HTTP协议所要知道的基础知识(微总结)
- js 学习之路9:运算符
- jupyter4.4.0自定义目录
- 文件服务器HFS
- Codeforces 1043 F - Make It One
- 用R的dgCMatrix包来构建稀疏矩阵 | sparse matrix by dgCMatrix
- 在Linux中以普通用户开机自动运行脚本程序
- 关于Unity中的光照(二)
- APICloud-端JS库功能API文档(1)
- python json.loads json.dumps(ensure_ascii = False) 汉字乱码问题解决
热门文章
- LRU hashMap(拉链) + 双向链表 java实现
- go语言设计模式之proxy
- 2个ajax的相互调用解决异步问题
- WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据
- ESA2GJK1DH1K升级篇: STM32远程乒乓升级,基于Wi-Fi模块AT指令TCP透传方式,MQTT通信控制升级(含有数据校验)-APP用户程序制作过程
- cartographer 3D运行录制rosbag包
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem 水题
- python-pandas读取mongodb、读取csv文件
- SQL Server设置数据库为状态为只读
- 05爬虫-requests模块基础(2)