Codeforces Round #946 (Div. 3)(A-E) python

A. Phone Desktop

https://codeforces.com/contest/1974/problem/A

19d82188a3a34b03b6470c2ce74349c7.png

签到题,贪心

一个屏幕最多能装两个大方格,先放完大的,剩下的空隙放小的,不够再补

t = int(input())
for _ in range(t):
    x, y = map(int, input().split())
    res = 0
    if y % 2 == 0:
        res += y//2
        las = 7*res
    else:
        res += (y+1)//2
        las = res*7+4
    if x > las:
        res += (x-las+14)//15
    print(res)

B. Symmetric Encoding

https://codeforces.com/contest/1974/problem/B

2a76a0992e34439985d93840ec3b88b1.png

排序

存下出现的字符,排序后第i个对应第m-i-1个字符

from collections import Counter
t = int(input())
for _ in range(t):
    n = int(input())
    b = list(input())
    lis = [0]*150
    for bi in b:
        lis[ord(bi)] = 1
    lis = [i for i in range(150) if lis[i] == 1]
    lis.sort()
    dic = [0]*150
    m = len(lis)
    for i in range(m):
        dic[lis[i]] = m-i-1
    for bi in b:
        print(chr(lis[dic[ord(bi)]]), end="")
    print()

C. Beautiful Triple Pairs

Problem - C - Codeforces

94bb84ec505448778db965187bd71d60.png

求只有一个固定位置不同,其余相同的三元组对数

哈希表存一下不同的分别为第1,2,3位时,不同取值对应的剩下元素值

最后计算该位置取值个数与,其他位置相同但该位置不同的个数的乘积

避免重复计算,每次计算后可以将该位置字符个数减掉

from collections import *
M = int(1e6+10)
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    c = [defaultdict(lambda: Counter()) for _ in range(3)]
    for i in range(1, n-1):
        x, y, z = a[i-1], a[i], a[i+1]
        c[0][(y, z)][x] += 1
        c[1][(x, z)][y] += 1
        c[2][(x, y)][z] += 1
    res = 0
    for i in range(3):
        for ci in c[i]:
            num = sum(c[i][ci].values())
            for x in c[i][ci]:
                ct = c[i][ci][x]
                res += (num-ct)*ct
                num -= ct
    print(res)

D. Ingenuity-2

Problem - D - Codeforces

b07fbbf513de4c9c93437c8fb2804198.png

模拟

要使两者能走到同一点,那么把所有操作累加起来最终x,y一定能平分,否则不行

将最终x,y除二,即可得到他们的终点,然后模拟其中一者走到终点的过程即可

注意,若以(0, 0)为终点时,要手动使其完成一次来回的操作,防止出现未行动的可能

最后判断终点是否正确,是否都进行过行动

def solve():
    x, y = 0, 0
    for si in s:
        x, y = x+dxy[si][0], y+dxy[si][1]
    if (x % 2 == 1) or (y % 2 == 1):
        return "NO"
    if x == 0 and y == 0:
        flag = 1
    else:
        flag = 0
    x, y = x//2, y//2
    res = [0]*n
    for i in range(n):
        si = s[i]
        # print(dxy[si], x, y, end="-->")
        if si == "E" or si == "W":
            dx = dxy[si][0]
            if abs(x-dx) < abs(x):
                x -= dx
                res[i] = 1
        else:
            dy = dxy[si][1]
            if abs(y-dy) < abs(y):
                y -= dy
                res[i] = 1
        # print(x, y)
        if x == 0 and y == 0:
            if flag == 1:
                flag = 0
                x, y = x-dxy[si][0], y-dxy[si][1]
                res[i] = 1
            else:
                break
    if x != 0 or y != 0:
        return "NO"
    res = ["R" if i == 1 else "H" for i in res]
    R = res.count("R")
    # print(x, y, res)
    if R != 0 and R != n:
        return "".join(res)
    return "NO"


dxy = {"N": (0, 1), "S": (0, -1), "E": (1, 0), "W": (-1, 0)}
t = int(input())
for _ in range(t):
    n = int(input())
    s = input()
    res = solve()
    print(res)

E. Money Buys Happiness

Problem - E - Codeforces

1f226d22716641cb93b90b2a2766a504.png

动态规划,贪心

观察数据,发现c很大但m和h很小,考虑以当前的幸福度为容量进行dp,

哈希表存储当前幸福的所需花费的最小值,遍历每一月,若钱数符合则更新哈希表取最小花费,最后找能得到的最大幸福度

t = int(input())
for _ in range(t):
    m, x = map(int, input().split())
    ch = [list(map(int, input().split())) for _ in range(m)]
    money = 0
    w = {0: 0}
    for i in range(m):
        c, h = ch[i]
        nw = w.copy()
        for j in w:
            if money-w[j] >= c:
                if nw.get(j+h, -1) == -1:
                    nw[j+h] = int(1e18)
                nw[j+h] = min(nw[j+h], w[j]+c)
        money += x
        w = nw
    res = max(w.keys())
    print(res)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值