Skip to content

Instantly share code, notes, and snippets.

@kusti8
Created January 30, 2018 20:37
Show Gist options
  • Save kusti8/ab682cfe5c1bac1b7051ab6c025523dd to your computer and use it in GitHub Desktop.
Save kusti8/ab682cfe5c1bac1b7051ab6c025523dd to your computer and use it in GitHub Desktop.
USACO December Silver
def maxelements(seq):
m = max(seq)
return [i for i, j in enumerate(seq) if j == m]
f = open('homework.in', 'r')
num = int(f.readline().strip())
nums = f.readline().strip().split(' ')
nums = [int(x) for x in nums]
f.close()
avgs = [0]*num
s = 0
min_so_far = 1000000
for i in range(num-2, -1, -1):
s += nums[i]
if nums[i] < min_so_far:
min_so_far = nums[i]
if num-i > 2:
avgs[i] = (s-min_so_far)/(num-i)
else:
avgs[i] = s/(num-i+1)
print(avgs)
out = '\n'.join(map(str, maxelements(avgs)))
f = open('homework.out', 'w')
f.write(out)
f.close()
import heapq
f = open('measurement.in', 'r')
i = f.readline().strip().split(' ')
num = int(i[0])
initial = int(i[1])
journal = {}
leaderboard = {}
highest_prev = []
for i in range(num):
line = f.readline().split(' ')
journal[int(line[0])] = [line[1], int(line[2])]
leaderboard[line[1]] = initial
f.close()
def highest(leaderboard):
out = []
highest = heapq.nlargest(1, leaderboard.values())
return [k for k, v in leaderboard.items() if v == highest[0]]
highest_prev = highest(leaderboard)
counter = 0
for key in sorted(journal):
leaderboard[journal[key][0]] += journal[key][1]
if journal[key][0] not in highest_prev and leaderboard[journal[key][0]] < leaderboard[highest_prev[0]]: # Not changing high person and the change isn't less than the highscore
continue
if journal[key][0] not in highest_prev and leaderboard[journal[key][0]] > leaderboard[highest_prev[0]]: # Not changing high person and the change is more than highscore
counter += 1
highest_prev = [journal[key][0]]
continue
if set(highest_prev) != set(highest(leaderboard)):
counter += 1
highest_prev = highest(leaderboard)
f = open('measurement.out', 'w')
f.write(str(counter))
import time
f = open('shuffle.in', 'r')
num = int(f.readline())
ns = f.readline().split(' ')
ns = [int(x) for x in ns]
ids = [[i] for i in range(num)]
f.close()
from collections import defaultdict
pair_locs = defaultdict(list)
for i,item in enumerate(ns):
pair_locs[item].append(i)
pair_locs = {k:v for k,v in pair_locs.items() if len(v)>1}
tot_top = 0
tot_bot = 0
while True:
temp = [[] for _ in range(num)]
start = time.time()
for index, i in enumerate(ns):
temp[i-1] = temp[i-1] + ids[index]
tot_top += time.time()-start
ids = temp
tot = 0
start = time.time()
for k, v in pair_locs.items():
i = 0
for x in v:
if ids[x] != []:
i += 1
if i > 1:
break
if i <= 1:
tot += 1
else:
break
if tot == len(pair_locs):
break
tot_bot += time.time()-start
print(tot_top, tot_bot)
ans = 0
for a in ids:
if a != []:
ans += 1
f = open('shuffle.out', 'w')
f.write(str(ans))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment