結果

問題 No.77 レンガのピラミッド
ユーザー リチウムリチウム
提出日時 2014-11-26 00:53:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 136 ms / 5,000 ms
コード長 1,173 bytes
コンパイル時間 2,071 ms
コンパイル使用メモリ 74,444 KB
実行使用メモリ 56,340 KB
最終ジャッジ日時 2023-09-01 22:08:46
合計ジャッジ時間 6,555 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,644 KB
testcase_01 AC 121 ms
55,932 KB
testcase_02 AC 123 ms
56,108 KB
testcase_03 AC 124 ms
56,340 KB
testcase_04 AC 121 ms
56,052 KB
testcase_05 AC 122 ms
55,852 KB
testcase_06 AC 132 ms
55,540 KB
testcase_07 AC 124 ms
55,868 KB
testcase_08 AC 136 ms
55,720 KB
testcase_09 AC 125 ms
55,696 KB
testcase_10 AC 124 ms
55,368 KB
testcase_11 AC 127 ms
55,652 KB
testcase_12 AC 131 ms
55,352 KB
testcase_13 AC 132 ms
56,024 KB
testcase_14 AC 131 ms
55,884 KB
testcase_15 AC 125 ms
55,964 KB
testcase_16 AC 123 ms
55,692 KB
testcase_17 AC 124 ms
55,548 KB
testcase_18 AC 129 ms
56,076 KB
testcase_19 AC 125 ms
55,812 KB
testcase_20 AC 126 ms
55,980 KB
testcase_21 AC 130 ms
55,936 KB
testcase_22 AC 128 ms
55,840 KB
testcase_23 AC 130 ms
55,488 KB
testcase_24 AC 124 ms
55,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.lang.Math;

public class Main {
	
	
  public static void main(String[] args) {
	  Scanner sc=new Scanner(System.in);
	  int n=sc.nextInt();
	  int a[]=new int[n];
	  int ans=Integer.MAX_VALUE;
	  int sum=0;
	  for(int i=0;i<n;i++){
		  a[i]=sc.nextInt();
		  sum+=a[i];
	  }
      int max=1;
      int z=1;
      while(true){
    	  if((z/2)*(z/2+1)+(z/2+1)<=sum){
    	  max=z;
    	  z+=2;
    	  }
    	  else break;
      }
      
	  for(int i=1;i<=max;i+=2){
		  int k=0;
		  int w=Math.max(max,n);
		  int p[]=new int[w];
		  int b[]=new int[w];
		  for(int j=0;j<n;j++){
			  b[j]=a[j];
		  }
		  int x=i/2+1;
		  int y=x*(x+1)-x;
		  if(y>sum)continue;
		  for(int j=0;j<i;j++){
			  if(j<x)p[j]=j+1;
			  else if(j<i) p[j]=i-j;
		  }
		  int s=sum;
		  while(s!=y){
			  for(int j=0;j<w;j++){
				  if(s!=y && b[j]>p[j]){
					  int m=Math.min(b[j]-p[j],s-y);
					  b[j]-=m;
					  s-=m;
					  k+=m;
				  }
			  }
				  
			  }
		  int u=0;
		  for(int j=0;j<w;j++){
			  u+=Math.abs(b[j]-p[j]);
		  }
		  ans=Math.min(ans,k+u/2);
		  }
	  System.out.println(ans);
  }}
0