Greedy
Time limit2sMemory limit1024 MB
Split a digit string into the fewest substrings, each forming a number at most x, and report the minimum after each single-digit update.
- Level
Medium7 of 10
- Topics
- Greedy, Segment tree
- Solved
- No attempts yet
Statement
James Bond has always been saved by new gadgets designed by a whole team at MI-6. Few people wonder how the geniuses of that department spend their time.
Q is one of the people who supply Bond with new weapons and keep the MI-6 computer network secure. When Q gets bored, he solves programming olympiad problems. His method is somewhat unusual.
Q knows that in real-life problems, all input data is fairly random and uniformly distributed over the values it can take. He also knows that real problems often have a solution that is some kind of greedy algorithm.
When Q gets a new olympiad problem, he first writes a solution right after reading the statement. It gives correct answers for small parameters but does not fit the time limit. Then he generates many tests completely at random, gets their answers from that solution, and builds a greedy algorithm that passes these tests and runs in the required time. He submits the new solution to the server and gets Accepted.
Yesterday Petya solved the following problem.
You are given a string of digits from 1 to 9 and an integer . Split this string into the smallest possible number of substrings such that each substring, read as a decimal number , satisfies .
Q wrote a greedy that gave correct answers on his tests and ran in , where is the length of the string. When he finished reading the statement, he found an extra requirement. The problem also asks to process queries that change one digit of the string, and after each query the new answer must be printed. Q's greedy is too slow for these limits. He thought for five more minutes, wrote a faster greedy, and got Accepted.
Solve this same problem.
Input
The first line contains a string of digits from 1 to 9 of length .
The second line contains two integers and (, ), where is the number of single-digit change queries and is the bound that every substring must not exceed.
Each of the next lines contains two integers and (, ). The digit at position is changed to .
Output
Print the minimum number of substrings for the original string on the first line.
Then print the answer after each of the queries is applied, one answer per line.