PDD 03/12 机试题

"""
1. 压缩字符串解码,aaabbc可以压缩为3a2b1c,现在给了压缩字符串问原字符串是啥。模拟一下就好了
"""
s = input()
i = 0
n = len(s)
res = ''
while i < n:
    j = i
    while j < n and s[j].isdigit():
        j += 1
    res += s[j]*int(s[i:j])
    i = j + 1
print(res)

"""
2. 游戏有t个独立关卡,每个关卡里有n个敌人,你可以发射两种炮弹一种能够直接秒杀一个敌人。一种能发射两个子弹飞别扣两个敌人1滴血(两个飞弹不能都命中同一敌人),问每一关最少需要发射几次? (血量为1的,一起用双发的划算,大于等于2的直接单发)
"""
t, n = map(int, input().split())
res = []
for i in range(t):
    a = list(map(int, input().split()))
    eq1 = 0
    for x in a:
        if x == 1:
            eq1 += 1
    eq2 = len(a) - eq1
    res.append((eq1+1)//2 + eq2)
for x in res:
    print(x)



"""
3. 有A B C三种方案,给定每种方案的成本和容纳的人数上限,以及n个人和每个人的若干个意向,问是否能够满足所有人的意向,如果能,最低成本是多少,如果不能,最多满足的人数是多少? 暴力法,先选最便宜的,超过上限就往贵的转(90%)
"""
def s2nums(s):
    res = []
    if 'A' in s:
        res.append(0)
    if 'B' in s:
        res.append(1)
    if 'C' in s:
        res.append(2)
    return res


n = int(input())
a = [s2nums(input()) for i in range(n)]
b = []
# limit coin id
for i in range(3):
    b.append(list(map(int, input().split())) + [len(b)])
# 优先选价格低的
rank = sorted(b[:], key=lambda x: x[1])
allo = [-1]*n

for r in range(3):
    for i in range(n):
        if rank[r][-1] in a[i] and allo[i] == -1:
            # cnt[rank[r][-1]] += 1
            allo[i] = rank[r][-1]
    sm = 0
    for x in allo:
        if x == rank[r][-1]:
            sm += 1
    if sm > rank[r][0]:
        for k in [1, 2]:
            if r + k > 2:
                break
            nr = r + k
            for i in range(n):
                if sm <= rank[r][0]:
                    break
                # send to another
                if rank[nr][-1] in a[i] and allo[i] == rank[r][-1]:
                    allo[i] = rank[nr][-1]
                    # cnt[rank[nr][-1]] += 1
                    sm -= 1

# check
if sum(x == 0 for x in allo) <= b[0][0] and sum(x == 1 for x in allo) <= b[1][0] and sum(x == 2 for x in allo) <= b[2][0]:
    print("YES")
    sm = 0
    for x in allo:
        sm += b[x][1]
    print(sm)
else:
    print("NO")
    # 优先选容量小的
    rank = sorted(b[:], key=lambda x: x[0])
    allo = [-1] * n

    for r in range(3):
        for i in range(n):
            if rank[r][-1] in a[i] and allo[i] == -1:
                # cnt[rank[r][-1]] += 1
                allo[i] = rank[r][-1]
        sm = 0
        for x in allo:
            if x == rank[r][-1]:
                sm += 1
        if sm > rank[r][0]:
            for k in [1, 2]:
                if r + k > 2:
                    break
                nr = r + k
                for i in range(n):
                    if sm <= rank[r][0]:
                        break
                    # send to another
                    if rank[nr][-1] in a[i] and allo[i] == rank[r][-1]:
                        allo[i] = rank[nr][-1]
                        # cnt[rank[nr][-1]] += 1
                        sm -= 1

    print(min(b[0][0], sum(x == 0 for x in allo)) + min(b[1][0], sum(x == 1 for x in allo)) + min(b[2][0], sum(x == 2 for x in allo)))

"""
4 给定一个长为n的序列,求前i个数的中位数和平均值(i取1-n),结果四舍五入。 对顶堆求中位数,只过了40%,有大佬看看问题在哪?不知道是不是精度出错了导致取整有问题。
"""
import heapq
class WrapLow:
    def __init__(self, x):
        self.x = x

    def __lt__(self, other):
        return self.x > other.x

# python 默认小根堆,大根堆直接对值取负数也行,但是习惯用wrapper了
class WrapHigh:
    def __init__(self, x):
        self.x = x

    def __lt__(self, other):
        return self.x < other.x


n = int(input())
a = list(map(int, input().split()))
s = 0
mean = []
median = []
hp_low, hp_high = [WrapLow(-1e9)], [WrapHigh(1e9)]


for i in range(n):
    s += a[i]
    mean.append(round(s / (i + 1) + 0.1))
    if a[i] <= hp_high[0].x:
        heapq.heappush(hp_low, WrapLow(a[i]))
    else:
        heapq.heappush(hp_high, WrapHigh(a[i]))

    while len(hp_low) - len(hp_high) > 1:
        x = heapq.heappop(hp_low).x
        heapq.heappush(hp_high, WrapHigh(x))

    while len(hp_high) - len(hp_low) > 0:
        x = heapq.heappop(hp_high).x
        heapq.heappush(hp_low, WrapLow(x))

    if (i+1) & 1:
        median.append(hp_low[0].x)
    else:
        median.append(int(round(0.1 + (hp_high[0].x + hp_low[0].x) / 2)))

print(' '.join(map(str, mean)))
print(' '.join(map(str, median)))

全部评论
我只有第一题AK了
点赞 回复 分享
发布于 2023-03-13 02:59 英国

相关推荐

原来已经一年了,因为没有加任何实验室没有学长学姐带,再一次偶然的机会下刷到我们学校的牛肉哥,和他聊天之后发现他也没加实验室能进大厂,我就燃起了希望,去年大概&nbsp;4&nbsp;月份找好路线&nbsp;零基础&nbsp;开始学&nbsp;5&nbsp;月背八股和开始刷算法很难受&nbsp;7-8&nbsp;月焦虑躯体化害怕找不到实习&nbsp;9&nbsp;月找到一家像样的小厂去实习了&nbsp;4&nbsp;个月大三上期末考试结束之后&nbsp;1&nbsp;月份回来边实习边准备工作压力很大&nbsp;当时只有字节、百度、商汤的面试,字节三面挂了,百度&nbsp;oc,商汤&nbsp;二面挂(差评&nbsp;无效面试),之后来深圳百度实习之后还是觉得不甘心一直没把算法和八股扔下一直在准备,百度实习的时候&nbsp;mt&nbsp;交给我一个特别重要的工作数据库迁移(特别感谢&nbsp;mt&nbsp;,这个需求学到了很多东西处理了一堆线上问题),本来看着暑期他们面试都很困难,然后听说百度要涨实习薪资(然而&nbsp;5&nbsp;月并没有涨),就想着留在百度吧也懒得面试了,4&nbsp;月&nbsp;20&nbsp;多的时候字节&nbsp;hr&nbsp;打电话约面问我要不要尝试一下询问了&nbsp;1&nbsp;月份三面为啥会挂有没有学习&nbsp;ai&nbsp;知识(因为字节这边后端岗位偏&nbsp;ai),我来到百度之后全面拥抱&nbsp;AI&nbsp;也认识了我的好兄弟&nbsp;X&nbsp;哥,他在百度&nbsp;XX&nbsp;部门&nbsp;Agent&nbsp;实习,他属于是我&nbsp;Agent&nbsp;的启蒙老师,来百度之后一直在了解&nbsp;AI&nbsp;这一块,我就接受了字节的面试,一面的时候&nbsp;20&nbsp;分钟实习拷打然后突然说&nbsp;30&nbsp;分钟代码考核我心就凉了以为是&nbsp;kpi,算法题是手撕高并发安全下的令牌桶限流器,我写了整整&nbsp;80&nbsp;多行代码最后也写出来了,但是从来没看到过出这种题能&nbsp;oc&nbsp;的我也就不管了,后边面试也是很顺利但是流程有点长可能一直在横向吧总结结果是好的!!!感谢这一年努力的自己和遇到的各位互联网大佬分享的知识!!!ps&nbsp;图二纯感慨&nbsp;(觉得🍬请不要喷我)欢迎大家一起交流学习呀!!!!
点赞 评论 收藏
分享
评论
1
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务