2010年9月13日月曜日

PKU Judge Online 1477 Box of Bricks

■1477 Box of Bricks

□Problem
いくつかのブロックを縦に並べた列がいくつかある.全ての列の高さを平坦化するのに必要なブロックの移動回数の最小値を求めよ.
(便宜上90°回転してます)
■■■■■
■■
■■■■

■■■■■■■
■■■■■

■■■■
■■■■
■■■■
■■■■
■■■■
■■■■

□Solution
やるだけ.

□Code
  1. package p1477;  
  2.   
  3. import java.util.*;  
  4. import java.lang.*;  
  5. import java.math.*;  
  6.   
  7. public class Main{  
  8.     Scanner sc=new Scanner(System.in);  
  9.   
  10.     void run(){  
  11.         for(int s=1;; s++){  
  12.             int n=sc.nextInt();  
  13.             if(n==0)  
  14.                 break;  
  15.             int[] a=new int[n];  
  16.             int ave=0;  
  17.             for(int i=0; i<n; i++)  
  18.                 ave+=a[i]=sc.nextInt();  
  19.             ave/=n;  
  20.             int ans=0;  
  21.             for(int i=0; i<n; i++)  
  22.                 if(a[i]>ave)  
  23.                     ans+=a[i]-ave;  
  24.             println("Set #"+s);  
  25.             println("The minimum number of moves is "+ans+".");  
  26.             println("");  
  27.         }  
  28.     }  
  29.   
  30.     void println(String s){  
  31.         System.out.println(s);  
  32.     }  
  33.   
  34.     void print(String s){  
  35.         System.out.print(s);  
  36.     }  
  37.   
  38.     public static void main(String[] args){  
  39.         new Main().run();  
  40.     }  
  41. }  

0 件のコメント: