codeforces补题(167 + 954)

codeforces167

codeforces167B

Problem - B - Codeforces

这道题的答案可以变成x + a + y,那么我们就通过遍历寻找最小长度的out就可以了


t = int(input())
for _ in range(t):
    a = input()
    b = input()
    out = a + b
    aa = len(a) + len(b)
    for i in range(len(b) + 1):
        j = i
        for c in a:
            if j < len(b) and c == b[j]:
                j += 1
        if aa + i - j < len(out):
            out = b[:i] + a + b[j:]
    print(len(out))

codeforces167C

Problem - C - Codeforces

这道题我们需要找到有几个是相同的1有几个是相同的-1,如果不相同或者相同的0,我们都要取最大的,最后一次遍历寻找两部电影中评分最小的与上一次的ans对比谁更大就取谁

from math import inf
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int, input().split()))
    a1,b1 = 0,0
    sua,sub = 0,0
    for i in range(n):
        if a[i] == b[i] == 0 or a[i] != b[i]:
            if a[i] > b[i]:
                sua += a[i]
            else:
                sub += b[i]
        else:
            if a[i] == b[i] == -1:
                b1 += 1
            else:
                a1 += 1
    ans = -inf
    for i in range(-b1,a1 + 1):
        ans = max(ans,min(sua + i,sub + (a1 - b1 - i)))
    print(ans)

codeforces954

codeforces954C

Problem - C - Codeforces

  1. 对于每个位置 x 在列表 a 中:
    • 将 s 中第 x-1 个位置(由于列表索引从0开始,而题目中位置从1开始,所以需要减1)的字符替换为 b 中对应位置的字符。
  2. 最后,将处理后的 s 打印出来,其中的字符直接连接在一起输出。
for _ in range(int(input())):
    n,m = map(int, input().split())
    s = list(input())
    a = sorted(set(map(int, input().split())))
    b = sorted(input())
    i = 0
    for x in a:
        s[x-1] = b[i]
        i+=1
    s = ['a','b','b','v']
    result = ''.join(s)
    print(result)

codeforces954D

Problem - D - Codeforces

这道题我分了很多情况,首先是把所有最终结果是0的先进行输出然后进入下一个案例,剩下的最小只能是1或者是其他,然后再来一个循环来寻找这些数中的两位数,然后再进行遍历,如果这个数是1,那就加0,如果最终结果res是0,那就要把0变成1。

from math import inf
for _ in range(int(input())):
    n = int(input())
    a = input()
    s = list(map(int,a.strip()))
    ans = inf
    if n == 2:
        print(int(a))
        continue
    if (a[0] == '0' or a[2] == '0') and n == 3:
        print(0)
        continue
    if '0' in a and n > 3:
        print(0)
        continue
    for i in range(n - 1):
        res = int(str(s[i]) + str(s[i + 1]))
        if res == 1:
            res = 0
        j = 0
        while j < n:
            if j == i:
                j += 2
                continue
            if s[j] != 1:
                res += s[j]
            j += 1
        if res == 0:
            res = 1
        ans = min(ans,res)
    print(ans)

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值