結果
問題 | No.77 レンガのピラミッド |
ユーザー |
|
提出日時 | 2017-02-28 19:06:39 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 2,196 bytes |
コンパイル時間 | 586 ms |
コンパイル使用メモリ | 64,764 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-12 00:26:10 |
合計ジャッジ時間 | 1,420 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 20 |
ソースコード
#include <iostream> #include <vector> #include <math.h> #define MAX_N 100 using namespace std; int piramidd(int x,int n){ //n段のピラミッド列を構成するとして、x番目の段数を返す if(x > n){ return n - (x-n); }else{ return x; } } int main(){ int n; int total = 0; int min = 99999999; int tmp = 0,tmp2 = 0,tmp3=0; vector<int> A(200,0); cin >> n; for(int d =0;d <n;d++){ cin >> A[d]; total += A[d]; } int max_h = sqrt(total); // cout <<"max:" << max_h << " " <<"total:"<< total << endl; if(max_h > n){ // cout <<"change:"<< n << endl; max_h = n; } //if(max_h > 50) max_h = 50; // cout << "max_h:" << max_h << endl; //sqrt(total) ~ 1段まで ピラミッドを構成してみる。 for(int d = max_h; d > 0; d--){ if(total == d*d){ for(int e = 0; e <= 2*d-2;e++){ if( A[e]-piramidd(e+1,d)>0) tmp+= A[e]-piramidd(e+1,d); else tmp2 += (piramidd(e+1,d)- A[e]); // cout << "A[e]:"<< A[e] << " | " << "pi(e,d)" << piramidd(e+1,d) << " " << A[e]-piramidd(e+1,d) << endl; } // cout << "tmp2:" << tmp2 << " tmp:" << tmp<< endl; }else{ //ゴミ捨て場に移す必要がある for(int e = 0; e <= 2*d-2;e++){ if( A[e]-piramidd(e+1,d)>0) tmp+= A[e]-piramidd(e+1,d);//多くつまれている場合 else tmp2 += (piramidd(e+1,d)- A[e]);//不足分 // cout << "A[e]:"<< A[e] << " | " << "pi(e,d)" << piramidd(e+1,d) << " " << A[e]-piramidd(e+1,d) << " total:"<< tmp << endl; } for(int e = 2*d-1; e < n;e++){ tmp3 += A[e]; } tmp += tmp3;//捨てる分は 合計-必要な数-不足分? // cout << "tmp2:" << tmp2 << " tmp:" << tmp << " tmp3:" << tmp3<<endl; } if(tmp2 > tmp){ tmp = tmp2;} if(min > tmp){ min = tmp; } tmp = 0; tmp2 = 0; tmp3 = 0; // cout << "test^^"<< endl; } cout << min << endl; return 0; }