#P3064. Cow Gang War
Cow Gang War
Cow Gang War
The cows on a farm have divided themselves into m gangs (numbered 1 through m) and now are fighting over a prized grazing field. At each minute one cow arrives on the field. The rules are as follows:
- If the field is empty, the arriving cow takes control for her gang and starts grazing (so the field now contains one cow of that gang).
- If the field is controlled by the arriving cow's gang, she simply joins the others grazing.
- If the field is controlled by a different gang then the arriving cow confronts one of the cows already grazing. As a result, both the incoming cow and one cow of the controlling gang leave the field to get a drink (i.e. they are removed from the process). If after this confrontation the field is empty then no gang controls it.
Bessie is a member of gang 1 and she wants her gang to control the field when all cows have arrived. Furthermore, she wants as many of her gang’s cows as possible to be grazing on the field at the end. If it is possible for gang 1 to control the field, output the maximum number of gang 1 cows that can remain on the field together and the lexicographically smallest ordering of cow arrivals that yields that maximum. (An ordering X
is lexicographically smaller than Y
if for some index k we have X[k] < Y[k]
and for all indices i with i < k, X[i] = Y[i]
.) If it is not possible for gang 1 to control the field at the end, output NO
.
Note on the simulation: The state of the field is defined by two values: (i) the gang currently controlling it (if the field is nonempty) and (ii) the number of cows grazing. The simulation works as follows. Suppose the field is empty, or controlled by gang g with c cows. When a cow of gang i arrives:
- If the field is empty, then it becomes controlled by gang i with count 1.
- If i = g, the cow simply joins and the count increases by 1.
- If i ≠ g, a confrontation occurs. In a confrontation, the arriving cow and one cow of the controlling gang (i.e. one of the c cows) are both removed. If after the confrontation the field’s cow count becomes 0, then no gang controls the field.
Input constraints: You are given m (the number of gangs) on the first line. The second line contains m space‐separated positive integers; the i
th integer is the number of cows in gang i
. (The total number of cows n is the sum of these m numbers.) You may assume that the total number of cows is small enough that a dynamic programming solution is feasible.
Task: Determine whether it is possible for gang 1 to control the field at the end. If yes, compute the maximum number of gang 1 cows that can be on the field and the lexicographically smallest ordering (represented as a sequence of gang numbers in the order the cows arrive) that attains that maximum.
inputFormat
The input begins with a line containing a single integer m (the number of gangs). The next line contains m space‐separated positive integers. The i
th integer represents the number of cows in gang i
.
For example, the input
3 5 3 2
indicates there are 3 gangs with gang 1 having 5 cows, gang 2 having 3 cows, and gang 3 having 2 cows.
outputFormat
If it is possible for gang 1 to control the field at the end, then on the first line output the maximum number of gang 1 cows that can remain grazing. On the second line output the lexicographically smallest ordering (as n space‐separated integers, each denoting the gang number of the arriving cow) that attains this maximum. If it is not possible, output a single line with NO
.
sample
3
5 3 2
4
1 2 1 3 1 2 1 1 3 1
</p>