Marching Orders
Time limit1sMemory limit2048 MB
Given n and a proposed order of professors, decide whether some m below 10^9 produces that order under the removal method, and print the smallest such m.
- Level
Medium6 of 10
- Topics
- Number theory, Backtracking, Simulation
- Solved
- No attempts yet
Problem
Dean Bob Roberts is in charge of the order in which the professors of his college march in the graduation ceremonies. After complaints from some professors in the newly created DEI Studies Department about seniority-based ordering, the dean decided the marching order should be random. Bob thinks this is fine. To be transparent, he has published the method he uses to build the marching list.
He starts with an alphabetically ordered list of professors numbered and a non-negative integer . The first person in the marching list is the one at position in the alphabetic list. That person is removed, which shortens the list by one and shifts down everyone at positions greater than . The second person is the one at position , and so on.
For example, assume there are six professors A, B, C, D, E and F. If , the marching list is created as follows:
The method sounds fair, but it is hard to verify. Dean Roberts does not publish the value of he uses, so the faculty cannot tell whether he follows the stated method or picked the order by personal preference. The faculty wants to know whether some value of produces a given marching order.
Input
The first line contains a single decimal integer (), the number of professors who will march. The second line contains a string, a permutation of the first uppercase letters of the alphabet, giving the proposed marching order.
Output
If the given marching order could not have come from the algorithm, print a single line containing NO. Otherwise, print two lines. The first line contains YES. The second line contains the smallest non-negative value of that produces the given marching order.