Elections
Time limit2sMemory limit256 MB
For each range [L, R] of a C/T vote string, find the fewest votes to nullify so Cap is never behind Tony in either forward or backward counting.
- Level
Medium7 of 10
- Topics
- String, Prefix sum, Greedy
- Solved
- No attempts yet
Problem
Today we must decide which superhero team is the best: Team Cap or Team Iron Man (Tony). There are enraged fans, and each one must vote for a favorite team: 'C' (Cap) or 'T' (Tony).
Cap has no chance of winning, and he is an honest man, so he decides to commit election fraud. He wants to nullify the minimum number of votes.
The votes are counted twice:
- In increasing order of fan index, skipping nullified votes.
- In decreasing order of fan index, skipping nullified votes.
Cap is happy if he is never behind Tony at any point during the counting. A tie is fine, but he must never have fewer votes.
No one can interfere with Tony. He knows Cap's plan, and for scenarios he wants to know how many votes Cap will nullify. Each scenario is given by and , and only the fans with indices from to (inclusive) take part in the vote.
Input
The first line contains , the number of fans. The second line contains a string of characters, each 'C' or 'T', giving the vote of each fan. The third line contains , the number of scenarios. Each of the next lines contains two integers and ().
Output
Print lines. The -th line contains the answer for the -th scenario.
Hints
In the first scenario, the votes are CCCTTTTTTCC. The only solution is to nullify 4 of Tony's votes.
In the second scenario, the votes are TTTTTT. The only solution is to nullify all votes.
In the third scenario, the votes are CCCTTT. The only solution is to nullify all of Tony's votes.