Red and Blue Dots 2
Time limit1sMemory limit1024 MB
Given N points on a circle, each red or blue, find the fewest crossing pairs among same-color chords that touch every point at least once.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Geometry, Greedy
- Solved
- No attempts yet
Problem
Points on a circle are placed at equal intervals, points in total. Each point is red or blue, and there are at least two red points and at least two blue points. Connect the points with chords so that every point is joined to at least one chord. Each chord must join two points of the same color.
The goal is to minimize the number of pairs of chords that cross at a point that is not an endpoint of either chord. Given the colors of the points in clockwise order, output the minimum number of crossing pairs over all valid ways to connect every point to at least one chord.
Input
The first line contains the number of test cases . Then test cases follow, one after another ().
The first line of each test case contains an integer ().
The next line contains a string of length . The -th character of the string is the color of the point that is steps clockwise from a point chosen arbitrarily as the reference. R means a red point and B means a blue point. The number of R and the number of B are each guaranteed to be at least 2.
The sum of over all test cases does not exceed .
Output
For each test case, print a line "Case #", where is the test case number. On the next line, print the minimum number of pairs of chords that cross.