Codeforces Round #446 (Div. 2)
阿新 • • 發佈:2017-11-18
temp 開始 codeforce isp 區間 one import port none
A. Greed
題意
問能否把所有可樂倒到兩個罐子裏
題解
挑兩個最大的罐子
1 sum = 0 2 b = [] 3 n = int(input()) 4 line1 = input().split() 5 line2 = input().split() 6 for i in range(n): 7 sum += int(line1[i]) 8 b.append(int(line2[i])) 9 continue 10 b.sort() 11 cap = b[n-1] + b[n-2] 12 if sum > cap: 13 printView Code(‘NO‘) 14 else: 15 print(‘YES‘)
B. Wrath
題意
每個人手裏都有一根長度為L[i]的EX咖喱棒, 只要鈴聲一響, 就把自己前面L[i]個人砍死(同時), 問最後活下來多少人
題解
把自己能砍死的最遠的人標記+1, 自己標記-1, 掃一遍標記數組, 是正數就是死人, 0就是活人
1 n = int(input()) 2 shit = [0 for i in range(n + 1)] 3 line = input().split() 4 for i in range(n): 5 claw = int(line[i])View Code6 shit[i] -= 1 7 pre = i - claw 8 if pre < 0: 9 pre = 0 10 shit[pre] += 1 11 continue 12 ans = n 13 tmp = 0 14 for i in range(n): 15 tmp += shit[i] 16 if tmp > 0: 17 ans -= 1 18 continue 19 print(ans)
C. Pride
題意
給定一個數組a, 有一種操作可以把相鄰的兩個數(x, y)中的一個(x或者y)替換為gcd(x, y), 問全部替換成1的最少操作, 不可能時輸出‘-1‘
題解
數組a內有1的時候, 顯然答案是非1的個數
否則, 只要找到最快生成一個1的操作次數
從a[i]開始, 計算a[i]到a[j]的gcd, (i < j < n), 當區間gcd為1時, 更新最小操作數為區間的長度, 即j - i + 1
1 import math 2 n = int(input()) 3 a = [] 4 k = 0 5 line = input().split() 6 for i in range(n): 7 a.append(int(line[i])) 8 if a[i] != 1: 9 k += 1 10 continue 11 ans = n 12 for i in range(n): 13 temp = a[i] 14 cur = 0 15 for j in range(i + 1, n): 16 cur += 1 17 temp = math.gcd(temp, a[j]) 18 if temp == 1: 19 if ans > cur: 20 ans = cur 21 pass 22 pass 23 continue 24 continue 25 if k == 0: 26 print(‘0‘) 27 elif ans == n: 28 print(‘-1‘) 29 else: 30 print(ans + k - 1)View Code
D. Gluttony
1 n = int(input()) 2 a = [int(i) for i in input().split()] 3 if n == 1: 4 print(a[0]) 5 else: 6 a_with_index = [[a[i], i] for i in range(n)] 7 a_with_index.sort() 8 ans = [0 for i in range(n)] 9 for i in range(n): 10 ans[a_with_index[i][1]] = str(a_with_index[(i + 1) % n][0]) 11 print(" ".join(ans))View Code
E.
Codeforces Round #446 (Div. 2)