http://hihocoder.com/problemset/problem/1499
题意理解:可以左右上下移动硬币,其实左右看作往右,然后可以移动负数个硬币。上下特殊处理,其实就是考虑最靠左的一列,如果上下都多,那么肯定往后移动,上下都少,肯定均需要往右移动负数,上下有多和少,内部先自己解决,然后再往右,这样的策略一定是最好的。然后处理完第一列,再处理第二列,贪心的做。
急转弯:只有一行的很简单,两行的按照一行的贪心,然后内部先处理原则就好了。注意细节,别累加答案累加错了
算法:无
数据结构:无
from __future__ import print_function
#
#
'test for python'
__author__ = 'hjkruclion'
import os
import sys
import math
def read_int():
"""Read a seris of numbers."""
return list(map(int, sys.stdin.readline().split()))
N = read_int()[0]
a = []
b = []
s = 0
for i in range(N):
x, y = read_int()
a.append(x)
b.append(y)
s = s + x + y
s = s // (2 * N)
res = 0
now = [0, 0]
for i in range(N):
now[0] = now[0] + a[i] - s
now[1] = now[1] + b[i] - s
if now[0] >= 0 and now[1] >= 0:
res = res + now[0] + now[1]
elif now[0] < 0 and now[1] < 0:
res = res + (-now[0]) + (-now[1])
elif now[0] >= 0 and now[1] < 0:
x = now[0]
y = -now[1]
if x >= y:
res += x #!! important
now[0] -= y
now[1] = 0
else:
res += y
now[0] = 0
now[1] += x
else:
x = -now[0]
y = now[1]
if y >= x:
res += y
now[0] = 0
now[1] -= x
else:
res += x
now[0] += y
now[1] = 0
print(res)