New Year and Rainbow Roads (Codeforces 908F)
Tags: Implement, graphs

Roy and Biv have a set of n points on the infinite number line.Each point has one of 3 colors: red, green, or blue.Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects.They want to do this in such a way that they will both see that all the points are connected (either directly or indirectly).However, there is a catch: Roy cannot see the color red and Biv cannot see the color blue.Therefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected).Help them compute the minimum cost way to choose edges to satisfy the above constraints.

Input: The first line will contain an integer n (1 ≤ n ≤ 300 000), the number of points.The next n lines will contain two tokens pi and ci (pi is an integer, 1 ≤ pi ≤ 109, ci is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the i-th point and the color of the i-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order.

Output: Print a single integer, the minimum cost way to solve the problem.

Note: In the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively.

Sample Inputs:
4
1 G
5 R
10 B
15 G

4
1 G
2 R
3 B
10 G

Sample Outputs:
23

12

Solution (Python):
def F1():
    n = int(input())
    ans = 0
    allc = 0
    bigr = 0
    bigb = 0
    lastr = -1
    lastb = -1
    lastg = -(1 << 60)
    for i in range(n):
        x = input().split()
        (pos, col) = (int(x[0]), x[1])
        if col == 'R' or col == 'G':
            if lastr != -1:
                bigr = max(bigr, pos - lastr)
                allc += pos - lastr
            lastr = pos
        if col == 'B' or col == 'G':
            if lastb != -1:
                bigb = max(bigb, pos - lastb)
                allc += pos - lastb
            lastb = pos
        if col == 'G':
            ans += allc + min(0, -bigr - bigb + pos - lastg)
            lastr = lastb = lastg = pos
            bigr = bigb = allc = 0
    ans += allc
    print(ans)
if __name__ == '__main__':
    F1()

Solution (Python):
n = int(input())
(R, G, B) = (0, 0, 0)
(maxR, maxG, maxB) = (0, 0, 0)
Cost = 0
for i in range(n):
    temp_input = input().split(' ')
    value = int(temp_input[0])
    choice = temp_input[1]
    if choice == 'R' or choice == 'G':
        if R:
            Cost += value - R
            maxR = max(maxR, value - R)
        R = value
    if choice == 'B' or choice == 'G':
        if B:
            Cost += value - B
            maxB = max(maxB, value - B)
        B = value
    if choice == 'G':
        if G:
            Cost += min(0, value - G - maxR - maxB)
        G = value
        maxR = 0
        maxB = 0
print(Cost)

Solution (Python):
n = int(input())
r = 0
g = 0
b = 0
maxR = 0
maxB = 0
maxG = 0
minCost = 0
for i in range(n):
    (p, c) = input().split(' ')
    p = int(p)
    if c == 'R' or c == 'G':
        if r:
            minCost += p - r
            maxR = max(maxR, p - r)
        r = p
    if c == 'B' or c == 'G':
        if b:
            minCost += p - b
            maxB = max(maxB, p - b)
        b = p
    if c == 'G':
        if g:
            minCost += min(0, p - g - maxR - maxB)
        g = p
        maxR = 0
        maxB = 0
print(minCost)