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
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
相关文章推荐
- Review of codeforces 493B Vasya and Wrestling based on Python
- Review of Codeforces 5C. Longest Regular Bracket Sequence
- Review of Codeforces 6B and 6C
- Codeforces Round #399 E Game of Stones 博弈
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Round #399:E. Game of Stones
- Educational Codeforces Round 4 D. The Union of k-Segments 排序
- codeforces D. The Union of k-Segments 排序
- Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!
- Codeforces Round #437 (Div. 2 C. Ordering Pizza 贪心 only two types of pizza
- codeforces Gym 100187A A. Potion of Immortality
- Codeforces Round #310 (Div. 2) B. Case of Fake Numbers
- 【Educational Codeforces Round 41 (Rated for Div. 2) D】Pair Of Lines
- Codeforces Round #342 (Div. 2) B. War of the Corporations 贪心
- Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree(并查集)
- 【Codeforces Round 354 (Div 2)B】【简单模拟】Pyramid of Glasses
- Codeforces Round #399 E Game of Stones 博弈
- Educational Codeforces Round 4-D. The Union of k-Segments
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Gym 101158 F. Three Kingdoms of Bourdelot