from sys import stdin
import bisect
# 請參考 https://docs.python.org/3/library/bisect.html
n, q = map(int, stdin.readline().split())
f = sorted([int(x) for x in stdin.readline().split()])
# 先排序
# 例如 8 5 10 11 8 會排序成 5 8 8 10 11
# 考慮到會有重複的元素
# 使用 bisect.bisect_left 取左邊位置
# bisect.bisect_right 取右邊位置
for _ in range(q):
a, b = map(int, stdin.readline().split())
a = bisect.bisect_left(f, a)
b = bisect.bisect_right(f, b)
r = b - a
if(r): print(r)
else: print('The candies are too short')
2020/04/20
[zerojudge] a450: 棒棒糖比身高
2020/04/13
[zerojudge] a121: 質數又來囉
from sys import stdin
def isPrime(n):
'''
參考 http://www.csie.ntnu.edu.tw/~u91029/Prime.html
質數判定
'''
if(n == 1): return 0
# 傳進來的 n 都已經過濾過,底下這 3 行先 mark 起來
#if(n in (2, 3)): return 1
#if(n % 2 == 0): return 0
#if(n%6 not in [1,5]): return 0
u = n - 1
t = 0
while(u % 2 == 0):
u >>= 1
t += 1
sprp = [2, 7, 61]
for sk in sprp:
a = sk % n
if (a == 0 or a == 1 or a == n-1): continue
x = pow(a, u, n)
if (x == 1 or x == n-1): continue
for i in range(t-1):
x = pow(x, 2, n)
if (x == 1): return 0
if (x == n-1): break
if (x != n-1): return 0
return 1
for s in stdin:
a, b = map(int, s.split())
r = 0
k = a//6
v = 1
while(v):
for n in (k*6+1, k*6+5):
# 為了節省時間,只檢測 6p+1, 6p-1
# 其實除了 5 以外, 5 的倍數也不必測
if(n < a): continue
if(n > b): # 超過上界
v = 0
break
r += isPrime(n)
k += 1
if(a <= 2 and b >= 2): r += 1
if(a <= 3 and b >= 3): r += 1
# 上面的檢測會遺漏 2, 3 在這裡補
print(r)
2020/04/09
[zerojudge] d984: 棄保效應
from sys import stdin
op = 'ABC'
for s in stdin:
f = [[i, int(c)] for i, c in enumerate(s.split())]
f.sort(key = lambda z: z[1])
t = f.pop(0)
f[0][1] += t[1]
f.sort(key = lambda z: -z[1])
print(op[f[0][0]])
2020/04/08
[zerojudge] a091: 今晚打老虎
import sys, bisect
'''
你如果把每個值都塞到 list
list 太長造成每個 insert , sort , pop 都很耗時
可以用一個 dict 來記錄每個值出現的次數
'''
f = [] # 要維護的 list
d = {} # 記錄每個值出現的次數
for s in sys.stdin:
if(s == '2\n'): # Query MAX
p = f[-1]
print(p)
d[p] -= 1
if(d[p] == 0): f.pop()
'''
印出尾部最大值
計數減一
計數為零才 pop()
'''
elif(s == '3\n'): # Query MIN
p = f[0]
print(p)
d[p] -= 1
if(d[p] == 0): f.pop(0)
'''
印出頭部最小值
計數減一
計數為零才 pop(0)
'''
else: # Insert
t = int(s.replace('1 ',''))
if(d.__contains__(t)): d[t] += 1
else: d[t] = 1
'''
計數不存在時設為 1
反之加 1
'''
if(d[t] == 1):
bisect.insort_left(f, t)
'''
如為新成員才 insert
利用 bisect.insort_left = insert and sort
'''
2020/04/05
[zerojudge] e974: 2. 座位安排 (Seats)
from sys import stdin
from collections import deque
r, c, n = map(int, stdin.readline().split())
h = deque([deque([x for x in range(i,i+c)]) for i in range(1, r*c+1, c)])
n -= 1
odd = even = n//2
even += n%2
h.rotate(odd % r)
for u in h:
u.rotate(even % c)
u = list(map(str, u))
print(' '.join(u))
訂閱:
文章 (Atom)