ZOJ Problem Set

第32页

Sequence in the Pocket

思维题
给定一个序列,每次可以将一个元素移到最前面,问你最少使用多少次,将元素排成上升序列。
解题思路:其实可以先创建一个新数组进行排序,排序后的数组和原先数组从后往前有多少个不一样的元素就是移动的次数

T = int(input())

for _ in range(T):
    n = int(input())
    arr = list(map(int, input().split()))
    sorted_arr = sorted(arr)

    j = len(sorted_arr) - 1
    for i in range(len(arr) - 1, -1, -1):
        if arr[i] == sorted_arr[j]:
            j = j - 1

    print(j + 1)

Abbreviation

给出一个字符串 要你将里面的a e i y o u删掉并输出 注意!首位不需要删掉

t = int(input())
vowel = {'a', 'e', 'i', 'y', 'o', 'u'}
for _ in range(t):
    word = input()
    new=""
    for i in range(len(word)):
        if i==0 or word[i] not in vowel:
            new = new +word[i]
    print(new)

Lucky 7 in the Pocket

给出一个数 n,输出一个大于等于 n 的数 m,要求 m 能被 7 整除但不能被 4 整除

T = int(input())
for _ in range(T):
    n = int(input())
    m = n
    while True:
        if m % 7 == 0 and m % 4 != 0:
            break
        m = m + 1
    print(m)

Calandar

有一个神奇的星球,在那里,一年有12个月,每月都有30天。并且在那个星球上一周只有五天。给你今天的日期和星期,问你一个目标日期是星期几。

只要求出两个日的关系,再输出对应的星期即可


day =["Monday", "Tuesday", "Wednesday", "Thursday" , "Friday"]

T = int(input())
for _ in range(T):
    y1, m1, d1, today_day = input().split()
    y1, m1, d1 = int(y1), int(m1), int(d1)
    y2, m2, d2 = map(int, input().split())
    total_days = (y2 - y1) * 12 * 30 + (m2 - m1) * 30 + (d2 - d1)

    for i in range(len(day)):
        if day[i]==today_day:
            today_index = i
    if total_days>=0:
        target_index = (today_index + total_days) % 5
    else:
        total_days = abs(total_days)%5
        target_index = (today_index+5 -total_days)%5
    print(day[target_index])

Stones in the Bucket

这题的要求是让你把每块石碓平均一下,每次只能移动一块石头或者直接丢掉一块石头,问你最少的次数

其实你只要把石头总数求和再平均一下,对于超过平均数的石头块减一下就是答案

T = int(input())
for _ in range(T):
    n = int(input())
    stones = list(map(int, input().split()))
    total = sum(stones)
    average = total//n
    cnt = 0
    for i in range(n):
        if stones[i]>average:
            cnt = cnt+ stones[i] -average
    print(cnt)

League of Legends

蓝方的血量是红方减的;红方的血量是蓝方减的。蓝方先手,减的是红方血量。

因此除了统计红方和蓝方的总血量,要注意蓝方先手,红方先被扣血。当蓝方血量大于等于红方血量,都是蓝方赢,否则相反。

blue_hp = list(map(int, input().split()))
red_hp = list(map(int, input().split()))
total_blue = sum(blue_hp)
total_red = sum(red_hp)
if total_blue >= total_red:
    print("Blue")
else:
    print("Red")

第31页

PPAP

让两个字符串相加,第一个字符串首字符变大写,第二个首字符删除

T = int(input())
for _ in range(T):
    a, b = input().split()
    s1 = b + a
    s2 = s1[0].upper() + s1[1:]
    print(s2)

Peak

让你在字符串中找一个山峰,只需要从左最大山峰和从右最大的山峰下标相等就可以输出Yes否则输出No

t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int,input().split(" ")))
    l=0
    r=n-1
    for i in range(n-1):
        if a[i]>= a[i+1]:
            l=i
            break
    for i in range(n-1, 0,-1):
        if a[i-1]<=a[i]:
            r=i
            break
    if l==r :
        print("Yes")
    else:
        print("No")

King of Karaoke

有两个序列D和S,D加上K之后和S有多少个数是相同的

T = int(input())
for _ in range(T):
    n = int(input())
    d = list(map(int, input().split()))
    s = list(map(int, input().split()))
    diff = {}
    res = 0
    for i in range(n):
        a = d[i]-s[i]
        if a in diff:
            diff[a]=diff[a]+1
        else:
            diff[a] = 1
    print(max(diff.values()))

Lucky 7

一个序列,只要有一个元素+b能被7整除就是Yes,如果全部都不能整除就是No

T = int(input())
for _ in range(T):
    n, b = map(int, input().split())
    a = list(map(int, input().split()))
    f =0
    for i in range(n):
        if (a[i]+b)%7==0:
            f=1
            break
    if f==1:
        print("Yes")
    else:
        print("No")

Peer Review

有n个同学,每个学生必须分别对其他(n-1)个学生打分,那么自然,为了让自己分数最高,给其他人都打0分的,所以所有人的分数都是0

同学们,冤冤相报何时了!

T = int(input())
for _ in range(T):
    n = int(input())
    for i  in range(n):
        if i!=0:
            print(" ",end='')
        print("0",end='')
    print()

Live Love

最大连击数就是所有的PERFECT连在一起

最小连击数可以考虑n−m个 “NON-PERFECT” 排列好,它们之间以及两端会形成 n−m+1个间隔。之后我们m个PERFECT尽量平均地放入这些间隔中,即m/(n-m+1),当然最后要考虑向上取整的问题。

import math

T = int(input())
for _ in range(T):
    n,m = map(int,input().split())
    r = m/(n-m+1)
    r = math.ceil(r)
    print(m,r)

第30页

27,36,47,58,59,92

Programming Ability Test

总成绩大于等于80则输出Yes,否则输出No

T = int(input())
for _ in range(T):
    ScoreA, ScoreB, ScoreC, ScoreD = map(int, input().split())
    total = ScoreA + ScoreB + ScoreC + ScoreD
    if total >= 80:
        print("Yes")
    else:
        print("No")

Apples and Ideas

知识可以共享,但苹果不是!所以交换后,苹果总数没有发生变化,但知识却是求和

T = int(input())
for _ in range(T):
    A, B, C, D = map(int, input().split())
    alice_apples = C
    alice_ideas = B + D
    bob_apples = A
    bob_ideas = B + D
    print(alice_apples, alice_ideas)
    print(bob_apples, bob_ideas)

Very Happy Great BG

对朋友总数求和即可

T = int(input())
for _ in range(T):
    N = int(input())
    friends = list(map(int, input().split()))
    total = N + sum(friends)
    print(total)

Cooking Competition

两个人比赛厨艺,有n个评委,每个评委可能给出4种评价,1表示A加1分,B不加分,2表示A不加分,B加1分,3表示两人各加1分,4表示两人各扣1分,问最后谁赢了比赛还是平局

T = int(input())
for _ in range(T):
    n = int(input())
    feedbacks = list(map(int, input().split()))
    kobayashi_score = 0
    tohru_score = 0
    for feedback in feedbacks:
        if feedback == 1:
            kobayashi_score += 1
        elif feedback == 2:
            tohru_score += 1
        elif feedback == 3:
            kobayashi_score += 1
            tohru_score += 1
        else:
            kobayashi_score -= 1
            tohru_score -= 1
    if kobayashi_score > tohru_score:
        print("Kobayashi")
    elif tohru_score > kobayashi_score:
        print("Tohru")
    else:
        print("Draw")

Problem Preparation

题目难度排序后判断即可

T = int(input())
for _ in range(T):
    n = int(input())
    s = list(map(int, input().split()))
    f = True
    if n<10 or n>13:
        print("No")
        continue
    s.sort()
    if s[0]!=1 or s[1]!=1:
        f = False
    for i in range(len(s) - 2):
        if abs(s[i + 1] - s[i]) > 2:
            f = False
    if f:
        print("Yes")
    else:
        print("No")

One-Dimensional Maze

思维题,解题的关键在于判断从起始位置到开头或到结尾的修改字符数哪个更少。具体步骤如下:

1‌.判断起始位置‌:如果起始位置在开头或结尾,可以直接到达目标位置,无需修改字符。
2‌.‌计算修改字符数‌:
从起始位置到开头的修改字符数:遍历从起始位置到开头的所有字符,将'R'改为'L'。
从起始位置到结尾的修改字符数:遍历从起始位置到结尾的所有字符,将'L'改为'R'。
3‌.‌选择最小修改次数‌:比较上述两种情况的修改次数,选择较小的那个作为答案。

t = int(input())
for _ in range(t):
    n, m = map(int, input().split())
    ch = input()
    if m == 1 or m == len(ch):
        print(0)
        continue
    ans1 = 0
    ans2 = 0
    for i in range(1, m - 1):
        if ch[i] == 'R':
            ans1 += 1
    for i in range(m, len(ch) - 1):
        if ch[i] == 'L':
            ans2 += 1

    if ch[m - 1] == 'L':
        print(min(ans1, ans2 + 1))
    else:
        print(min(ans1 + 1, ans2))

第29页

19,60,67,69,75,80

Average Score

把Bob成绩转移到另一个班级,使两个班级的成绩都提高。我们可以遍历[0,100]作为Bob的成绩,然后每次去判断是否满足条件,最终输出Bob成绩的最小最大值

t = int(input())
for _ in range(t):
    n,m = map(int,input().split())
    a = list(map(int,input().split()))
    sum1 = sum(a)
    b = list(map(int, input().split()))
    sum2 = sum(b)
    mymin = 101
    mymax = -1
    for i in range(0,101):
        avg1 = (sum1+i)/n
        avg2 = sum2/m
        if sum1/(n-1)>avg1 and (sum2+i)/(m+1)>avg2:
            mymin =min(mymin,i)
            mymax = max(mymax, i)
    print(mymin,mymax)

Find the Spy

在一个序列中找到不同的数,记录一下每个数字出现的个数,找到出现次数为1的数字就可以了

t = int(input())
for _ in range(t):
    count = {}
    n = int(input())
    a = list(map(int,input().split()))
    for num in a:
        if num in count:
            count[num] += 1
        else:
            count[num] = 1
    for num, freq in count.items():
        if freq == 1:
            print(num)

Earthstone: Easy Version

炉石传说中的两个随从相互攻击的结果,如果攻击值为0,则无法攻击,输出Invalid,如果攻击后生命值小于等于0,则输出Discard,如果都不是则输出两个随从的攻击和生命就好了

t = int(input())
for _ in range(t):
    a1,h1,a2,h2 = map(int,input().split())
    if a1==0:
        print("Invalid")
        continue
    h1 = h1-a2
    h2 = h2-a1
    res1 =f"{a1} {h1}"
    res2 = f"{a2} {h2}"
    if h1<=0:
        res1="Discard"
    if h2<=0:
        res2="Discard"
    print(res1,res2)

Ace of Aces

要找到得票最多的人,如果有两个及以上得票最多的人就输出Nobody

t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    vote ={}
    for id in a:
        if id in vote:
            vote[id]+=1
        else:
            vote[id] = 1
    mymax =-1
    for id,v in vote.items():
        if v>mymax:
            win=[id]
            mymax=v
        elif v==mymax:
            win.append(id)
    if len(win) == 1:
        print(win[0])
    else:
        print("Nobody")

Lunch Time

在开胃菜,主菜和甜点中各选一样,每类食物可能有多种,给出名字和价格,选择的标准是选价格在中间的那一样,如果是偶数种,选择两者中价格贵的那一种,最后输出总价格和选择每道菜的名字

t = int(input())
for _ in range(t):
    s,m,d = map(int,input().split())
    slist = []
    for _ in range(s):
        name,price = input().split()
        slist.append((int(price),name))
    slist.sort()

    mlist = []
    for _ in range(m):
        name, price = input().split()
        mlist.append((int(price), name))
    mlist.sort()

    dlist = []
    for _ in range(d):
        name, price = input().split()
        dlist.append((int(price), name))
    dlist.sort()

    median_s = slist[s // 2]
    median_m = mlist[m // 2]
    median_d = dlist[d // 2]
    sum = median_s[0]+median_m[0]+median_d[0]
    print(sum,median_s[1],median_m[1],median_d[1])

Demacia of the Ancients

判断一个序列中超过6000的个数

t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    res = 0
    for num in a:
        if num>6000:
            res+=1
    print(res)

第28页

12,14,67,76,78,87

Hard to Play

总得分的计算方式为 P = Point * (Combo * 2 + 1),要求出可能的最低分和最高分。比较容易想到最低分Point顺序是300,100,50,最高分则相反。

t = int(input())
for _ in range(t):
    a,b,c = map(int,input().split())
    combo = 0
    mymin = 0
    for i in range(a):
        mymin+=300*(combo*2+1)
        combo+=1
    for i in range(b):
        mymin+=100*(combo*2+1)
        combo+=1
    for i in range(c):
        mymin+=50*(combo*2+1)
        combo+=1
    combo = 0
    mymax = 0

    for i in range(c):
        mymax+=50*(combo*2+1)
        combo+=1
    for i in range(b):
        mymax+=100*(combo*2+1)
        combo+=1
    for i in range(a):
        mymax+=300*(combo*2+1)
        combo+=1
    print(mymin,mymax)

Java Beans

已知有N个小孩围成一圈,每个小孩手中有一定数量的豆子,老师要选择M个连续座位上的小孩并收集他们的豆子,要求出M的最大值。你可以将数组扩展成两倍(即将原数组复制一份接在后面),这样就可以处理连续座位跨越 “圈头” 和 “圈尾” 的情况,而不用单独处理边界。然后计算每个长度为M的连续区间内的豆子总数,找出其中的最大值

t = int(input())
for _ in range(t):
    n,m = map(int,input().split())
    c = list(map(int,input().split()))
    c = c+c
    mymax = 0
    for i in range(n):
        mymax =  max(mymax,sum(c[i:i+m]))
    print(mymax)

Elevator

判断电梯是否超重,把每个人体重数量求和之后判断即可

T = int(input())
for _ in range(T):
    N, M = map(int, input().split())
    weights = list(map(int, input().split()))
    total_weight = sum(weights)
    if total_weight <= M:
        print("Safe")
    else:
        print("Warning")

Pokemon Master

判断那边总宝可梦更重

T = int(input())
for _ in range(T):
    N, M = map(int, input().split())
    calem = list(map(int, input().split()))
    calem_total = sum(calem)
    serena = list(map(int, input().split()))
    serena_total_weight = sum(serena)
    if calem_total > serena_total_weight:
        print("Calem")
    elif serena_total_weight > calem_total:
        print("Serena")
    else:
        print("Draw")

Talented Chef

这道题的核心是计算厨师准备所有菜肴所需的最少时间。已知厨师能同时处理 M 道菜肴,每处理一次能让这 M 道菜肴各完成一个步骤。要准备 N 道菜肴,每道菜肴有不同的步骤数。我们需要找出准备好所有菜肴的最少用时。

首先我们要让将所有菜肴的步骤数相加,这个总步骤数反映了完成所有菜肴总共需要完成的操作量。例如,如果有 3 道菜,步骤数分别为 2、3、4,那么总步骤数就是 2 + 3 + 4 = 9。假如M=4,那我们只需要3分钟时间。

但是我们还要在所有菜肴的步骤数中找出最大值。这个最大值很重要,因为即使其他菜肴都完成了,步骤数最多的那道菜还需要继续处理,直到完成。比如上述例子中,最大步骤数就是 4。

最后就是两者取最大值,3和4取最大值就是4

import math
t = int(input())
for _ in range(t):
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    total_steps = sum(a)
    max_steps = max(a)
    result = max(math.ceil(total_steps/m), max_steps)
    print(result)

Access System

我们可以对学生的到达时间进行排序,排序之后依次判断需要刷门禁的学生并记录,最后输出

def mytime(time_str):
    h, m, s = map(int, time_str.split(":"))
    return h * 3600 + m * 60 + s


t = int(input())
for _ in range(t):
    n, l = map(int, input().split())
    arrival_times = []
    for i in range(n):
        time_str = input()
        sec = mytime(time_str)
        arrival_times.append((sec, i + 1))
    arrival_times.sort()
    ans = []
    unlock_time = -1
    for sec, index in arrival_times:
        if sec >= unlock_time:
            ans.append(index)
            unlock_time = sec + l
    ans.sort()
    print(len(ans))
    print(" ".join(map(str, ans)))
posted @ 2025-02-18 11:33  傻傻的小小豪  阅读(100)  评论(0)    收藏  举报