codeforces 1040a (回文数的舞蹈)

本文介绍了一个关于回文舞蹈的问题,通过分析舞蹈队伍的颜色排列,找出形成回文所需的最小成本。代码实现中考虑了多种情况,包括已有的白色或黑色套装,以及未来可能购买的套装。

原题链接
A. Palindrome Dance
A group of 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.
题目大意:这个题目本意就是回文串的应用,在这之间还加入了价值的运算,思维量不大,注意中间的一些细节就好了。。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

int f[21];
int n,a,b;

int main()
{
    while(cin>>n>>a>>b)
    {
        for(int i=0;i<n;i++) cin>>f[i];
        int sum=0;
        int flag=0;
        for(int i=0;i<n/2;i++)
        {
            if(f[i]!=f[n-i-1])
            {
                if(!(f[i]==2||f[n-i-1]==2))//1 1 2 1 0这样的就不可以了
                {
                    flag=1;
                    break;
                }
                else 
                {
                    if(f[i]!=2) sum+=(f[i]==0)?a:b;
                    if(f[n-i-1]!=2) sum+=(f[n-i-1]==0)?a:b;
                }
            }
            else if(f[i]==f[n-i-1]&&f[i]==2) sum+=2*min(a,b);
        }
        if(n&1)/n为奇数的时候需要特判一下
        {
            if(f[n/2]==2) sum+=min(a,b);
        }
        if(flag) cout<<"-1"<<endl;
        else cout<<sum<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值