Pinkie Pie Eats Patty-cakes(贪心或者二分)

Pinkie Pie has bought a bag of patty-cakes with different fillings! But it appeared that not all patty-cakes differ from one another with filling. In other words, the bag contains some patty-cakes with the same filling.

Pinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the patty-cakes but to try not to eat the patty-cakes with the same filling way too often. To achieve this she wants the minimum distance between the eaten with the same filling to be the largest possible. Herein Pinkie Pie called the distance between two patty-cakes the number of eaten patty-cakes strictly between them.

Pinkie Pie can eat the patty-cakes in any order. She is impatient about eating all the patty-cakes up so she asks you to help her to count the greatest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating!

Pinkie Pie is going to buy more bags of patty-cakes so she asks you to solve this problem for several bags!

Input

The first line contains a single integer TT (1≤T≤1001≤T≤100): the number of bags for which you need to solve the problem.

The first line of each bag description contains a single integer nn (2≤n≤1052≤n≤105): the number of patty-cakes in it. The second line of the bag description contains nn integers a1,a2,...,ana1,a2,...,an (1≤ai≤n1≤ai≤n): the information of patty-cakes' fillings: same fillings are defined as same integers, different fillings are defined as different integers. It is guaranteed that each bag contains at least two patty-cakes with the same filling.

It is guaranteed that the sum of nn over all bags does not exceed 105105.

Output

For each bag print in separate line one single integer: the largest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating for that bag.

Example

input

Copy

复制代码
4
7
1 7 1 6 4 4 6
8
1 1 4 6 4 6 4 7
3
3 3 3
6
2 5 2 3 1 4

output

Copy

复制代码
3
2
0
4

Note

For the first bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): 11, 66, 44, 77, 11, 66, 44 (in this way, the minimum distance is equal to 33).

For the second bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): 11, 44, 66, 77, 44, 11, 66, 44 (in this way, the minimum distance is equal to 22).

思路:

题意:最大化相同的相邻a的距离,=》其实是平均分配

1,可以二分,也可以贪心推公式,我fw

2,贪心:找到出现最多次的几个数,x个出现num次的数,对其平均分配,其它的不用管,

答案=(n-num*x)/(x-1)+num-1;

(n-num*x)剩余的个数,/(x-1)相邻之间插得数的个数,num-1次数相同的数导致的距离

代码:

cpp 复制代码
int a[maxj];
void solve(){
    int n;cin>>n;
    map<int,int>mp;
    int mx=0,sum=0;
    for(int i=1;i<=n;++i){
        cin>>a[i];
        mp[a[i]]++;
        if(mp[a[i]]==mx)sum++;
        if(mp[a[i]]>mx){
            mx=mp[a[i]];
            sum=1;
        }
    }
    cout<<(n-sum*mx)/(mx-1)+sum-1<<'\n';
}
相关推荐
沫璃染墨2 分钟前
《从零入门Linux系统篇(二十九):文件篇·二——深入文件描述符:从文件描述符表到重定向,再到Shell实现》
linux·运维·服务器·c++·驱动开发·系统架构
消费知多少2 分钟前
解析勤策签约大西洋焊接费用核销实践案例
开发语言·c#
leavesleo19 分钟前
DeepSeek 偷偷给你取外号?拆一下 AI 人格化背后的技术
算法
liliangcsdn33 分钟前
skewness收益偏度取负因子背后逻辑的探索
人工智能·算法·机器学习
LabVIEW开发33 分钟前
LabVIEW按段拆分TDMS文件的格式边界与重构
开发语言·数据库·重构·labview·labview知识·labview功能·labview程序
码匠许师傅42 分钟前
【C++ 面试真题】35. 聊聊 C++ 的万能引用(T&&)和完美转发(std::forward)
java·c++·面试
asdzx671 小时前
使用 Python 为 Excel 添加各类数据验证规则
开发语言·python·excel
Romantic_love_1 小时前
理解USART真实收发过程
开发语言·stm32·单片机·学习
鹿角片ljp1 小时前
LeetCode 300. 最长递增子序列|从 DP O (n²) 到贪心 + 二分 O (n log n)
算法·动态规划
叠层归一研究院1 小时前
缝合维度:时空3+1结构的拓扑起源
人工智能·算法·机器学习·agi