当前位置:网站首页>CSP2021 T3 回文
CSP2021 T3 回文
2022-07-24 13:42:00 【lAWTYl】
回文
传送门:CSP2021 T3 回文
题目大意
给定一个长度为 2 n 2n 2n 的数组,在这个数组中,数字 1 , 2 , 3 , ⋯ , n 1, 2, 3, \cdots, n 1,2,3,⋯,n 分别出现两次。
现在有两个操作,一是把 a a a 的开头放到 b b b 数组的结尾,二是把 a a a 的结尾放到 b b b 的结尾。要求使得 b b b 最后是一个回文数组,并且要求字典序最小。
最后输出方案。
分析
假设我们第一步是 L L L(第一步是 R R R 其实也差不多, 这里就以是 L L L 为例)。我们显然可以 O ( n ) O(n) O(n) 在数组中找到一个点 x x x 满足 x ∈ [ 2 , 2 n ] ∧ a 1 = a x x \in [2, 2n] \land a_1 = a_x x∈[2,2n]∧a1=ax。然后我们就可以发现,因为我们第一步把 a 1 a_1 a1 放到了 b b b 数组的开头,所以 b b b 数组的结尾就一定是我们找到的 a x a_x ax。所以我们就通过这两个点把数组分成了 2 2 2 个栈,分别是:
- 栈顶 → \to → 栈底 : a 2 → a x − 1 a_2 \to a_{x - 1} a2→ax−1
- 栈顶 → \to → 栈底 : a 2 n → a x + 1 a_{2n} \to a_{x + 1} a2n→ax+1
我们就以样例中的第一组为例子:
4 , 1 , 2 , 4 , 5 , 3 , 1 , 2 , 3 , 5 \color{red}{4}\color{black}{, 1, 2,} \color{red}{4} \color{black}{, 5, 3, 1, 2, 3, 5} 4,1,2,4,5,3,1,2,3,5
我们弄出来的两个栈就是:

然后我们就可以继续下面的工作了 似乎变得很简单了qwq。
我们把这两个栈看成双端队列,然后找同时再栈底和栈顶都存在的数字(可能有两对或者一对,一对就直接搞,两队就选字典序小的搞)。同时弹出它们两个然后记录一下是怎么弹的输出就好了。
如果没看懂上面的文字描述的可以看下面的完整的弹栈图解(还是以刚才的数列为例):

显然这样做的复杂度是 O ( n ) O(n) O(n) 的。
代码
#include<bits/stdc++.h>
using namespace std;
#define in read()
#define MAXN 500500 << 1
inline int read(){
int x = 0; char c = getchar();
while(c < '0' or c > '9') c = getchar();
while('0' <= c and c <= '9'){
x = x * 10 + c - '0'; c = getchar();
}
return x;
}
int T = 0;
int n = 0;
int a[MAXN] = {
0 };
char ans[MAXN];
bool work(int l1, int r1, int l2, int r2){
for(int i = 1; i < n; i++){
if(l1 <= r1 and ((l2 <= r2 and a[l1] == a[l2]) or (l1 < r1 and a[l1] == a[r1])))
if(l1 < r1 and a[l1] == a[r1]){
l1++, r1--;
ans[i] = 'L', ans[2 * (n - 1) - i + 1] = 'L';
}
else{
l1++, l2++;
ans[i] = 'L', ans[2 * (n - 1) - i + 1] = 'R';
}
else if(l2 <= r2 and ((l1 <= r1 and a[r1] == a[r2]) or (l2 < r2 and a[l2] == a[r2])))
if(l2 < r2 and a[l2] == a[r2]){
l2++, r2--;
ans[i] = 'R', ans[2 * (n - 1) - i + 1] = 'R';
}
else{
r1--, r2--;
ans[i] = 'R', ans[2 * (n - 1) - i + 1] = 'L';
}
else return 0;
}
return 1;
}
int main(){
T = in;
while(T--){
n = in; int x1 = -1, x2 = -1;
for(int i = 1; i <= 2 * n; i++) a[i] = in;
for(int i = 1; i <= 2 * n; i++) ans[i] = 0;
for(int i = 2; i <= 2 * n; i++) if(a[i] == a[1]) {
x1 = i; break; }
for(int i = 1; i < 2 * n; i++) if(a[i] == a[2 * n]) {
x2 = i; break; }
if(work(2, x1 - 1, x1 + 1, 2 * n)) printf("L%sL\n", ans + 1);
else if(work(1, x2 - 1, x2 + 1, 2 * n - 1)) printf("R%sL\n", ans + 1);
else puts("-1");
}
return 0;
}
边栏推荐
- Solution to embedded SD card /u disk read-only problem (fat read-only repair method)
- 2022年全国职业院校技能大赛赛项比赛时间、承办校信息统计表(第二批)
- Happy number ~ ~ ~ (in fact, I'm not happy at all) & ugly number
- R语言使用epiDisplay包的tableStack函数制作统计汇总表格(基于目标变量分组的描述性统计、假设检验等)、设置by参数为目标变量、设置percent参数配置是否显示百分比信息
- Browser failed to get cookies, browser solution
- Odoo+ test
- How to verify the domain name after applying for SSL digital certificate?
- Chat room project
- R语言使用epiDisplay包的summ函数计算dataframe中指定变量在不同分组变量下的描述性统计汇总信息并可视化有序点图、自定义cex.main参数配置标题文本字体的大小
- 深入浅出边缘云 | 2. 架构
猜你喜欢

Common OJ questions of stack and queue

网络安全——文件上传黑名单绕过

基于典型相关分析的多视图学习方法综述

ICML2022 | 分支强化学习

Network security - use exchange SSRF vulnerabilities in combination with NTLM trunking for penetration testing

Odoo+ test

论文笔记:Swin-Unet: Unet-like Pure Transformer for MedicalImage Segmentation

WSDM 22 | 基于双曲几何的图推荐

Group knowledge map: distributed knowledge transfer and federated map reasoning

Win10 log in with Microsoft account and open all programs by default with administrator privileges: 2020-12-14
随机推荐
Simple order management system small exercise
[enlightenment -51]: since people are doomed to die, why should they live?
Basic operation of file
从云原生到智能化,深度解读行业首个「视频直播技术最佳实践图谱」
Common OJ questions of stack and queue
使用Activiti创建数据库表报错,
第六章 总线
Unity UGUI中scroll bar在游戏中启动界面时没有从最上面显示
Network security -- man in the middle attack penetration test
基于图正则化的贝叶斯宽度学习系统
申请了SSL数字证书如何进行域名验证?
Chat room project
rhce第一次作业
CSDN垃圾的没有底线!
From cloud native to intelligent, in-depth interpretation of the industry's first "best practice map of live video technology"
Get (min / max value) of (object array / array)
基于ABP实现DDD--实体创建和更新
Common doc commands
[机缘参悟-51]:既然人注定要死亡,为什么还要活着?
网络安全——Cookie注入