結果
| 問題 |
No.77 レンガのピラミッド
|
| コンテスト | |
| ユーザー |
@abcde
|
| 提出日時 | 2019-03-07 00:13:12 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,270 bytes |
| コンパイル時間 | 1,408 ms |
| コンパイル使用メモリ | 163,572 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-23 14:41:46 |
| 合計ジャッジ時間 | 2,230 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 WA * 2 |
| other | AC * 3 WA * 17 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main() {
// 1. 入力情報取得.
int N;
cin >> N;
int A[200], B[200];
for(int i = 0; i < 200; i++) A[i] = B[i] = 0;
int total = 0;
for(int i = 0; i < N; i++){
int a;
cin >> a;
A[i] = a;
total += a;
}
// 2. ピラミッド配置への最小の移動数は?
// -> ピラミッド配置分は, 合計平方数になっているはず.
// ex. [1,2,3,4,3,2,1] の場合, 合計 16 = 4 * 4 になっている.
// 2-1. とりあえず, ピラミッドを作成してみる.
int sq = sqrt(total * 1.0);
int foot = sq * 2 - 1;
for(int i = 0; i < sq; i++) B[i] = B[foot - 1 - i] = i + 1;
// for(int i = 0; i < 10; i++) cout << A[i] << " ";
// cout << endl;
// for(int i = 0; i < 10; i++) cout << B[i] << " ";
// cout << endl;
// 2-2. 作成したピラミッド と 元のブロック位置 の 差分を確認.
// ex.
// [入力例]
// 6
// 1 4 2 7 8 3
//
// 1 4 2 7 8 3 0 0 0
// 1 2 3 4 5 4 3 2 1
// -> 8個移動が必要と考える.
// int ans = 0;
// for(int i = 0; i < 200; i++) ans += abs(A[i] - B[i]);
// -> ロジック誤りのため, ロジック修正を行った.
//
// ex.
// [入力例]
// 5
// 1 1 2 3 2
// の場合, パターン②の方が, 移動個数が少なくできることに注意.
//
// パターン①
// 1 1 2 3 2
// 1 2 3 2 1
// -> 2個移動が必要と考える.
// パターン②
// 1 1 2 3 2 0
// 0 1 2 3 2 1
// -> 1個移動が必要と考える.
vector<int> C;
for(int i = 0; i < foot; i++) C.push_back(B[i]);
int ans = 1e4 + 1;
for(int i = 0; i < 200; i++){
int mBlock = 0;
// 確認範囲は, vector C の サイズに限定する点に注意.
for(int j = 0; j < C.size(); j++) mBlock += abs(A[j] - C[j]);
ans = min(ans, mBlock);
// 次のターンで, C の 先頭に, 0 を追加する形で更新.
// C: 1 2 3 2 1 -> 0 1 2 3 2 1 -> 0 0 1 2 3 2 1 -> ...
C.insert(C.begin(), 0);
}
// 3. 出力.
ans /= 2;
cout << ans << endl;
return 0;
}
@abcde