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

CCC '24 J5 - Harvest Waterloo

#include<iostream> #include <vector> #include <algorithm> #include <cmath> #include <stack> using namespace std; int main() { int r, c,...

CCC '24 J4 - Troublesome Keys

s1 = input() s2 = input() silly = '' silly_o = '' quiete = '-' i = 0 j = 0 while i < len(s1) and j < len(s2): if s1[i] != s2[j]: if...

CCC '22 J5 - Square Pool

#include<iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; bool rowcom(pair<int, int> a, pair<int,...

Comentários


One-time Donations
I hope I can solve all CCC problems!,
Please e-transfer cccottawa862008@gmail.com

Subscribe to AIOICode newsletter

I'm a title. ​Click here to edit me.

Thanks for submitting!

  • Twitter
  • Facebook
  • Linkedin

© 2023 by AIOICode.

bottom of page