F - Stones in the Bucket

淡淡的烟草味﹌ 2022-01-30 08:13 275阅读 0赞

在这里插入图片描述
Output

For each test case output one line containing one integer, indicating the minimum number of times needed to make all the buckets contain the same number of stones.

Sample Input

4
3
1 1 0
4
2 2 2 2
3
0 1 4
1
1000000000

Sample Output

2
0
3
0

Hint

For the first sample test case, one can remove all the stones in the first two buckets.
For the second sample test case, as all the buckets have already contained the same number of stones, no operation is needed.
For the third sample test case, one can move 1 stone from the 3rd bucket to the 1st bucket and then remove 2 stones from the 3rd bucket.

题意:给你n堆石头,把他们分成每堆都一样的数目,可以移动也可以拿走,每次操作算一步。

思路:输入的时候统计一下总的和,如果就1堆,直接输出0。否则这n堆从小到大排序,在判断总和能不能整数这n堆,如果可以整除,就说明不需要移除,只需要移动,统计下平均数前面的数与平均数的差值就行了。如果不能整除,说明需要移除,就统计从平均数的后面与平均数的差值。

AC代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. typedef long long ll;
  5. const int mmax=1e6+10;
  6. int a[mmax];
  7. int main()
  8. {
  9. ll t;
  10. cin>>t;
  11. while(t--)
  12. {
  13. ll n;
  14. cin>>n;
  15. ll sum;
  16. sum=0;
  17. for(int i=1;i<=n;i++)
  18. {
  19. cin>>a[i];
  20. sum+=a[i];
  21. }
  22. if(n==1)
  23. cout<<"0"<<endl;
  24. else
  25. {
  26. sort(a+1,a+1+n);
  27. if(sum/n==(double)sum/n)
  28. {
  29. ll ave=sum/n,ans1=0;
  30. for(int i=1;i<=n;i++)
  31. {
  32. if(a[i]<=ave)
  33. {
  34. ans1+=(ave-a[i]);
  35. }
  36. else
  37. break;
  38. }
  39. cout<<ans1<<endl;
  40. }
  41. else
  42. {
  43. ll ave=sum/n;
  44. ll nun=0,k;
  45. for(int i=1;i<=n;i++)
  46. {
  47. if(a[i]>=ave)
  48. {
  49. nun+=(a[i]-ave);
  50. }
  51. }
  52. cout<<nun<<endl;
  53. }
  54. }
  55. }
  56. return 0;
  57. }

发表评论

表情:
评论列表 (有 0 条评论,275人围观)

还没有评论,来说两句吧...

相关阅读

    相关 A. Stones

    [A. Stones][] 题意:有三堆石头a b c,让你拿石头,问他最多能拿多少石头。 拿石头规则:1.从a堆拿1个,必须从b堆拿两个。 2.从b堆拿1个,必须从c