B. Collecting Game

time limit per test

1 second

memory limit per test

256 megabytes

You are given an array a of n positive integers and a score. If your score is greater than or equal to ai, then you can increase your score by ai and remove ai from the array.

For each index i, output the maximum number of additional array elements that you can remove if you remove ai and then set your score to ai. Note that the removal of ai should not be counted in the answer.

Input

Each test contains multiple test cases. The first line contains an integer t (1≤t≤5000) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤105) --- the length of the array.

The second line of each test case contains n integers a1,a2,...,an (1≤ai≤109) --- the elements of the array.

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

Output

For each test case, output n integers, the i-th of which denotes the maximum number of additional array elements that you can remove if you remove ai from the array and then set your score to ai.

Example

Input

Copy

复制代码

4

5

20 5 1 4 2

3

1434 7 1442

1

1

5

999999999 999999999 999999999 1000000000 1000000000

Output

Copy

复制代码
4 3 0 3 1 
1 0 2 
0 
4 4 4 4 4 

Note

In the first test case, the answers are as follows:

If we start with i=4, our initial score is a4=4 and a=20,5,1,2. We can remove 3 additional elements in the following order:

  1. Since 4≥1, we can remove 1 and our score becomes 5. After this, a=20,5,2.
  2. Since 5≥5, we can remove 5 and our score becomes 10. After this, a=20,2.
  3. Since 10≥2, we can remove 2 and our score becomes 12. After this, a=20.

If we start with i=1 we can remove all remaining elements in the array, so the answer is 4.

If we start with i=2, we can remove 3 additional elements in the following order: 1, 4, 2.

If we start with i=3, we can remove no additional elements.

If we start with i=5, we can remove 1 additional element: 1.

解题说明:此题是一道模拟题,采用贪心算法,首先对数组排序,从大到小计算每个元素"合并所有更小元素后能否继续向上吞并"的可达上界下标,最后按原数组顺序输出每个元素的可达上界。

cpp 复制代码
#include<bits/stdc++.h>
#include<vector>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
	int t, n, i;
	cin >> t;
	while (t--)
	{
		cin >> n;
		vector<int>a(n), b(n);
		vector<long long>sm(n);
		map<int, int>mp;
		for (i = 0; i < n; ++i) 
		{
			cin >> a[i];
			b[i] = a[i];
		}
		sort(b.begin(), b.end());
		sm[0] = b[0];
		for (i = 1; i < n; ++i)
		{
			sm[i] = sm[i - 1] + b[i];
		}
		mp[b[n - 1]] = n - 1;
		for (i = n - 2; i >= 0; --i) 
		{
			if (sm[i] >= b[i + 1])
			{
				mp[b[i]] = mp[b[i + 1]];
			}
			else
			{
				mp[b[i]] = i;
			}
		}
		for (i = 0; i < n; ++i)
		{
			cout << mp[a[i]] << " ";
		}
		cout << endl;
	}
	return 0;
}
相关推荐
Go-higher16 小时前
C语言--数据结构-栈、队列、堆
c语言·数据结构
2601_966492311 天前
C语言变量揭秘
c语言·变量·编译器·线程安全·内存布局
AndreaYYmm1 天前
SIGUSR2 触发热重载:一个正在抓包的进程如何原子地换掉规则集
c语言·网络协议
2601_966494721 天前
C语言是什么
c语言·rust·linux内核·内存管理·系统编程
老师我太想进步了20261 天前
【数据结构-16】链式二叉树在线OJ题
c语言·数据结构·算法
程序员大阳1 天前
副队长C语言数据结构教程(4)--双向链表
c语言·数据结构·链表·双向链表
AndreaYYmm1 天前
一张 1KB 的表算出整个报文的校验和:CRC 查表法、端序陷阱与一个反直觉的性能结论
c语言·架构
AndreaYYmm1 天前
任务 A 的配置消息,在任务 B 起来之前就发了:两阶段启动屏障
c语言·架构
老花眼猫1 天前
C语言绘图方法:(22)高斯面旋转
c语言·经验分享·青少年编程·课程设计
Navigator_Z2 天前
LeetCode //C - 1326. Minimum Number of Taps to Open to Water a Garden
c语言·算法·leetcode