#P6917. [ICPC 2016 WF] Balanced Diet
[ICPC 2016 WF] Balanced Diet
题目描述
Every day, Danny buys one sweet from the candy store and eats it. The store has types of sweets, numbered from to . Danny knows that a balanced diet is important and is applying this concept to his sweet purchasing. To each sweet type , he has assigned a target fraction, which is a real number (). He wants the fraction of sweets of type among all sweets he has eaten to be roughly equal to . To be more precise, let denote the number of sweets of type that Danny has eaten, and let . We say the set of sweets is balanced if for every we have
[ n f_ i - 1 < s_ i < n f_ i + 1. ]
Danny has been buying and eating sweets for a while and during this entire time the set of sweets has been balanced. He is now wondering how many more sweets he can buy while still fulfilling this condition. Given the target fractions and the sequence of sweets he has eaten so far, determine how many more sweets he can buy and eat so that at any time the set of sweets is balanced.
输入格式
The input consists of three lines. The first line contains two integers (), which is the number of types of sweets, and (), which is the number of sweets Danny has already eaten.
The second line contains positive integers . These numbers are proportional to , that is, $\displaystyle f_ i = \frac{a_ i}{\sum _{j = 1}^ m a_ j}$. It is guaranteed that the sum of all is no larger than .
The third line contains integers (), where each denotes the type of sweet Danny bought and ate on the day. It is guaranteed that every prefix of this sequence (including the whole sequence) is balanced.
输出格式
Display the maximum number of additional sweets that Danny can buy and eat while keeping his diet continuously balanced. If there is no upper limit on the number of sweets, display the word forever.
6 5
2 1 6 3 5 3
1 2 5 3 5
1
6 4
2 1 6 3 5 3
1 2 5 3
forever
提示
Time limit: 2000 ms, Memory limit: 1048576 kB.
International Collegiate Programming Contest (ACM-ICPC) World Finals 2016