結果

問題 No.77 レンガのピラミッド
ユーザー Grun1396Grun1396
提出日時 2017-02-28 19:06:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,196 bytes
コンパイル時間 561 ms
コンパイル使用メモリ 64,620 KB
実行使用メモリ 4,496 KB
最終ジャッジ日時 2023-09-02 17:58:48
合計ジャッジ時間 1,725 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 1 ms
4,368 KB
testcase_09 AC 2 ms
4,368 KB
testcase_10 AC 2 ms
4,368 KB
testcase_11 AC 2 ms
4,368 KB
testcase_12 AC 2 ms
4,372 KB
testcase_13 AC 1 ms
4,368 KB
testcase_14 AC 1 ms
4,372 KB
testcase_15 AC 2 ms
4,372 KB
testcase_16 AC 1 ms
4,372 KB
testcase_17 AC 2 ms
4,372 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 2 ms
4,496 KB
testcase_20 AC 2 ms
4,368 KB
testcase_21 AC 1 ms
4,368 KB
testcase_22 AC 2 ms
4,368 KB
testcase_23 AC 2 ms
4,372 KB
testcase_24 AC 2 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0