結果

問題 No.1077 Noelちゃんと星々4
ユーザー kk
提出日時 2020-09-02 15:07:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 851 bytes
コンパイル時間 1,869 ms
コンパイル使用メモリ 199,680 KB
実行使用メモリ 42,612 KB
最終ジャッジ日時 2023-08-13 17:52:25
合計ジャッジ時間 3,641 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 23 ms
32,052 KB
testcase_03 AC 27 ms
38,360 KB
testcase_04 AC 10 ms
15,664 KB
testcase_05 AC 8 ms
11,572 KB
testcase_06 AC 12 ms
17,740 KB
testcase_07 AC 13 ms
19,760 KB
testcase_08 AC 10 ms
15,632 KB
testcase_09 AC 9 ms
13,784 KB
testcase_10 AC 29 ms
38,308 KB
testcase_11 AC 19 ms
26,148 KB
testcase_12 AC 17 ms
23,824 KB
testcase_13 AC 7 ms
11,564 KB
testcase_14 AC 29 ms
38,192 KB
testcase_15 AC 15 ms
21,760 KB
testcase_16 AC 11 ms
15,660 KB
testcase_17 AC 17 ms
23,980 KB
testcase_18 AC 28 ms
40,364 KB
testcase_19 AC 7 ms
11,596 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 32 ms
42,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++)
#define ALL(x) (x).begin(), (x).end()

const double PI = acos(-1);

int n;
int y[1000];
int dp[1000][10000+1];


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

  cin >> n;
  REP (i, n) cin >> y[i];

  for (int h = 0; h <= 10000; h++)
    dp[0][h] = abs(h - y[0]);

  for (int i = 1; i < n; i++) {
    for (int h = 0; h <= 10000; h++) {
      dp[i][h] = dp[i-1][h] + abs(h - y[i]);
      if (h > 0) {
        if (y[i] > h-1)
          dp[i][h] = min(dp[i][h], dp[i][h-1] - 1);
        else
          dp[i][h] = min(dp[i][h], dp[i][h-1] + 1);
      }
    }
  }

  int ret = 1<<30;
  for (int h = 0; h <= 10000; h++)
    ret = min(ret, dp[n-1][h]);
  cout << ret << endl;
  
  return 0;
}
0