CF1040A Palindrome Dance

本文解析了Codeforces平台上的A题PalindromeDance,该题要求在不改变舞者位置的情况下,通过购买最少成本的服装形成回文序列。文章详细介绍了输入输出格式,给出了解题思路和代码实现,帮助读者理解如何解决此类问题。

原题链接:http://codeforces.com/contest/1040/problem/A

Palindrome Dance

A group of n n n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they’ve studied their dancing moves and can’t change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.

On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer’s suit is the same as the color of the rightmost dancer’s suit, the 2nd left is the same as 2nd right, and so on.

The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it’s possible, what’s the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.

Input

The first line contains three integers n n n, a a a, and b ( 1 ≤ n ≤ 20 , 1 ≤ a , b ≤ 100 ) b (1≤n≤20, 1≤a,b≤100) b(1n20,1a,b100) — the number of dancers, the cost of a white suit, and the cost of a black suit.

The next line contains n n n numbers c i , i c_i, i ci,i-th of which denotes the color of the suit of the i i i-th dancer. Number 0 0 0 denotes the white color, 1 1 1 — the black color, and 2 2 2 denotes that a suit for this dancer is still to be bought.

Output

If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.

Examples
input

5 100 1
0 1 2 1 2

output

101

input

3 10 12
1 2 0

output

-1

input

3 12 1
0 1 0

output

0

Note

In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.

In the second sample, the leftmost dancer’s suit already differs from the rightmost dancer’s suit so there is no way to obtain the desired coloring.

In the third sample, all suits are already bought and their colors form a palindrome.

题解

因此题过水,题解被折叠。

代码
#include<bits/stdc++.h>
using namespace std;
const int M=25;
int n,a,b,ans,x1[M],x2[M];
void in(){scanf("%d%d%d",&n,&a,&b);for(int i=1;i<=n;++i)scanf("%d",&x1[i]),x2[n-i+1]=x1[i];}
void ac()
{
	for(int i=1;i<=n;++i)
	if(x1[i]==2)ans+=(x2[i]==2?min(a,b):(x2[i]?b:a));
	else if(x1[i]!=x2[i]&&x2[i]!=2)puts("-1"),exit(0);
	printf("%d",ans);
}
int main(){in();ac();}
CF1904A 是 Codeforces 平台上的一道编程题目,通常这类问题要求参赛者在限定时间内设计高效的算法来解决问题。尽管具体的题目细节未直接提供,但根据 Codeforces 的题号命名规则和题面风格,CF1904A 通常会涉及基础到中等难度的算法技巧,例如模拟、贪心、简单数据结构或数学推理。 ### 解题思路 CF1904A 通常作为比赛的第一题,难度相对较低,主要考察逻辑思维与对题意的理解能力。根据常见的 Codeforces A 题风格,可能涉及以下几种思路: 1. **直接模拟**:某些问题可以通过直接按照题目描述进行模拟来解决。例如,题目可能要求计算某种操作的最终结果,而该操作可以通过简单的循环或条件判断来实现。 2. **数学推导**:部分题目可能要求找出输入与输出之间的数学关系。例如,给定一个序列或两个变量,需要推导出一个公式来快速计算结果,而不是通过暴力方式。 3. **贪心策略**:如果问题涉及选择或决策,通常可以通过贪心算法来解决。例如,在每一步选择当前最优解,最终得到整体最优解。 4. **特殊情况处理**:某些题目可能包含边界条件或特殊情况,需要单独处理。例如,输入为 0、1 或者极小值时,常规逻辑可能不适用。 5. **字符串或数组操作**:如果题目涉及字符串处理或数组操作,通常可以通过简单的遍历或替换操作来完成。 --- ### 示例代码 假设 CF1904A 的题目要求是:给定一个由小写字母组成的字符串,你需要判断是否可以通过删除最多一个字符使得字符串成为回文串。 ```python def is_palindrome(s): return s == s[::-1] def can_remove_one_char(s): left, right = 0, len(s) - 1 while left < right: if s[left] != s[right]: # 尝试删除左边或右边字符 skip_left = s[left+1:right+1] skip_right = s[left:right] return skip_left == skip_left[::-1] or skip_right == skip_right[::-1] left += 1 right -= 1 return True # 输入字符串 s = input().strip() print("YES" if can_remove_one_char(s) else "NO") ``` 上述代码展示了如何通过贪心策略解决回文判断问题,适用于某些特定的 CF A 题。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值