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,否则就增加选择的值,直到所有值全部遍历一遍得到最终解。

cpp 复制代码
#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;
}
相关推荐
裕晟资质规划9 小时前
武器装备科研生产单位保密资质申请方法论:条件模型与流程拆解
人工智能·算法
en.en..10 小时前
C语言 标准输入 / 输出缓冲区
算法
吃好睡好便好12 小时前
查找函数的使用
学习·算法·matlab·生活·查找函数
学习星球12 小时前
单调栈——从“找下一个更大的“到柱状图中的最大矩形
数据库·c++·算法·leetcode·xcode
靠沿13 小时前
贪心算法专题(二)
算法·贪心算法
猎头南楼14 小时前
具身智能模型部署方向的能力要求与技术栈梳理
人工智能·深度学习·算法
啥都想学点的研究生15 小时前
一篇文章讲清楚:线性回归问题的求解——正规方程法
算法·机器学习·线性回归
青少儿编程课堂15 小时前
树状数组精讲:逆序对计数与离散化(附双语言解法)
算法·树状数组·离散化·信息学奥赛·逆序对
血小板要健康15 小时前
网格 dfs 与 FloodFill:从岛屿、区域到搜索路径
笔记·算法·leetcode·深度优先
VALENIAN瓦伦尼安教学设备16 小时前
设备状态检测振动分析实训台案例分析
大数据·数据库·人工智能·嵌入式硬件·算法