Problem Description:
Bear Grylls is a forest lover, so he spends some free time taking care of many of her loved ones' animals. He likes to offer them treats, but wants to do that in an impartial way.
Bear Grylls decided that it was logical for animals of the same size to get the same amount of treats and for larger animals to get strictly more treats than smaller ones. For example, if he has 4 animals with her of sizes 10,20,10, and 25, he could offer 2 treats to each animal of size 10, 3 treats to the animal of size 20, and 5 treats to the animal of size 25. This requires her to buy a total of 2+3+2+5=12 treats. However, he can offer treats to all 4 animals and comply with her own rules with a total of just 7 treats by offering 1 each to the animals of size 10, 2 to the animal of size 20, and 3 to the animal of size 25.
Help Bear Grylls plan her next animal day. Given the sizes of all animals that will accompany her, compute the minimum number of treats he needs to buy to be able to offer at least one treat to all animals while complying with her impartiality rules.
#include<stdio.h>
#include<stdlib.h>
#define MAXN 105
#define read(x) scanf("%d",&x)
int s[MAXN];
int cmp(const void *a,const void *b)
{
return (*(int*)a - *(int*)b);
}
void sol()
{
int n;
read(n);
for(int i=0;i<n;i++)
read(s[i]);
qsort(s,n,sizeof(int),cmp);
int treats=1;
int total=1;
for(int i=1;i<n;i++)
{
if(s[i]>s[i-1])
treats++;
total+=treats;
}
printf("%d\n",total);
}
int main()
{
int T;
read(T);
while(T--)
sol();
return 0;
}
0 Comments