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, sr, sc; cin >> r; cin >> c; int p[r][c]; bool v

CCC '24 J4 - Troublesome Keys

#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> #include <cmath> using namespace std; int main() { string ps; string ds; cin >> ps; cin >> ds;

CCC '22 J5 - Square Pool

#include<iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; bool rowcom(pair<int, int> a, pair<int, int> b){ return a.first < b.first; } bool colcom(pair<int,

留言


bottom of page