soft_drinks

Time limit: 3.5s Memory limit: 128MB Input: Output:

Peter has recently taken up mixing soft drinks as a part-time hobby. He has purchased the access to an infinite supply of NN different drink types. Each ii-th drink type (0≀i<N)(0 \leq i < N) contains AiA_i grams of sugar per liter and BiB_i grams of acids per liter.

Peter is hosting a gathering at his home and has invited QQ of his closest friends. Each friend is quite health-conscious and picky and arrives with the following constraints:

  • MAM_A: the maximum total weight (in grams) of sugar they are willing to consume.
  • MBM_B: the maximum total weight (in grams) of acids they are willing to consume.
  • LA,RAL_A,R_A: they will only drink from those drink types whose sugar concentration (sugar grams per liter) is in the range [LA,RA][L_A,R_A] (inclusive).

Peter can mix varying quantities (including fractional amounts) of each drink type to prepare a custom mocktail. Formally, let the real non-negative ViV_i be the amount (in liters) of the ii-th drink used in the final mixture. Then:

  • The total volume of the mocktail is βˆ‘i=0Nβˆ’1Vi\sum_{i=0} ^{N-1} V_i liters.
  • The total weight of sugar in the mixture is βˆ‘i=0Nβˆ’1Viβ‹…Ai\sum_{i=0} ^{N-1} V_i \cdot A_i grams.
  • The total weight of acids in the mixture is βˆ‘i=0Nβˆ’1Viβ‹…Bi\sum_{i=0} ^{N-1} V_i \cdot B_i grams.

Your task is to help Peter determine the maximum total volume (in liters) he can serve to each friend, while respecting their individual constraints.

Implementation details

You need to implement the following functions as defined in soft_drinks.h:

void init(const std::vector<int>& A, const std::vector<int>& B);

The init function gets called exactly once before any other queries. Its parameters correspond to:

  • A: a vector of length NN where A[i] is the sugar content per liter of the ii-th drink.
  • B: a vector of length NN where B[i] is the acid content per liter of the ii-th drink.
double friendDrink(int Ma, int Mb, int La, int Ra);

The friendDrink function gets called once for each of the QQ friends. Its parameters correspond to:

  • Ma: the maximum total weight of sugar the friend will consume.
  • Mb: the maximum total weight of acids the friend will consume.
  • La, Ra: the inclusive bounds on the sugar concentration of the drink types the friend will accept drinking from.

The function should return a double representing the maximum volume (in liters) that Peter can serve this friend without violating any of his constraints.

Your answer is considered correct, if its absolute or relative error doesn't exceed 10βˆ’610^{-6}. Namely, if your answer is aa, and the correct answer is bb, then your answer is accepted, if $$\frac{|a-b|}{max(1,|b|)} \leq 10^{-6}$

Local testing

A local grader Lgrader.cpp and the corresponding header file soft_drinks.h are provided.

Input format:

  • First line: integers NN and QQ.
  • Next NN lines: two integers AiA_i and BiB_i for each drink.
  • Next QQ lines: four integers MA,MB,LAM_A, M_B, L_A, and RAR_A for each friend.

The grader will call init, followed by friendDrink for each friend, printing the result to standard output.

Restrictions

  • 1≀N,Q≀1051 \leq N,Q \leq 10^5
  • 0≀Ai,Bi,MA,MB,LA,RA≀1090 \leq A_i, B_i, M_A, M_B, L_A, R_A \leq 10^9
  • Either Ai=ΜΈ0A_i \not= 0 or Bi=ΜΈ0B_i \not= 0
Subtask Points NN QQ Additional constraints
0 0 βˆ’- βˆ’- The sample test.
1 6 ≀2\leq 2 ≀100\leq 100 βˆ’-
2 9 ≀105\leq 10^5 ≀105\leq 10^5 MA=MBM_A=M_B
3 7 ≀105\leq 10^5 ≀105\leq 10^5 MA=0,Ai=0M_A=0,A_i=0
4 9 ≀500\leq 500 ≀500\leq 500 βˆ’-
5 15 ≀5Β 000\leq 5 \ 000 ≀5Β 000\leq 5 \ 000 βˆ’-
6 7 ≀105\leq 10^5 ≀1Β 000\leq 1 \ 000 (LA,RA)=(0,109)(L_A,R_A)=(0,10^9)
7 18 ≀105\leq 10^5 ≀105\leq 10^5 (LA,RA)=(0,109)(L_A,R_A)=(0,10^9)
8 29 ≀105\leq 10^5 ≀105\leq 10^5 βˆ’-

Example

stdin

4 3
4 0
2 8
6 4
3 6
8 2 4 6
8 2 3 6
0 10 0 10

Interaction

init({4, 2, 6, 3}, {0, 8, 4, 6})
friendDrink(8, 2, 4, 6): return 2
friendDrink(8, 2, 3, 6): return 2.0833333
friendDrink(0, 10, 0, 10): return 0

Log in or sign up to be able to send submissions!