Under construction

Still being built. Things may change

And Now For Something Completely Different!

Time limit1sMemory limit128 MB

Summary
The program scans a chore record for split points where both halves favor the opposite child from the total and prints each match with rounded percentages.
Level

Medium4 of 10

Topics
Prefix sum, Simulation, Implementation
Solved
No attempts yet

Problem

Bart and Lisa Simpson get plenty of chores, and they do not always do them well. So their father started keeping score, giving credit only when a chore was done right. After a month he showed them what he had written down.

The first month, in the order the chores were handed out:

Assigned toBartBartBartLisaLisaLisaBart
Done right1010111

Bart did 3 of his 4 chores right, so 75.00%. Lisa did 2 of her 3 right, so 66.67%.

The second month looked like this:

Assigned toBartBartLisaLisaLisaLisaLisaLisaLisa
Done right010110100

Bart did 1 of 2, so 50.00%. Lisa did 3 of 7, so 42.86%.

Their father then put the two months together to argue that Bart does the most chores:

Month 1Month 2ComputationTotal
Bart75.00%50.00%125.00 / 234.5353.30%
Lisa66.67%42.86%109.53 / 234.5346.70%

Lisa looked at the tables and said "No no no, that's not right, I've done more." Bart pointed at the numbers and said "Numbers don't lie." Lisa said "But look, I've done 5 chores and you've only done 4. You just look good because you always run away before somebody asks you to do something." Bart laughed and ran away.

Now Lisa needs a program that shows how unfair this scoring is. It reads one long record of chores and finds every place where cutting the record into a before part and an after part makes both parts point the opposite way from the whole record. To keep things fair, she counts a cut only when Bart and Lisa both have chores assigned on both sides of it.

Stated exactly: a record holds NN chores, numbered 1 to NN in the order they were handed out. Over the whole record let CBC_B be the number of chores Bart did right and CLC_L the number Lisa did right. When the two differ, the larger one is the conclusion of the whole record. Cutting after chore kk, where 1≤k<N1 \le k < N, splits the record into chores 1 to kk and chores k+1k+1 to NN. The cut counts only when both children have at least one chore in each part. Inside a part, a child's percentage is the number of chores that child did right in the part, divided by the number of chores assigned to that child in the part, times 100. The cut reverses the conclusion of the whole record when the child named by that conclusion has the lower percentage in both parts.

Input

The input holds several records. The first line of a record holds the number of chores NN (2≤N≤500002 \le N \le 50000). Each of the next NN lines holds Bart or Lisa, one space, and 0 or 1. The name is the child the chore was assigned to, and the number is 1 when the chore was done right and 0 when it was not. Every record has at least one chore for Bart and at least one for Lisa. The last line of the input holds 0 in place of a record.

Output

For each record, print the record number and how many chores each child did right on the first line:

Case <case number>: Bart did <CB> and Lisa did <CL>

If CBC_B and CLC_L are equal there is no conclusion to oppose, so print this on the next line:

Bart and Lisa accomplished same number of chores

If they differ, look at every cut that counts. If none of them reverses the conclusion of the whole record, print this on the next line:

Simpson's paradox not detected

If at least one does, print this on the next line:

Trend measured in 2 parts is reversed

and then one line per reversing cut, in increasing order of chore number:

After chore <chore number>: <BBefore>% <LBefore>% <BAfter>% <LAfter>%

BBefore and LBefore are Bart's and Lisa's percentages over chores 1 to the cut, and BAfter and LAfter are their percentages over the rest of the record. Print every percentage with exactly four digits after the decimal point. A value that sits exactly halfway between two such numbers rounds up. Print a blank line between the outputs of two consecutive records.

Note

Lisa wants the program to show where the before and after percentages support the opposite conclusion from the whole record, so two percentages that print as the same value count as no difference at all. A cut whose two before percentages print the same, or whose two after percentages print the same, is not counted.

Examples1

  1. Example 1

    Input
    16
    Bart 1
    Bart 0
    Bart 1
    Lisa 0
    Lisa 1
    Lisa 1
    Bart 1
    Bart 0
    Bart 1
    Lisa 0
    Lisa 1
    Lisa 1
    Lisa 0
    Lisa 1
    Lisa 0
    Lisa 0
    2
    Lisa 1
    Bart 1
    2
    Lisa 0
    Bart 1
    0
    
    Expected output
    Case 1: Bart did 4 and Lisa did 5
    Trend measured in 2 parts is reversed
    After chore 4: 66.6667% 0.0000% 66.6667% 55.5556%
    After chore 5: 66.6667% 50.0000% 66.6667% 50.0000%
    After chore 7: 75.0000% 66.6667% 50.0000% 42.8571%
    
    Case 2: Bart did 1 and Lisa did 1
    Bart and Lisa accomplished same number of chores
    
    Case 3: Bart did 1 and Lisa did 0
    Simpson's paradox not detected