微软2017年预科生计划在线编程笔试第二场-#1499 : A Box of Coins

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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值