Algoogle

Algorithm for Programming Contest

Typical DP Contest C - トーナメント

Category: TDPC Tag: dp

C - トーナメント

問題概要


解法


m人目がnラウンド目を勝つ確率をメモ化再帰した #Code1

left, rightは2分木のmのn-1ラウンド目のところの兄弟を根とした葉の範囲

tdpc-c

DP解 #Code2

コード


#code1

(C.d) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import std.stdio, std.string, std.conv;
import std.algorithm, std.array, std.bigint, std.math, std.range;
import core.thread;

string[] tokens;
int tokenId = 0;
string readToken(){
    for(;tokenId == tokens.length;)
        tokens = readln.split, tokenId = 0;
    return tokens[tokenId++];
}
int readInt(){ return to!int(readToken);}

int k;
int[1024] r;
double memo[1024][16];

void main(){
    k = readInt;
    foreach(i;0..1024) memo[i][] = -1;
    foreach(i;0..1<<k) r[i] = readInt;
    foreach(i;0..1<<k) writefln("%.10f", rec(i,k));
}

double rec(int m, int n){
    if(memo[m][n] >= 0) return memo[m][n];
    if(n == 0) return memo[m][n] = 1;

    double ret = 0, prev = rec(m, n - 1);
    int d = k - n;
    int left = 0, right = 1 << k, res;
    foreach(i;0..d){
        res = (left + right) / 2;
        if(m < res) right = res;
        else left = res;
    }
    res = (left + right) / 2;
    if(m < res) left = res;
    else right = res;
    foreach(i; left..right)
        ret += prev * rec(i, n - 1) / (1 + pow(10.0, 1.0 * (r[i] - r[m]) / 400));
    return (memo[m][n] = ret);
}

#code2

(C.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cmath>
#include <iostream>
using namespace std;
#define int long long
#define repi(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,a) repi(i,0,a)

int K, R[1024];
double dp[16][1024];

void input(){
    cin >> K;
    rep(i,1<<K) cin >> R[i];
}

void solve(){
    rep(i,1<<K) dp[0][i] = 1.0;
    repi(i,1,K+1)rep(j,1<<K){
        int d = j >> (i - 1);
        int l, r;
        d -= 2 * (d % 2);
        l = (d + 1) << (i - 1);
        r = (d + 2) << (i - 1);
        repi(k,l,r)if(j != k) dp[i][j] += dp[i-1][j] * dp[i-1][k] / (1 + pow(10.0,1.0*(R[k]-R[j]) / 400.0));
    }
    rep(i,1<<K) printf("%.9f\n",dp[K][i]);
}

signed main(){
    input();
    solve();
    return 0;
}

Comments