bzoj#P3301. [USACO2011 Feb] Cow Line
[USACO2011 Feb] Cow Line
Description
The cows conveniently numbered are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Farmer John what their line number is. In return, Farmer John can give them a line number and the cows must rearrange themselves into that line.
A line number is assigned by numbering all the permutations of the line in lexicographic order.
Consider this example:
Farmer John has cows and gives them the line number of . The permutations of the line in ascending lexicographic order:
st:
nd:
rd:
Therefore, the cows will line themselves in the cow line .
The cows, in return, line themselves in the configuration and ask Farmer John what their line number is.
Continuing with the list:
th:
th:
Farmer John can see the answer here is .
Farmer John and the cows would like your help to play their game.
They have queries that they need help with.
Query has two parts: will be the command, which is either P
or Q
.
-
If is
P
, then the second part of the query will be one integer , which is a line number. This is Farmer John challenging the cows to line up in the correct cow line. -
If C_i is
Q
, then the second part of the query will be distinct integers . This will denote a cow line. These are the cows challenging Farmer John to find their line number.
Input
Line : Two space-separated integers: and .
Lines : Line and will contain a single query.
Line will contain just one character: Q
if the cows are lining up and asking Farmer John for their line number or P
if Farmer John gives the cows a line number.
If the line is Q
, then line will contain space-separated integers which represent the cow line. If the line is P
, then line will contain a single integer which is the line number to solve for.
Output
Lines : Line will contain the answer to query .
If line of the input was Q
, then this line will contain a single integer, which is the line number of the cow line in line .
If line of the input was P
, then this line will contain space separated integers giving the cow line of the number in line .
5 2
P
3
Q
1 2 5 3 4
1 2 4 3 5
5
Data scale and Agreement
For data, , , , .
Source
Silver