結果

問題 No.463 魔法使いのすごろく🎲
ユーザー r_dream0r_dream0
提出日時 2017-02-20 21:35:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,788 bytes
コンパイル時間 759 ms
コンパイル使用メモリ 82,164 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-28 21:40:40
合計ジャッジ時間 2,231 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 1 ms
4,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 WA -
testcase_15 AC 3 ms
4,376 KB
testcase_16 WA -
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 WA -
testcase_21 AC 5 ms
4,376 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 5 ms
4,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 WA -
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 WA -
testcase_38 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;

int n, m;
vector<long double> c;

int main() {
  cin >> n >> m;
  c = vector<long double>(n);
  for(int i = 1; i < n - 1; i++) {
    cin >> c[i];
  }
  // 後ろからの期待値
  // x_i マス iに居る時の期待値
  vector<vector<long double>> M(n, vector<long double>(n + 1));
  // x_n = 0
  M[n-1][n-1] = 1;
  M[n-1][n] = 0;
  for(int i = 0; i < n - 1; i++) {
    for(int j = 1; j <= m; j++) {
      int to = i + j;
      if(to >= n) to = n - (to - n) - 1;
      M[i][to] = -1.0 / m;
    }
    M[i][i] = 1;
    M[i][n] = c[i];
  }
  // for(int i = 0; i < n; i++) {
  //   for(int j = 0; j <= n; j++) {
  //     cout 
  //       << M[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  for(int i = 0; i < n; i++) {
    int pivot = i;
    for(int j = i; j < n; j++) {
      if(abs(M[j][i]) > abs(M[pivot][i])) pivot = j;
    }
    swap(M[pivot], M[i]);
    for(int j = i + 1; j <= n; j++) {
      M[i][j] /= M[i][i];
    }
    M[i][i] = 1;
    for(int j = 0; j < n; j++) {
      if(i == j) continue;
      long double div = M[j][i] / M[i][i];
      for(int k = 0; k <= n; k++) {
        M[j][k] -= div * M[i][k];
      }
    }
  }
  vector<long double> de(n), dp(n);
  for(int i = 0; i < n; i++) de[i] = M[i][n];
  for(int i = n - 1; i >= 0; i--) {
    if(i + m >= n) dp[i] = c[i];
    else {
      long double t1 = 0;
      for(int j = 1; j <= m; j++) {
        t1 += dp[j + i];
      }
      dp[i] = min(de[i], t1 / m + c[i]);
      for(int j = 1; j <= m; j++) {
        dp[i] = min(dp[i], c[i] + de[i + j]);
      }
    }
  }
  cout << fixed << setprecision(10) << min(de[0], dp[0]) << endl;
  //for(int i = 0; i< n; i++) {
  //  cout << dp[i] << "," << de[i] << endl;
  //}
}
0