CSP2021-S T3 回文

一道裸的贪心题目。
暂且先考虑取左边的情况。
老规矩,题目CSP2021-S回文
很容易证明,当取任意一头的一个数的时候,若取的时候数组中的另一个相同的数不能满足条件,那么它们一开始的时候就一定不满足回文的位置关系。那么我们只需要按照题目的字典序最小(指的是输出答案最小,而非取出数组的字典序最小)从左端和右端分别取第一个模拟即可。

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define MA 1000005
//#define Ma 500005
int c[MA];
struct node {
int s[MA];
int nl,nr;
void clear() {
nl=1;
nr=0;
}
int size() {
return nr-nl+1;
}
bool empty() {
if(nl>nr)
return 1;
return 0;
}
void push(int x) {
s[++nr]=x;
}
void pop_front() {
++nl;
}
void pop_end() {
--nr;
}
int front() {
return s[nl];
}
int end() {
return s[nr];
}
void print() {
int i;
for(i=nl;i<=nr;++i)
printf("%d ",s[i]);
printf("\n");
}
}a,b;
int n,m;
char ans[MA],dp[MA];
int ask_loc(int x,int loc) {//询问第几个
int i;
for(i=1;i<=m;++i)
if(c[i]==x&&i!=loc)
return i;
return 0;
}
int now;
bool pan() {//判断字典序
int i=1;
while(i<=m&&ans[i]==dp[i])
++i;
// printf("%d %c%c\n",i,ans[i],dp[i]);
return (i<=m && ans[i]>dp[i]);
}
void inti() {
for(int i=1;i<=m;++i)
ans[i]=dp[i];
return;
}
void work() {
now=2;
while(1) {
// puts("");
// a.print();
// b.print();
// puts("!");
if( a.empty() && b.empty() ) {
return;
}
else if( b.empty() ) {
if( a.front()==a.end() ) {
dp[now]=dp[m+1-now]='L';
a.pop_front();
a.pop_end();
}
else {
dp[1]='Z';
return;
}
}
else if( a.empty() ) {
if(b.front()==b.end()) {
dp[now]=dp[m+1-now]='R';
b.pop_front();
b.pop_end();
}
else {
dp[1]='Z';
return;
}
}
else {
if( a.front()==a.end()&&a.size()>1) {
dp[now]=dp[m+1-now]='L';
a.pop_front();
a.pop_end();
}
else if(a.front()==b.end()) {
dp[now]='L';
dp[m+1-now]='R';
a.pop_front();
b.pop_end();
}
else if(b.front()==a.end()) {
dp[now]='R';
dp[m+1-now]='L';
b.pop_front();
a.pop_end();
}
else if(b.front()==b.end()&&b.size()>1) {
dp[now]=dp[m+1-now]='R';
b.pop_front();
b.pop_end();
}
else {
dp[1]='Z';
break;
}
}
++now;
}
return;
}
int main() {
//freopen("help.out","w",stdout);
int t;
scanf("%d",&t);
int i,loc;
bool p;
while(t--) {
ans[1]='Y';
scanf("%d",&n);
m=n<<1;
for(i=1;i<=m;++i)
scanf("%d",&c[i]);
dp[1]=dp[m]='L';
loc=ask_loc(c[1],1);
a.clear(),b.clear();
for(i=2;i<loc;++i)
a.push(c[i]);
for(i=m;i>loc;--i)
b.push(c[i]);
work();
p=pan();
if(p==1)
inti();
//printf("now==%d\n",now);
dp[1]='R';
dp[m]='L';
loc=ask_loc(c[m],m);
a.clear(),b.clear();
for(i=1;i<loc;++i)
a.push(c[i]);
for(i=m-1;i>loc;--i)
b.push(c[i]);
//puts("sbccf");////////////////////////////////////////
work();
p=pan();
if(p==1)
inti();
//printf("%s\n",ans+1);
if(ans[1]!='Y') {
for(i=1;i<=m;++i)
putchar(ans[i]);
putchar('\n');
}
else
printf("-1\n");
}
return 0;
}
/*
1
20
3 5 13 2 19 9 20 6 11 4 10 8 7 17 15 1 18 14 16 18 15 17 7 12 8 10 4 11 6 20 9 19 2 13 3 5 1 14 16 12
*/
posted @   WE-R  阅读(118)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示