1. 程式人生 > >hdu ACM Steps 1.2.4 Box of Bricks

hdu ACM Steps 1.2.4 Box of Bricks

return logs 移動 solid bottom top 想想 ons 模擬題

模擬題。

題意為把高度不同的磚頭堆變成高度相同的磚頭堆最少需要移動幾次。

想想就知道,要把每個磚頭堆變成平均高度磚頭堆最少需要移動的塊數就是倆者的差值。

把所有差值都加起來以後要除以2,因為移動一塊磚頭對倆個磚頭堆有影響。

#include<cstdio>
#include<cmath>
const int maxn = 100 + 10;

int a[maxn],n,s,h,ans,kase;

int main() {
    while(scanf("%d",&n) && n) {
        s=ans=0;
        
for(int i=1;i<=n;i++) { scanf("%d",&a[i]); s+=a[i]; } h=s/n; for(int i=1;i<=n;i++) ans+=abs(h-a[i]); ans/=2; printf("Set #%d\nThe minimum number of moves is %d.\n",++kase,ans); printf("\n"); } return 0
; }

hdu ACM Steps 1.2.4 Box of Bricks