Codeforces Round 955 (Div. 2) (A~D) python

A. Soccer

Problem - A - Codeforces

75b7f36532c54b1d9d773196bd4ff82d.png

若x2 != x1且两队优劣较之前无有变化,则有可能从未相同

t = int(input())
for _ in range(t):
    x1, y1 = map(int, input().split())
    x2, y2 = map(int, input().split())
    if x2 != y2 and ((x1 > y1 and x2 > y2) or (x1 < y1 and x2 < y2)):
        print("YES")
    else:
        print("NO")

B. Collatz Conjecture

Problem - B - Codeforces

57ba382560a64f1a985e57b9e18cd7c5.png

模拟,数学

每次加到能整除k,然后除到不能整除为止,剩下不够加到能整除,x直接加上剩余k,打印答案

若x在过程中变为1,则每k-1轮会再次变成1,退出单独判断

t = int(input())
for _ in range(t):
    x, y, k = map(int, input().split())
    while k > 0:
        nd = y - (x % y)
        if k > nd:
            x += nd
            k -= nd
        else:
            break
        while x % y == 0:
            x //= y
        if x == 1:
            break
    if k >= (y-1) and x == 1:
        k %= (y-1)
    if k > 0:
        x += k
    while x % y == 0:
        x //= y
    print(x)

C. Boring Day

Problem - C - Codeforces

6954130f444140ffadc0f10dc87ee730.png

数据结构,双端队列,贪心

用双端队列维护当前轮的牌,记录总点数,按顺序拿牌,每次能获胜则直接结束,否则若小于l则继续拿,大于r则将最前面的牌移出,直到当前牌空或者不再大于r,以模拟该牌在之前被放弃的情况

from collections import *


def check(x):
    global s, d, res
    if l <= x <= r:
        res += 1
        s = 0
        d = deque()
        return True
    return False


t = int(input())
for _ in range(t):
    n, l, r = map(int, input().split())
    a = list(map(int, input().split()))
    res = 0
    d, s = deque(), 0
    for i in range(n):
        d.append(a[i])
        s += a[i]
        if check(s):
            continue
        elif s > r:
            while s > r:
                s -= d.popleft()
            if check(s):
                continue
    print(res)

D. Beauty of the mountains

Problem - D - Codeforces

290cfd68457e48f18c135dc5a8f1e6c1.png

暴力枚举,前缀和,数学

要使两种山的高度和相同,只需要消除山高度的总差值,

每次可以使k*k的子矩阵同时加减任意数,只需计算每个子矩阵0,1的数量差,每次操作可使山的差距变化差值的整数倍

当存在一些差值的gcd是山总差值的因子时,则可通过重复多次固定的操作将山的总差值消除

from math import gcd


def check():
    c = 0
    for i in range(n):
        for j in range(m):
            if mp[i][j] == 1:
                c += a[i][j]
            else:
                c -= a[i][j]
    c = abs(c)
    if c == 0:
        return True
    s = [[0] * (m + 1) for _ in range(n + 1)]
    for i in range(1, n + 1):
        for j in range(1, m + 1):
            s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + mp[i - 1][j - 1]
    k2 = k * k
    now = -1
    for i in range(1, n - k + 2):
        for j in range(1, m - k + 2):
            x, y = i + k - 1, j + k - 1
            one = s[x][y] - s[i - 1][y] - s[x][j - 1] + s[i - 1][j - 1]
            zero = k2 - one
            b = abs(one - zero)
            if b == 0:
                continue
            elif now == -1:
                now = b
            else:
                now = gcd(b, now)
            if c % now == 0:
                return True
    return False


t = int(input())
for _ in range(t):
    n, m, k = map(int, input().split())
    a = [list(map(int, input().split())) for _ in range(n)]
    mp = [list(map(int, list(input()))) for _ in range(n)]
    if check():
        print("YES")
    else:
        print("NO")

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值