結果

問題 No.77 レンガのピラミッド
コンテスト
ユーザー pillow
提出日時 2026-01-29 17:53:09
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,075 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,434 ms
コンパイル使用メモリ 337,520 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-01-29 17:53:13
合計ジャッジ時間 4,402 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

vector<int> pyramid(int p) {
    vector<int> t;
    for (int i = 1; i <= p; i++) t.push_back(i);
    for (int i = p - 1; i >= 1; i--) t.push_back(i);
    return t;
}

int main() {
    int n;
    cin >> n;
    int tot = 0;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        tot += a[i];
    }

    int ans = 1000000000;
    for (int j = 1; j * j <= tot; j++) {
        vector<int> p = pyramid(j);
        int excess = 0;
        int shor = 0;
        for (int i = 0; i < max((int)p.size(), (int)a.size()); i++) {
            if (i < a.size() && i < p.size()) {
                if (a[i] > p[i]) {
                    excess += a[i] - p[i];
                } else if (a[i] < p[i]) {
                    shor += p[i] - a[i];
                }
            } else if (i >= a.size()) {
                shor += p[i];
            } else if (i >= p.size()) {
                excess += a[i];
            }
        }
        ans = min(ans, max(excess, shor));
    }
    cout << ans << endl;
    return 0;
}
0