您的位置:首页 > 编程语言 > Python开发

Review of codeforces 492C Vanya and Computer Game based on Python

2014-12-07 11:39 423 查看
492C is relatively easy. But we should not ignore the exceed time limit problem. 

I use heap to complete this task. pop out a element each time. We should notice that it is a bad idea to increase the sum every time one by one, which may result in time limit exceed. We should split it as two problems. 

The source code is as follows:

import heapq
n, r, avg = map(int, raw_input().split())
h = []
h1 = []
for i in xrange(n):
temp = list(map(int, raw_input().split()))
if r - temp[0] > 0:
heapq.heappush(h, [temp[1], r - temp[0]])
h1.append(temp[0])
h.sort()
h.reverse()
s = sum(h1)
x = n * avg - s
ans = 0

while x > 0 :
temp = h.pop()
if temp[1] > x :
ans += temp[0] * x
x = 0

else :
x -= temp[1]
ans += temp[0] * temp[1]
print ans
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息