zoukankan      html  css  js  c++  java
  • codeforces_1040_A Python练习

    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 = 0
    tail = n-1
    tot = 0
    if (a>b):
        d = b
    else:
        d = a
    
    while (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 -= 1
    if (n%2 == 1):
        if (c[n//2] == 2):
            tot += d
    print(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)
    

      

      

  • 相关阅读:
    SQL0668N 由于表 "db2inst1.test" 上的原因代码 "3",所以不允许操作(解因为LOAD引起的LOAD暂挂状态锁)
    重装系统后,如何恢复DB2数据库?
    db2 导入导出命令大全
    解决tomcat启动时日志出现 javax.naming.NamingException: Invalid byte 1 of 1byte UTF8 sequence.的问题
    【fcntl系统调用】
    C++读书笔记之函数模板
    unix shell笔记
    用setsockopt()来控制recv()与send()的超时 【转】
    Linux守护进程的编程实现 [转]
    GDB调试core文件样例(如何定位Segment fault) 【转】
  • 原文地址:https://www.cnblogs.com/oxxxo/p/9844101.html
Copyright © 2011-2022 走看看