結果

問題 No.77 レンガのピラミッド
ユーザー Yang33Yang33
提出日時 2018-04-17 00:21:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 5,000 ms
コード長 3,276 bytes
コンパイル時間 2,880 ms
コンパイル使用メモリ 166,912 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 10:59:47
合計ジャッジ時間 3,358 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/04/16  Problem: yukicoder 077  / Link: http://yukicoder.me/problems/no/077  ----- */
/* ------問題------

Vegaはレンガで平面ピラミッドを作りたいと考えてる。

庭には、すでにN列分のレンガが隙間なく積まれていて、それぞれのi列目の積まれている個数はAiで表される。
最初の配置から、レンガをいくつか動かして、ピラミッドの配置になるように並び替えたいです。

ピラミッドの配置とは、1から始めて[1,2,3,4,3,2,1]のように中心の位置まで1ずつ増え、中心以降は1ずつ減るような配置である。
(つまり、ピラミッド配置の列数は奇数になる。)

ピラミッドとは別の場所に、捨て置き場も設けられており、ピラミッドの配置に使われなかったレンガを置くこともできる。

移動とは、任意の列の一番上にある1つのレンガを持ち、別の列に移動するか、もしくは捨て置き場に移動することである。
このとき、ピラミッド配置は、最初の配置の1列目のレンガの場所から始めるとする。
与えられた列数よりか大きい列になる場合がある。
最初の配置から0個以上のレンガを移動しできるピラミッド配置への最小の移動数を求めてください。


ビラミッドの場所には1つのピラミッドのみがあるとして、不要なレンガはすべて捨て置き場に移動するとする。
レンガはどれも同じサイズの立方体であるとする。

-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

LL ans = 0LL;

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int N; cin >> N;
	VI a(N*100,0);
	FOR(i, 0, N) {
		cin >> a[i];
	}
	N *= 100;
	int sum = accumulate(ALL(a), 0);
	int s = 0, p = -1;
	while (s + p + 2 <= sum) {
		s += p + 2;
		p += 2;
	}
	int top = (p + 1) / 2;
	ans = INF;
	int t = top * 2 - 1;
	FOR(s, 0, N - t + 1) {
		LL ret = 0;
		FOR(i, 0, s) {
			ret += a[i];
		}

		FOR(i, s, s + t) {
			if (a[i] >= min(i + 1 - s, t - i - s)) {
				ret += a[i] - min(i + 1 - s, t - i - s);
			}
		}
		FOR(i, s + t, N) {
			ret += a[i];
		}
		ans = min(ans, ret);
	}

	cout << ans << "\n";

	return 0;
}
0