news 2026/6/8 6:46:02

D. Friends and the Restaurant

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
D. Friends and the Restaurant

time limit per test

2 seconds

memory limit per test

256 megabytes

A group of n friends decide to go to a restaurant. Each of the friends plans to order meals for xi burles and has a total of yi burles (1≤i≤n).

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 xi values in the group must not exceed the sum of yi values in the group.

What is the maximum number of days friends can visit the restaurant?

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:

  • 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≥13, they can actually form a group.
  • 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≥9).

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.

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.

Output the maximum number of days the friends can visit the restaurant for given n, x and y.

Input

The first line of the input contains an integer t (1≤t≤104) — the number of test cases in the test.

The descriptions of the test cases follow.

The first line of each test case contains a single integer n (2≤n≤105) — the number of friends.

The second line of each test case contains exactly n integers x1,x2,…,xn (1≤xi≤109). The value of xi corresponds to the number of burles that the friend numbered i plans to spend at the restaurant.

The third line of each test case contains exactly n integers y1,y2,…,yn (1≤yi≤109). The value yi corresponds to the number of burles that the friend numbered i has.

It is guaranteed that the sum of n values over all test cases does not exceed 105.

Output

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.

Example

Input

Copy

6

6

8 3 9 2 4 5

5 3 1 4 5 10

4

1 2 3 4

1 1 2 2

3

2 3 7

1 3 10

6

2 3 6 9 5 7

3 2 7 10 6 10

6

5 4 2 1 8 100

1 1 1 1 1 200

6

1 4 1 2 4 2

1 3 3 2 3 4

Output

Copy

2 0 1 3 1 3

Note

The first test case in explained in the problem statement.

In the second test case, friends cannot form at least one group of two or more people.

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≥2+3+7). Note that they do not have the option of splitting into two groups.

解题说明:此题是一道模拟题,采用贪心算法,首先统计出每个人单独去的差值,然后进行从小到大排序,每次选择头尾的值,然后判断是否大于0,否则就增加选择的值,直到所有值全部遍历一遍得到最终解。

#include<bits/stdc++.h> #include<algorithm> #include<iostream> using namespace std; int a[100005]; int main() { int t; cin >> t; while (t--) { int n, x, ans = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> x; a[i] = x - a[i]; } sort(a, a + n); int j = n - 1; for (int i = 0; i < j; i++) { if (a[i] + a[j] >= 0) { ans++; j--; } } cout << ans << endl; } return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/8 6:45:18

玩转SSD1306的8种扫描模式:用Arduino实现OLED动画和特效显示

玩转SSD1306的8种扫描模式&#xff1a;用Arduino实现OLED动画和特效显示在创客和电子爱好者的世界里&#xff0c;OLED显示屏因其高对比度、低功耗和快速响应等特性&#xff0c;成为项目展示的理想选择。而SSD1306作为驱动这类显示屏的常用控制器&#xff0c;其功能远比我们想象…

作者头像 李华
网站建设 2026/6/8 6:42:43

Circle Loss超参数调优指南:如何在你的自定义数据集上找到最优的γ和m?

Circle Loss超参数调优实战&#xff1a;从理论到业务落地的γ与m选择策略当你在商品图像检索系统中发现模型对相似款式的区分度不足&#xff0c;或在声纹识别任务中遇到同类声音特征分散的问题时&#xff0c;Circle Loss的两个神秘参数γ和m往往成为破局关键。不同于传统损失函…

作者头像 李华
网站建设 2026/6/8 6:42:40

避坑指南:Apple Pay订阅续期与服务端状态同步的那些事儿(Java版)

避坑指南&#xff1a;Apple Pay订阅续期与服务端状态同步的那些事儿&#xff08;Java版&#xff09;订阅型商品在移动应用生态中扮演着重要角色&#xff0c;但相比一次性购买&#xff0c;自动续期订阅的后端实现复杂度呈指数级上升。作为Java后端工程师&#xff0c;我们不仅要处…

作者头像 李华
网站建设 2026/6/8 6:41:51

RAG生产实战:检索质量、生成稳定性与延迟优化七关

1. 这不是理论课&#xff0c;是我在三个RAG项目里踩出来的实操手册“Practical Tips and Tricks for Developers Building RAG Applications”——这个标题里最重的词不是RAG&#xff0c;不是Application&#xff0c;而是Practical。它不承诺你听懂Transformer架构就能上线&…

作者头像 李华