# 腾讯2020届实习编程题2——Wine trading in Gergovia


题目描述

小Q所在的城镇里有一条笔直的公路,上面分布n个村庄,编号1到n。有些村庄需要购进水果,有些村庄需要贩卖水果,设第i个村庄对水果的需求量为 A i A_i Ai A i > 0 A_i>0 Ai>0表示该村需要贩卖水果, A i < 0 A_i<0 Ai<0表示该村需要购进水果,所有村庄供需平衡,即所有 A i A_i Ai之和为0。把k个单位的水果从一个村庄运到相邻村庄需要k块钱的运费,请你帮小Q计算一下,最少需要多少运费就可以满足所有村庄对水果的需求。

解题思路

思路一:由于在运输过程中必须要经过相邻村庄,如果村庄1需要 A 1 A_1 A1,则一定是通过村庄2提供的,先算上从2到1的运费,则问题就转化为村庄2的需求量为 A 2 A_2 A2和后面的村庄,以此类推,相应的代码如下:

def func(num_villages, list_demand):
    m = 0
    cost = 0
    if num_villages == 0:
        return 0
    for i in range(num_villages):
        # if not -1000 <= list_demand[i] <= 1000:
        #     return 0
        m += list_demand[i]
        cost += abs(m)
    return cost

if __name__ == '__main__':
    num_villages = int(input())
    list_demand = map(int, input().split())
    list_demand = list(list_demand)
    cost = func(num_villages, list_demand)
    print(cost)

思路二:标记出“提供”村庄和“需求”村庄,从左到右扫描村庄,譬如对于第1个村庄需求量为 A 1 > 0 A_1>0 A1>0,则由近即提供给"需求"村庄,反之亦然,相应的代码如下:

"""
The second programming problem in Tencent test.
Similar to the "wine trading in Gergovia": https://blog.sengxian.com/solutions/uva-11054
"""

def func(num_villages, list_demand, neg, pos):
    n, p = 0, 0
    cost = 0
    if num_villages == 0:
        return 0
    for i in range(num_villages):
        # if not -1000 <= list_demand[i] <= 1000:
        #     return 0
        if list_demand[i] > 0:   # 第i个村庄为正,则由近即远找负的村庄
            while list_demand[i] > 0:
                while list_demand[neg[n]] == 0:
                    n += 1
                if list_demand[i] >= abs(list_demand[neg[n]]):
                    cost += abs(i - neg[n])*abs(list_demand[neg[n]])
                    list_demand[i] += list_demand[neg[n]]
                    list_demand[neg[n]] = 0
                else:
                    cost += abs(i - neg[n]) * list_demand[i]
                    list_demand[neg[n]] += list_demand[i]
                    list_demand[i] = 0
        elif list_demand[i] < 0:      # 第i个村庄为负,则由近即远找正的村庄
            while abs(list_demand[i]) > 0:
                while list_demand[pos[p]] == 0:
                    p += 1
                if abs(list_demand[i]) <= list_demand[pos[p]]:
                    cost += abs(i - pos[p]) * abs(list_demand[i])
                    list_demand[pos[p]] += list_demand[i]
                    list_demand[i] = 0
                else:
                    cost += abs(i - pos[p]) * list_demand[pos[p]]
                    list_demand[i] += list_demand[pos[p]]
                    list_demand[pos[p]] = 0

        elif list_demand[i] == 0:
            continue


    return cost

if __name__ == '__main__':
    num_villages = int(input())
    list_demand = map(int, input().split())
    list_demand = list(list_demand)
    neg, pos = [], []
    for i in range(num_villages):
        if list_demand[i] > 0:
            pos.append(i)
        else:
            neg.append(i)
    cost = func(num_villages, list_demand, neg, pos)
    print(cost)

测试案例有:

10
100 200 300 400 500 -500 -400 -300 -200 -100
10
100 200 300 400 500 -100 -200 -300 -400 -500
10
100 -100 200 -200 300 -300 400 -400 500 -500
3
-10 20 -10
10
100 200 300 400 500 600 700 800 900 -4500
10
-2100 500 400 300 200 500 800 400 600 -1600
15
-1 -2 -3 -4 5 -6 7 -8 9 -10 11 -12 13 -14 15
0

相应的结果为:

5500
7500
1500
20
16500
9900
100

你可能感兴趣的:(笔试编程)