結果

問題 No.463 魔法使いのすごろく🎲
ユーザー koba-e964koba-e964
提出日時 2016-12-14 01:55:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,029 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 69,048 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 06:22:07
合計ジャッジ時間 2,125 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:58:55: warning: division by zero [-Wdiv-by-zero]
   vector<vector<double> > dp(2, vector<double>(n, 1.0 / 0));
                                                   ~~~~^~~

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;


int gauss_elim(vector<vector<double> > &A,
			  vector<double> &b) {
  int n = A.size();
  int m = A[0].size();
  assert (b.size() == n);
  int c = 0;
  // TODO no pivoting
  REP(i, 0, n) {
    while (c < m && A[i][c] == 0) {
      c++;
    }
    if (c >= m) {
      return i;
    }
    double aic = A[i][c];
    A[i][c] = 1;
    REP(j, c + 1, m) {
      A[i][j] /= aic;
    }
    b[i] /= aic;
    REP(j, 0, n) {
      if (i == j) { continue; }
      double ajc = A[j][c];
      A[j][c] = 0;
      REP(k, c + 1, m) {
	A[j][k] -= ajc * A[i][k];
      }
      b[j] -= ajc * b[i];
    }
  }
  return n;
}


int main(void){
  int n, m;
  cin >> n >> m;
  vector<double> c(n, 0);
  REP(i, 1, n - 1) {
    cin >> c[n - 1 - i];
  }
  vector<vector<double> > dp(2, vector<double>(n, 1.0 / 0));
  REP(i, 0, m + 1) {
    dp[0][i] = 0.0;
  }
  // Calculates dp[1][i], by using Gaussian elimination
  vector<vector<double> > A(m, vector<double>(m, 0));
  REP(i, 0, m) {
    A[i][i] = m;
  }
  vector<double> b(m);
  REP(i, 1, m) {
    REP(j, 1, m + 1) {
      A[i][abs(i - j)] -= 1.0;
      b[i] += c[abs(i - j)];
    }
  }
  int result = gauss_elim(A, b);
  assert (result == m);
  REP(i, 0, m) {
    dp[1][i] = b[i];
  }
  REP(i, m, n) {
    double tot = 0;
    REP(j, 1, m + 1) {
      tot += dp[1][i - j] + c[i - j];
    }
    dp[1][i] = tot / m;
  }
  REP(i, m, n) {
    double tot = 0;
    REP(j, 1, m + 1) {
      tot += dp[0][i - j] + c[i - j];
    }
    double mi = tot / m;
    REP(j, 1, m + 1) {
      mi = min(mi, dp[1][i - j] + c[i - j]);
    }
    dp[0][i] = mi;
  }
  if (0) {
    REP(i, 0, 2) {
      REP(j, 0, n) {
	cerr << "dp[" << i << "," << j << "]=" << dp[i][j] << endl;
      }
    }
  }
  printf("%.15f\n", dp[0][n - 1]);
}
0