我试图用codechef:https://www.codechef.com/OCT19B/problems/S10E来解决这个问题
厨师想买一部新手机,但他不愿意花很多钱。相反,他每天检查他选择的模型的价格,并等待价格降到一个可接受的值。到目前为止,他已经观察了N天的价格(从1到N);对于每一个有效的i,第一天的价格是Pi美元。
在每一天,厨师认为手机的价格是好的,如果它是严格地小于所有的价格,他已经观察到在前五天。如果过去5天中的某些日子没有价格记录(因为大厨还没有开始检查当天的价格),那么大厨就会忽略前一天的价格--我们可以说,他认为当天的价格是无限的。
现在,厨师想知道-多少天他认为这个价格是好的?找出这几天的数字。
这就是我所做的,在所有的测试用例中,这都给出了正确的结果(如我所述)。
#!/usr/bin/env python
# Number of test cases
T = int(input())
# for each test case do the following
for i in range(T):
N = int(input()) # number of days
P = list(map(int, input().split())) # list containing all the prices
G = 1 #number of good days. Day 1 is always a good day hence start with 1
# since price on a given day should be STRICTLY lesser than any of the
# previous 5 days, price on the day must be minimum of the 6 values
# (5 previous and today). Also the day's price must not be equal to any of
# the other values (STRICTLY)
for i in range(5,N):
if(min(P[i-5: i+1]) == P[i]) and (P[i] not in P[i-5: i]):
G = G + 1
print(G)但它仍未被接受为答案,我做错了什么?另外,任何关于解决这一问题的更好办法的意见都是非常欢迎的。
发布于 2019-10-06 20:44:16
1:for i in range(1,N):而不是for i in range(5,N):开始循环,因为从i=1到i=4的时间是很好的。为了避免负索引的问题,max函数作为第二个参数。所有适用的更改都会导致以下结果:
for i in range(1,N):
if(min(P[max(i-5, 0): i+1]) == P[i]) and (P[i] not in P[max(i-5, 0): i]):
G = G + 1发布于 2019-10-06 20:36:13
不确定这是否以“最好”的方式回答了您的问题,因为我对Python相当陌生,但是这里是我对您的问题的解答。
我们用一张列有>6个条目的列表。列表= 101,102,95,124,612,95,123,612现在我们想要获取列表中的最后六个条目并找到最小值。
def minList(P):
list2 = []
for i in range(6):
list2.append(P[-i])
return min(list2)这一产出为95。
理论上,尽管体积很大,但您可以将值发送到上面的函数中,以获得前6天的最小值。
https://stackoverflow.com/questions/58260809
复制相似问题