博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces_1040_A Python练习
阅读量:4499 次
发布时间:2019-06-08

本文共 2210 字,大约阅读时间需要 7 分钟。

codeforces1040A Palindrome Dance

A group of nn 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.

原意:给出一串数字,让他们回文,而且费用最小

题解:一个前指针head,一个后指针tail

四种情况:1.c[head]+c[tail]=1 表明是0+1,输出错误

      2.c[head]+c[tail]=2 表明⑴1+1 那么跳过 ;⑵0+2,那么选白色的

      3.c[head]+c[tail]=3 表明1+2 那么选黑色的

      4.c[head]+c[tail]=4 表明2+2,选费用少的衣服

代码:

n,a,b = map(int,input().split())c = list(map(int,input().split()))head = 0tail = n-1tot = 0if (a>b):    d = belse:    d = awhile (head < tail):    if (c[head]+c[tail] == 2 and (c[tail]!=c[head])):       tot += a    elif (c[head]+c[tail] == 3):        tot += b    elif (c[head] + c[tail] == 4):        tot += d*2    elif (c[head]+c[tail] == 1):        print(-1)        exit()    head += 1    tail -= 1if (n%2 == 1):    if (c[n//2] == 2):        tot += dprint(tot)

  

顺便贴上学到的语法:

read = lambda :list(map(int,input().split()))n,a,b = read()c = read()print(n,a,b)print(c)n,a,b = [*map(int,input().split())]c = [*map(int,input().split())]print(n,a,b)print(c)

  

  

转载于:https://www.cnblogs.com/oxxxo/p/9844101.html

你可能感兴趣的文章
python之函数用法file()
查看>>
order by
查看>>
Web应用程序开发知识点回顾
查看>>
NetMQ介绍
查看>>
CentOS 6.0 系统 LAMP(Apache+MySQL+PHP) 安装步骤
查看>>
oracle 内连接 左外连接 右外连接的用法,(+)符号用法
查看>>
深入理解javascript闭包
查看>>
敏捷练习(1)评估我的生活方向盘
查看>>
web版微信自动发消息(实现微信个人号机器人)
查看>>
【C/C++】产生随机数
查看>>
dp_c_区间dp_g
查看>>
C#Dictionary键值对取值用法
查看>>
关于动态绑定时遇到的问题:
查看>>
java 并发——线程
查看>>
C#排序算法小结
查看>>
什么是Servlet以及如何开发一个Servlet
查看>>
非程序员的GNU Emacs使用心得...... Shell Mode 第1集 序言
查看>>
GRU-CTC中文语音识别
查看>>
18.C#扩展方法(十章10.1-10.2)
查看>>
[笔记]C#基础入门(三)——C#的常量
查看>>