郑州大学“战疫杯”大学生程序设计在线邀请赛(4)题解(Python版)

郑州大学“战疫杯”大学生程序设计在线邀请赛(4)题解(Python版),第1张

“战疫杯”大学生程序设计在线邀请赛(4)题解

由于今天晚上线下上机房上课,导致第三题做题的时候环境一直很乱,好长时间都浪费了。

但是好像也能水进前五十hh

1. 时空密接

由于读错题,wa了一发,我当时理解的是只要B去过A经过的地方都算密集。

实际题意是A,B需要同时同地才算时空密接,那么我们模拟一下A,B的位置即可

x1,y1,x2,y2,t = map(int,input().split())
nums1 = [[x1,y1]]
s1 = input()
s2 = input()
flags = False
for i in range(len(s1)):
    if (x2,y2) == (x1,y1):
        flags = True
        break
    if s1[i] == 'D':
        x1 += 1
    elif s1[i] == 'W':
        y1 += 1
    elif s1[i] == 'A':
        x1 -= 1
    elif s1[i] == 'S':
        y1 -= 1
    if s2[i] == 'D':
        x2 += 1
    elif s2[i] == 'W':
        y2 += 1
    elif s2[i] == 'A':
        x2 -= 1
    elif s2[i] == 'S':
        y2 -= 1
    
if flags:
    print("yes")
else:
    print("no")
2. 今晚吃鸡

小的模拟题,按照题意模拟即可

当击中胸背部时,会造成30点伤害,当击中四肢时,会造成10点伤害。当血量小于等于0时,游戏结束,血量不为0的一方获胜

如果防具当前耐久为x,收到的攻击为y,当x>y时,防具的耐久变为xy,不会破碎。当xy时,防具会破碎,且此次攻击不再扣除玩家血量。

这句话很关键,然后下面随便写模拟的过程即可

a1,b1,c1 = map(int,input().split())
a2,b2,c2 = map(int,input().split())
n = int(input())
for i in range(n):
    s = input()
    roles,bw = s.split()
    if roles == "777777777Plus":
        if bw == "head":
            if c1 > 0:
                c1 = 0
            else:
                a1 = 0
        elif bw == "body":
            if b1 > 0:
                b1 -= 30
            else:
                a1 -= 30
        elif bw == "limbs":
            a1 -= 10
    else:
        if bw == "head":
            if c2 > 0:
                c2 = 0
            else:
                a2 = 0
        elif bw == "body":
            if b2 > 0:
                b2 -= 30
            else:
                a2 -= 30
        elif bw == "limbs":
            a2 -= 10
if a1 > 0:
    print("777777777Plus")
else:
    print("SW2000")

 

经过大佬提点,这题不用模拟,因为题目保证数据保证不存在玩家血量小于等于0后继续攻击的情况。

所以最后一个受攻击的是必败态

a1,b1,c1 = map(int,input().split())
a2,b2,c2 = map(int,input().split())
n = int(input())
for i in range(n):
    s = input()
    roles,bw = s.split()
    if  i == n-1:
        if roles == "777777777Plus":
            print("777777777Plus")
        else:
            print("SW2000")
3. 核酸排队

用t1,t2两个数组递推模拟排队时间的过程

第i项012
t1579
t281114
nums356
res568

注意事项:数组开大一点,因为开成10010,wa了一发以及输出格式需要注意行末不能有空格,也wa了一次

n = int(input())
x,y = map(int,input().split())
nums = list(map(int,input().split()))
res = []
t1,t2 = [0] * 100010, [0] * 100010
t1[0] = nums[0] + x
t2[0] = nums[0] + x + y
res.append(x + y)
for i in range(1,len(nums)):
    # 上次同学扫完码之后的时间 + x ,当前同学来之后 加上 x的时间 
    t1[i] = max(t1[i-1] + x,  nums[i] + x)
    # 当前同学来之后 加上 x + y的时间,上次同学做完的时间 + y, 上个同学扫完码之后的时间 + y
    t2[i] = max(nums[i] + x + y , t2[i-1] + y,t1[i] + y)
    res.append(t2[i] - nums[i])
for i in range(len(res)):
    if i != len(res) - 1:
        print(res[i],end=" ")
    else:
        print(res[i],end="")

滚动数组优化:

n = int(input())
x,y = map(int,input().split())
nums = list(map(int,input().split()))
res = []
t1,t2 = 0, 0
for i in range(len(nums)):
    t1 = max(t1 + x,  nums[i] + x)
    t2 = max(nums[i] + x + y , t2 + y,t1 + y)
    res.append(t2 - nums[i])
for i in range(len(res)):
    if i != len(res) - 1:
        print(res[i],end=" ")
    else:
        print(res[i],end="")

最后附加一手C++最简洁代码,不用开任何额外数组,19行代码

时间复杂度O(N),空间复杂度O(1)

#include 
typedef long long LL;
int n;
// res 更新结果,x,y对应时间 a 每次输入的ai
LL res,x,y,a;
using namespace std;
int main(){
   cin >> n >> x >> y;
   LL t1,t2;
   t1 = 0,t2 = 0;
   for(int i = 0;i < n ;i ++){
       cin >> a;
       t1 = max(t1 + x,a + x);
       t2 = max(max(a + x + y,t2 + y) , t1 + y);
       res = t2 - a;
       if(i != n-1) printf("%lld ",res);
       else  printf("%lld",res);
   }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/943708.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存