我在用python解决facebook hackercup上的^{
那么,有没有可能用python不能在6分钟内计算出这个问题的解决方案呢?或者是我的方法太差了?
问题陈述:
发出笑脸后,约翰决定玩数组。你知道黑客喜欢玩数组吗?John有一个基于零的索引数组m
,它包含n
非负整数。然而,他只知道数组的第一个k
值,他想找出其余的值。
John知道以下几点:对于每个索引i
,其中k <= i < n
,m[i]
是最小的非负整数,它是m
的前一个*k*
值中包含的而不是。
例如,如果k = 3
、n = 4
和m
的已知值是[2, 3, 0]
,他可以计算出m[3] = 1
。
约翰正忙于使世界变得更加开放和联系起来,因此,他没有时间去了解阵列的其余部分。你的任务是帮助他。
给定m
的第一个k
值,计算该数组的第n个值。(即m[n - 1]
)。
由于n
和k
的值可能非常大,我们使用伪随机数生成器来计算m
的第一个k
值。给定正整数a
、b
、c
和r
,可以如下计算m
的已知值:
m[0] = a
m[i] = (b * m[i - 1] + c) % r, 0 < i < k
输入
第一行包含一个整数T(T<;=20),测试数 案例。
接下来是T测试用例,每个测试用例由2行组成。
每个测试用例的第一行包含两个空格分隔的整数,
n
,k
(1 <= k <= 10^5
,k < n <= 10^9
)。
每个测试用例的第二行包含4个空格分隔的整数
a
,b
,c
,r
(0<;=a,b,c<;=10^9,1<;=r<;=10^9)。
我尝试了两种方法,但都未能在6分钟内返回结果,下面是我的两种方法:
首先:
import sys
cases=sys.stdin.readlines()
def func(line1,line2):
n,k=map(int,line1.split())
a,b,c,r =map(int,line2.split())
m=[None]*n #initialize the list
m[0]=a
for i in xrange(1,k): #set the first k values using the formula
m[i]= (b * m[i - 1] + c) % r
#print m
for j in range(0,n-k): #now set the value of m[k], m[k+1],.. upto m[n-1]
temp=set(m[j:k+j]) # create a set from the K values relative to current index
i=-1 #start at 0, lowest +ve integer
while True:
i+=1
if i not in temp: #if that +ve integer is not present in temp
m[k+j]=i
break
return m[-1]
for ind,case in enumerate(xrange(1,len(cases),2)):
ans=func(cases[case],cases[case+1])
print "Case #{0}: {1}".format(ind+1,ans)
第二:
import sys
cases=sys.stdin.readlines()
def func(line1,line2):
n,k=map(int,line1.split())
a,b,c,r =map(int,line2.split())
m=[None]*n #initialize
m[0]=a
for i in xrange(1,k): #same as above
m[i]= (b * m[i - 1] + c) % r
#instead of generating a set in each iteration , I used a
# dictionary this time.
#Now, if the count of an item is 0 then it
#means the item is not present in the previous K items
#and can be added as the min value
temp={}
for x in m[0:k]:
temp[x]=temp.get(x,0)+1
i=-1
while True:
i+=1
if i not in temp:
m[k]=i #set the value of m[k]
break
for j in range(1,n-k): #now set the values of m[k+1] to m[n-1]
i=-1
temp[m[j-1]] -= 1 #decrement it's value, as it is now out of K items
temp[m[k+j-1]]=temp.get(m[k+j-1],0)+1 # new item added to the current K-1 items
while True:
i+=1
if i not in temp or temp[i]==0: #if i not found in dict or it's val is 0
m[k+j]=i
break
return m[-1]
for ind,case in enumerate(xrange(1,len(cases),2)):
ans=func(cases[case],cases[case+1])
print "Case #{0}: {1}".format(ind+1,ans)
第二种方法中的最后一个for循环也可以写成:
for j in range(1,n-k):
i=-1
temp[m[j-1]] -= 1
if temp[m[j-1]]==0:
temp.pop(m[j-1]) #same as above but pop the key this time
temp[m[k+j-1]]=temp.get(m[k+j-1],0)+1
while True:
i+=1
if i not in temp:
m[k+j]=i
break
样本输入:
5
97 39
34 37 656 97
186 75
68 16 539 186
137 49
48 17 461 137
98 59
6 30 524 98
46 18
7 11 9 46
输出:
Case #1: 8
Case #2: 38
Case #3: 41
Case #4: 40
Case #5: 12
我已经试过了,但是还没有人回答。
最多经过
k+1
步后,数组中的最后一个k+1
数字将是0...k
(按某种顺序)。随后,序列是可预测的:m[i] = m[i-k-1]
。因此,解决这个问题的方法是运行幼稚实现的k+1
步骤。然后得到一个包含2k+1
元素的数组(第一个k
是从随机序列生成的,另一个k+1
是从迭代生成的)。现在,最后的k+1元素将无限重复。所以您可以立即返回
m[n]
的结果:它是m[k + (n-k-1) % (k+1)]
。下面是一些实现它的代码。
这四个案例在我的机器上运行了4秒钟,最后一个案例可能是最大的
n
。我通过添加map提高了性能。
这是我的O(k)解决方案,它基于与上面相同的想法,但是运行得更快。
这里的关键点是,m[i]永远不会超过k,如果我们记住从0到p的前k个数字中的连续数,那么p永远不会减少。
如果m[i-k-1]大于p,那么显然我们应该将m[i]设为p+1,p至少会增加1。
如果数m[i-k-1]小于或等于p,则应考虑m[i-k:i]中是否存在相同的数,如果不存在,则m[i]应设置为m[i-k-1],如果存在,则应将m[i]设置为p+1,就像“m[i-k-1]-大于p”一样。
当p等于k时,循环开始,循环大小为(k+1),所以我们可以跳出计算,现在打印出答案。
相关问题 更多 >
编程相关推荐