Just Arrange the Icons——优雅的暴力

简介: BerPhone X is almost ready for release with n applications being preinstalled on the phone. A category of an application characterizes a genre or a theme of this application (like “game”, “business”, or “education”). The categories are given as integers between 1 and n, inclusive;
                          J. Just Arrange the Icons
                          time limit per test5 seconds
                          memory limit per test512 megabytes
                          inputstandard input
                          outputstandard output


BerPhone X is almost ready for release with n applications being preinstalled on the phone. A category of an application characterizes a genre or a theme of this application (like “game”, “business”, or “education”). The categories are given as integers between 1 and n, inclusive; the i-th application has category ci.


You can choose m — the number of screens and s — the size of each screen. You need to fit all n icons of the applications (one icon representing one application) meeting the following requirements:


On each screen, all the icons must belong to applications of the same category (but different screens can contain icons of applications of the same category);

Each screen must be either completely filled with icons (the number of icons on the screen is equal to s) or almost filled with icons (the number of icons is equal to s−1).

Your task is to find the minimal possible number of screens m.


Input


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


The first line of each test case contains an integer n (1≤n≤2⋅106) — the number of the icons. The second line contains n integers c1,c2,…,cn (1≤ci≤n), where ci is the category of the i-th application.


It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 2⋅106.


Output


Print t integers — the answers to the given test cases in the order they follow in the input. The answer to a test case is an integer m — the minimum number of screens on which all n icons can be placed satisfying the given requirements.


Example


input 1


3
11
1 5 1 5 1 5 1 1 1 1 5
6
1 2 2 2 2 1
5
4 3 3 1 2


output 1


3
3
4


Note


In the first test case of the example, all the icons can be placed on three screens of size 4: a screen with 4 icons of the category 1, a screen with 3 icons of the category 1, and a screen with 4 icons of the category 5.


题目大意:


给出 n 个数,表示这 n 个图标的类型,可以自己安排屏幕的大小尺寸(能够放图标的个数),然后问能够最少需要几个屏幕;

条件是:1. 不同一个屏幕只能是放一种类型的图标,但是同一种类型的图标可以放进不同的屏幕

2. 放的时候只能是放满或者是只空一个位置


方法:优雅的进行暴力就完了,注意统计数量的时候,不要用map,会被卡内存的(CF不会卡)

详尽的可以参考 这篇博客第22条


Code:


ll mp[maxn];
vector <ll>vet;
ll val(ll size, ll cnt) {
  ///屏幕大小和这一种一共的数量
  ll t1 = (cnt + size - 1) / size;
  /// 类似上取整比如大小为4,但是刚好有5个,这时候就不得不占用两个屏幕
  ll t2 = 0x3f3f3f3f;
  if (size > 1) {
    t2 = cnt / (size - 1);///最大的需要数量
    //因为每一屏幕可以放size - 1个
  }
  if (t1 <= t2) return t1;
  else return 0x3f3f3f3f;
}
int main()
{
  int T = read;
  while (T--) {
    ll n = read;
    ll ans = 0x3f3f3f3f;
    vet.clear();///每次都要清空
    for (ll i = 1; i <= n; i++) {
      ll x = read;
      mp[x]++;
    }
    for (ll i = 1; i <= n; i++) {
      if (mp[i]) {///只要是有
        vet.push_back(mp[i]);
        mp[i] = 0;
      }
    }
    sort(vet.begin(), vet.end());
    ll SZ = vet.size();
    ll lim = vet[0] + 1;
    for (ll i = 1; i <= lim; i++) {///最少的个数 +1 屏幕的大小
      ll sum = 0;
      for (ll j = 0; j < SZ; j++)
      {
        sum += val(i, vet[j]);
      }
      ans = min(ans, sum);///每次统计最小
    }
    printf("%lld\n", ans);
  }
  return 0;
}


目录
相关文章
|
6月前
|
前端开发 Java C++
面试官:用 CSS 实现一个元素的水平垂直居中,写出你能想到的所有答案
面试官:用 CSS 实现一个元素的水平垂直居中,写出你能想到的所有答案
|
算法
light oj 1047 - Neighbor House 动态规划
动态规划,这个和刘汝佳算法竞赛入门经典P158的数字三角形有些相似,不过是求最小的值,而且有些限制,每次走到点和上次走的点不在同一列。
30 0
|
存储 算法
【gif图文】KMP算法(从暴力匹配到快速匹配)
【gif图文】KMP算法(从暴力匹配到快速匹配)
85 0
|
JavaScript
JS 刷 Leetcode:228. 汇总区间
JS 刷 Leetcode:228. 汇总区间
JS 刷 Leetcode:228. 汇总区间
|
JavaScript
JS 刷 Leetcode:167.两数之和 II - 输入有序数组
JS 刷 Leetcode:167.两数之和 II - 输入有序数组
JS 刷 Leetcode:167.两数之和 II - 输入有序数组
|
JavaScript
JS 刷 Leetcode:026. 删除有序数组中的重复项
JS 刷 Leetcode:026. 删除有序数组中的重复项
JS 刷 Leetcode:026. 删除有序数组中的重复项
|
存储 JavaScript 索引
JS 刷 Leetcode:219. 存在重复元素 II
JS 刷 Leetcode:219. 存在重复元素 II
JS 刷 Leetcode:219. 存在重复元素 II
|
算法 前端开发 机器人
「LeetCode」62-不同路径⚡️
「LeetCode」62-不同路径⚡️
112 0
「LeetCode」62-不同路径⚡️
|
算法 机器人
☆打卡算法☆LeetCode 63、不同路径 II 算法解析
“给定一个矩阵,从矩阵左上角移动到右下角,并且中间还有障碍物,有多少条路径。”
|
算法 机器人
☆打卡算法☆LeetCode 62、不同路径 算法解析
“给定m * n带下的网格, 从网格左上角出发,求有多少条到右下角的路径。”