输入包含多组测试数据。
对于每组测试数据:
N - 本组测试数据有n个数
a1,a2...an - 需要计算的数据
保证:
1<=N<=100000,0<=ai<=INT_MAX.
对于每组数据,输出两个数,第一个数表示差最小的对数,第二个数表示差最大的对数。
6 45 12 45 32 5 6
1 2
import sys for line in sys.stdin: temp = [int(i) for i in line.split()] if len(temp) == 1: # 把N跳过 continue temp.sort() Dict = {} for i in temp: if i in Dict: Dict[i] += 1 else: Dict[i] = 1 res = 0 for k in Dict.keys(): if Dict[k] >= 2: temp2 = [i for i in range(Dict[k])] res += sum(temp2) if res == 0: # 没重复的情况,比如[1,2,3,9]这种 temp3 = [] for j in range(len(temp)-1): temp3.append(temp[j+1] - temp[j]) temp3.sort() # print()会换行,算例通不过,加了end就不会换行 print(temp3.count(temp3[0]), end=" ") else: print(res, end=" ") num_max, num_min = Dict[temp[-1]], Dict[temp[0]] print(num_max*num_min)我用python3,各位一定要注意print()会直接换行,算例通不过
import sys def MaxAndMinPair(s): if len(s) <= 1: return 0 s.sort(reverse=True) if s[0] == s[-1]: minPair = maxPair = int(len(s) * (len(s) - 1) / 2) print(str(minPair)+' '+str(maxPair)) else: minGap = s[0] - s[-1] minPair = 1 for i in range(len(s)-1): if s[i] - s[i + 1] < minGap: minGap = s[i] - s[i + 1] if minGap == 0: break minPair = 1 continue if s[i] - s[i + 1] == minGap: minPair += 1 if minGap == 0: minPair = 0 elementDic = {} for i in range(len(s)): if s[i] not in elementDic: elementDic[s[i]] = 1 else: elementDic[s[i]] += 1 for key in elementDic: num = elementDic[key] minPair += num*(num-1)/2 # to find the max gap pair num # 2 1 1 1 maxNum = 1 minNum = 1 for i in range(len(s)-1): if s[i] == s[i+1]: maxNum += 1 else: break for i in range(len(s)-1): if s[len(s)-1-i] == s[len(s)-2-i]: minNum += 1 else: break print(str(int(minPair)) + ' ' + str(maxNum * minNum)) if __name__ == '__main__': k = 0 for line in sys.stdin: k = 1 - k if (k == 0): data = [int(i) for i in line.strip().split()] MaxAndMinPair(data)
input_num = []
while True:
try:
num = int(input().strip())
list_num = [int(i) for i in input().strip().split()]
input_num.append((num,list_num))
except EOFError:
break
def find(numOfList,list_num):
num = numOfList
if num == 1:
return 0,0
listNum = list_num
listNum.sort()
amount = 0
i = 0
while i < num-1: #i<num-1,
if listNum[i] == listNum[i+1]:
count = 1 #count记录有多少个相等的listNum[i]
while i < num-1 and listNum[i] == listNum[i+1]:
count += 1
i += 1
amount += count*(count-1)//2 #n个相同的数俩俩组合,会形成C![2](https://uploadfiles.nowcoder.com/files/20190308/2663869_1552046687233_equation?tex=C_%7Bn%7D%5E%7B2%7D "图片标题")
else:
i += 1
if amount != 0:
numOfsmall = amount
else:
i = 0
while i < num-1:
if listNum[i+1] - listNum[i] == 1:
amount += 1
i += 1 #i只是+1,而不是+2的原因在于任意数字俩俩组合
else:
i += 1
numOfsmall = amount
numOfmin = 1
i = 1
while i < num and listNum[i] == listNum[0]: #一定要注意范围
numOfmin += 1
i += 1
numOfmax = 1
i = num-2
while i >= 0 and listNum[i] == listNum[-1]: #一定要注意范围
numOfmax += 1
i -= 1
numOfbig = numOfmin*numOfmax
return numOfsmall,numOfbig
for i in input_num:
numOfsmall,numOfbig = find(i[0],i[1])
print(numOfsmall,numOfbig,sep=' ')
import sys def main(): k = 0 for line in sys.stdin: k = 1 - k if (k == 0): li = [int(i) for i in line.strip().split()] m = len(li) li.sort() small, big = li[0], li[m-1] smallnum, bignum, ansbig, anssmall, mincha, mincount = 1, 1, 0, 0, -1, 0 # answer big while (li[smallnum] == small): smallnum += 1 while (li[m-1-bignum] == big): bignum += 1 ansbig = smallnum * bignum #answer small for i in range(m-1): if (li[i+1] - li[i] < mincha or mincha < 0): mincha = li[i+1] - li[i] mincount = 1 elif (li[i+1] - li[i] == mincha): mincount += 1 if (mincha > 0): anssmall = mincount else: p = 0 for i in range(m-1): if (li[i+1] == li[i]): p += 1 else: if (p > 0): anssmall += p * (p + 1) / 2 p = 0 anssmall += p * (p + 1) / 2 print str(anssmall) + " " + str(ansbig) main()
def solve(n, arr): arr.sort() if arr[0] == arr[-1]: print (n*(n-1)/2), (n*(n-1)/2) return big = arr.count(arr[0]) * arr.count(arr[-1]) small_arr = [arr[i+1] - arr[i] for i in range(n-1)] small = small_arr.count(min(small_arr)) print small, big if __name__ == '__main__': while 1: try: n = input() arr = map(int, raw_input().strip().split()) if n == 1: print '1 1' continue solve(n, arr) except: break