题目
The king’s birthday dinner was attended by k guests. The dinner was quite a success: every person has eaten several dishes (though the number of dishes was the same for every person) and every dish was served alongside with a new set of kitchen utensils.
All types of utensils in the kingdom are numbered from 1 to 100. It is known that every set of utensils is the same and consist of different types of utensils, although every particular type may appear in the set at most once. For example, a valid set of utensils can be composed of one fork, one spoon and one knife.
After the dinner was over and the guests were dismissed, the king wondered what minimum possible number of utensils could be stolen. Unfortunately, the king has forgotten how many dishes have been served for every guest but he knows the list of all the utensils left after the dinner. Your task is to find the minimum possible number of stolen utensils.
Input
The first line contains two integer numbers n and k(1≤n≤100,1≤k≤100) — the number of kitchen utensils remaining after the dinner and the number of guests correspondingly.
The next line contains nn integers a1,a2,…,an (1≤ai≤100) — the types of the utensils remaining. Equal values stand for identical utensils while different values stand for different utensils.
Output
Output a single value — the minimum number of utensils that could be stolen by the guests.
Examples
input
5 2
1 2 2 1 3
output
1
input
10 3
1 3 3 1 3 5 5 5 5 100
output
14
题意:国王邀请k个人来赴宴就餐,每个人都吃了好几道菜(菜的数量对每个人来说都一样),每道菜都配有一套新的厨具。每一套器具都是一样的,由不同类型的器具组成,所有种类的器皿都由1到100编号。每一类器具最多在一道菜只能出现一次。晚餐结束后,客人们都被打发走了,国王想知道能多少餐具被偷。不幸的是,国王忘记了每一位客人有多少菜,但他知道晚餐后剩下的所有餐具的清单。求出丢失餐具的最小数量。
解题思路:首先找出个数最多的餐具种类记为max,当max%k==0时,则表示每个种类餐具数量都应为max(即每个人吃菜数量为max/k道),当max%k!=0时,表示菜的数量大于最多数量餐具的类型,则需要最小该类餐具类型数量为(max/k+1)*k.
AC–Code
import java.util.HashSet;
import java.util.Scanner;
public class CF1079A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] num = new int[101];
int n = sc.nextInt();
int k = sc.nextInt();
int max = 0;
HashSet<Integer> set = new HashSet<Integer>();
for(int i =0;i<n;i++)
{
int a = sc.nextInt();
num[a]++;
max = Math.max(max, num[a]);
set.add(a);
}
max = max%k==0?max:(max/k+1)*k;
int c = 0;
for (Integer integer : set) {
c += max-num[integer];
}
System.out.println(c);
sc.close();
}
}