惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
O
OpenAI News
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Webroot Blog
Webroot Blog
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
N
News | PayPal Newsroom
H
Hacker News: Front Page
博客园_首页
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Last Watchdog
The Last Watchdog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Heimdal Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Schneier on Security
宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
Microsoft Security Blog
Microsoft Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
GbyAI
GbyAI
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
人人都是产品经理
人人都是产品经理
P
Palo Alto Networks Blog
M
MIT News - Artificial intelligence
G
GRAHAM CLULEY
C
Check Point Blog
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
T
Troy Hunt's Blog
L
Lohrmann on Cybersecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
量子位
博客园 - 聂微东
S
Securelist
博客园 - 三生石上(FineUI控件)
F
Full Disclosure
G
Google Developers Blog
L
LINUX DO - 热门话题
P
Proofpoint News Feed
AI
AI
PCI Perspectives
PCI Perspectives

Makerlife 的小站

2025-2026 赛季 游记 && 退役记 NOIP 2024 游记 集训记录 CSP2024 游记 板子库 动态规划 刷题记录 杂题乱记 数学期望 学习笔记 数论 学习笔记 CF1000F One Occurrence 题解 P6878 [JOI 2020 Final] JJOOII 2 题解 CSP2023 游寄 主定理 CF1695C Zero Path 题解 字符串算法全家桶 学习笔记 AT_ABC306D 题解 2023.06.03 模拟赛 Azure for Students 使用指北 AT_ABC286C 题解 洛谷 AT1898 题解 洛谷 CF1036A 题解 洛谷 SP3591 题解 洛谷 AT278 题解 洛谷 CF141B 题解 洛谷 AT2561 题解 洛谷 AT3525 题解 洛谷 CF899B 题解 洛谷 AT4787 题解 洛谷 AT4810 题解 洛谷 SP5450 题解
洛谷 CF1040A 题解
Makerlife · 2022-07-12 · via Makerlife 的小站

洛谷题目传送门 | CF 原题传送门

基础回文数判断题。

感觉题目翻译并不是很好,需要注意回文串中不能出现 22,例如 102011 0 2 0 1 不能作为最终答案。

思路

直接把数组从 11 到 $\lfloor \frac{n}{2}\rfloor $ 扫一遍即可。

有以下几种情况需要分类讨论:

  1. 如果原串首尾不同但不是 22,即形如 1010010100 这样的,无法变为回文串,直接输出 −1-1
  2. 如果原串首尾都为 22,只需要把两个 22 都换成 0011代价最小的数
  3. 如果原串首尾有且仅有一个是 22,把那一个 22 改为和另一端相同的数
  4. 最后再特判 nn 为奇数的情况,如果中间数为 22ansans 需要再加上 min⁡(a,b)\min(a,b)形成不含 22 的回文串

然后这道题就很简单了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<cmath>
#include<iostream>
using namespace std;
inline int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0' || ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0' && ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
int n,a,b,t[30],ans=0;
int main()
{
n=read();a=read();b=read();
for(int i=1;i<=n;i++) t[i]=read();
for(int i=1;i<=n/2;i++)
{
if((t[i]==0 && t[n-i+1]==1) || (t[i]==1 && t[n-i+1]==0))//首尾不同且不是2
{
cout<<-1<<endl;
return 0;
}
else if(t[i]==2 && t[n-i+1]==2)//首尾都为2
{
ans+=min(a,b)*2;
if(a<b) t[i]=t[n-i+1]=0;
else t[i]=t[n-i+1]=1;
}
else if(t[i]!=2 && t[n-i+1]==2)//结尾为2
{
if(t[i]==0) ans+=a,t[n-i+1]=0;
else ans+=b,t[n-i+1]=1;
}
else if(t[i]==2 && t[n-i+1]!=2)//开头为2
{
if(t[n-i+1]==0) ans+=a,t[i]=0;
else ans+=b,t[i]=1;
}
}
if(n%2==1 && t[n/2+1]==2) ans+=min(a,b);//n为奇数需要特判
cout<<ans<<endl;
return 0;
}