top of page

CCC 2016 J5 and S2: Tandem Bicycle

This is a Greedy Algorithm.

Because if the members of a pair have speeds a and b, then the bike speed of the pair is max(a, b), then sorted the two teams speed list.

For the minimum total speed, let the bigger speed member of the two teams make a pair, and sum all pairs speed, this is the answer.

For the maximum total speed, let one bigger speed member and one smaller speed member of the two teams make a pair, and sum all pairs speed, this is the answer.


cmd = int(input())
N = int(input())
list1 = []
list2 = []

S1 = input().rstrip().split(" ")
S2 = input().rstrip().split(" ")

for i in range(N):
    list1.append(int(S1[i]))
    list2.append(int(S2[i]))

list1.sort()
list2.sort()
sum = 0
if cmd == 1:
    for i in range(N):
        sum += max(list1[i], list2[i])
else:
    for i in range(N):
        sum += max(list1[i], list2[N-i-1])
print(sum)


Recent Posts

See All

Comments


bottom of page