結果

問題 No.463 魔法使いのすごろく🎲
ユーザー koba-e964koba-e964
提出日時 2016-12-14 01:52:48
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,186 bytes
コンパイル時間 999 ms
コンパイル使用メモリ 87,560 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-07 13:42:59
合計ジャッジ時間 2,249 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#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;
  }
  REP(i, 1, m) {
    REP(j, 1, m + 1) {
      A[i][abs(i - j)] -= 1.0;
    }
  }
  vector<double> b(m);
  REP(i, 0, m) {
    b[i] = c[i];
  }
  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