codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1729D.md (3455B)


      1 Source: https://codeforces.com/problemset/problem/1729/D
      2 
      3 D. Friends and the Restaurant
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 A group of $n$ friends decide to go to a restaurant. Each of the friends plans to order meals for $x_i$ burles and has a total of $y_i$ burles ($1 \le i \le n$).
     22 
     23 The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $x_i$ values in the group must not exceed the sum of $y_i$ values in the group.
     24 
     25 What is the maximum number of days friends can visit the restaurant?
     26 
     27 For example, let there be $n = 6$ friends for whom $x$ = [$8, 3, 9, 2, 4, 5$] and $y$ = [$5, 3, 1, 4, 5, 10$]. Then:
     28 
     29 * first and sixth friends can go to the restaurant on the first day. They will spend $8+5=13$ burles at the restaurant, and their total budget is $5+10=15$ burles. Since $15 \ge 13$, they can actually form a group.
     30 * friends with indices $2, 4, 5$ can form a second group. They will spend $3+2+4=9$ burles at the restaurant, and their total budget will be $3+4+5=12$ burles ($12 \ge 9$).
     31 
     32 It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.
     33 
     34 So, the maximum number of groups the friends can split into is $2$. Friends will visit the restaurant for a maximum of two days. Note that the $3$-rd friend will not visit the restaurant at all.
     35 
     36 Output the maximum number of days the friends can visit the restaurant for given $n$, $x$ and $y$.
     37 
     38 Input
     39 
     40 The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.
     41 
     42 The descriptions of the test cases follow.
     43 
     44 The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$) — the number of friends.
     45 
     46 The second line of each test case contains exactly $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$). The value of $x_i$ corresponds to the number of burles that the friend numbered $i$ plans to spend at the restaurant.
     47 
     48 The third line of each test case contains exactly $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$). The value $y_i$ corresponds to the number of burles that the friend numbered $i$ has.
     49 
     50 It is guaranteed that the sum of $n$ values over all test cases does not exceed $10^5$.
     51 
     52 Output
     53 
     54 For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.
     55 
     56 Example
     57 
     58 Input
     59 
     60 ```
     61 6
     62 
     63 6
     64 
     65 8 3 9 2 4 5
     66 
     67 5 3 1 4 5 10
     68 
     69 4
     70 
     71 1 2 3 4
     72 
     73 1 1 2 2
     74 
     75 3
     76 
     77 2 3 7
     78 
     79 1 3 10
     80 
     81 6
     82 
     83 2 3 6 9 5 7
     84 
     85 3 2 7 10 6 10
     86 
     87 6
     88 
     89 5 4 2 1 8 100
     90 
     91 1 1 1 1 1 200
     92 
     93 6
     94 
     95 1 4 1 2 4 2
     96 
     97 1 3 3 2 3 4
     98 ```
     99 
    100 Output
    101 
    102 ```
    103 2
    104 0
    105 1
    106 3
    107 1
    108 3
    109 ```
    110 
    111 Note
    112 
    113 The first test case in explained in the problem statement.
    114 
    115 In the second test case, friends cannot form at least one group of two or more people.
    116 
    117 In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($1+3+10 \ge 2+3+7$). Note that they do not have the option of splitting into two groups.