Upstairs and Downstairs
Time limit5sMemory limit512 MB
Konstantin chooses and orders at least K activities from limited copies to minimize the chance Ilia wakes after falling asleep.
- Level
Hard8 of 10
- Topics
- Probability, Dynamic programming, Greedy, Sorting
- Solved
- No attempts yet
Problem
Konstantin and Ilia live in the same house. Konstantin lives upstairs and likes activities that involve jumping, moving furniture around and making noise. Ilia lives downstairs and likes to sleep.
To have a good evening, Konstantin wants to perform at least activities. Last night Ilia asked him not to wake him up, and Konstantin agreed. He took the request literally, so he picks his activities so that the probability of waking Ilia after Ilia has fallen asleep is as small as possible.
Activity has a probability attached to it. When Konstantin performs that activity, Ilia is awake at the moment it ends with probability and asleep otherwise. Ilia's state before the activity does not change this probability. Konstantin can perform activity at most times. Doing it more often bores him, and a bored Konstantin does not have a good evening.
Konstantin fixes the whole sequence of activities in advance, so that:
- the sequence holds at least activities;
- activity appears at most times;
- the probability that Ilia is woken up at least once is as small as possible.
Ilia starts the evening awake. He is woken up when he is asleep at the moment one activity ends and awake at the moment the next activity ends. Konstantin cannot see whether Ilia is awake or asleep, so he cannot change the plan while the evening goes on.
Find the smallest Konstantin can reach.
Input
The first line holds the number of test cases . Each test case starts with a line holding two integers and . The next lines describe one activity each in the format a/b c: the activity leaves Ilia awake with probability at the moment it ends, and Konstantin can perform it at most times.
Limits
- and for every
- The sum of all in one test case is at most .
- the sum of all in that test case.
Output
For each test case print one line in the format Case #x: Q, where is the test case number starting from 1 and is the smallest probability that Ilia is woken up. Print with exactly nine digits after the decimal point.
Every answer is a rational number, and no answer lies within of a value halfway between two nine digit decimals, so rounding a double precision result gives the required digits.