UVA 11176 - Winning Streak(dp)

4001 단어
Problem B Winning Streak Input: Standard Input
Output: Standard Output
 
"You can run on for a long time, sooner or later God'll cut you down."
– Traditional folk song
Mikael likes to gamble, and as you know, you can place bets on almost anything these days. A particular thing that has recently caughtMikael's interest is the length of the longest winning streak of a team during a season (i.e. the highest number of consecutive games won). In order to be able to make smarter bets, Mikael has asked you to write a program to help him compute the expected value of the longest winning streak of his favourite teams.
In general, the probability that a team wins a game depends on a lot of different factors, such as whether they're the home team, whether some key player is injured, and so on. For the first prototype of the program, however, we simplify this, and assume that all games have the same fixed probability p of being won, and that the result of a game does not affect the win probability for subsequent games.
The expected value of the longest streak is the average of the longest streak in all possible outcomes of all games in a season, weighted by their probability. For instance, assume that the season consists of only three games, and that p = 0.4. There are eight different outcomes, which we can represent by a string of 'W':s and 'L':s, indicating which games were won and which games were lost (for example, 'WLW' indicates that the team won the first and the third game, but lost the second). The possible results of the season are:
Result
LLL
LLW
LWL
LWW
WLL
WLW
WWL
WWW
Probability
0.216
0.144
0.144
0.096
0.144
0.096
0.096
0.064
Streak
0
1
1
2
1
1
2
3
In this case, the expected length of the longest winning streak becomes 0.216·0 + 0.144·1 + 0.144·1 + 0.096·2 + 0.144·1 + 0.096·1 + 0.096·2 + 0.064·3 = 1.104
Input
Several test cases (at most 40), each containing an integer 1 ≤ n ≤ 500 giving the number of games in a season, and a floating point number 0 ≤ p ≤ 1, the win probability. Input is terminated by a case where n = 0, which should not be processed.
 

Output


For each test case, give the expected length of the longest winning streak. The answer should be given as a floating point number with an absolute error of at most 10-4.

Sample Input                               Output for Sample Input

3 0.4
10 0.75
0 0.5
 
1.104000
5.068090
 
제목: n회보다 확률 p를 얻고 연속적으로 이길 기대를 구합니다.
사고방식: 아이디어를 내지 못하고 나중에 친구에게 물어봤다. dp[i][j]는 첫 번째이고 연속으로 이기는 횟수가 j의 모든 상황을 초과하지 않을 확률을 나타낸다.이렇게 해서 dp[i][j]=dp[i-1][j]는 이 상태가 아닌 경우의 확률, 즉 한 판을 더 이긴 후 연속적인 상황이 j를 초과하는 경우 이 상황은 마지막에 j개가 연속적으로 이기는 경우만 나타나므로 dp[i][j]=dp[i-1][j]-dp[i-[i-1][j][i-j][j][j][i-1][j]*P. 주의해야 한다.특이한 게 하나 있는데 다 이긴 거야. 따로 생각해야 돼.
코드:
#include <stdio.h>
#include <string.h>
const int N = 505;

int n;
double p;
double P[N];
double dp[N][N];

void init() {
    memset(dp, 0, sizeof(dp));
    P[0] = 1;
    for (int i = 0; i <= n; i ++)
	dp[0][i] = 1;
    for (int i = 1; i <= n; i ++)
	P[i] = P[i - 1] * p;
}

void solve() {
    init();
    double ans = 0; 
    for (int i = 1; i <= n; i++) {
	for (int j = 0; j <= n; j++) {
	    dp[i][j] = dp[i - 1][j];
	    if (j == i - 1)
		dp[i][j] -= P[j + 1];
	    else if (j < i - 1)
		dp[i][j] -= dp[i - 2 - j][j] * (1 - p) * P[j + 1];
	}
	for (int i = 1; i <= n; i ++)
	    ans += i * (dp[n][i] - dp[n][i - 1]);
    }
    printf("%.6lf
", ans); } int main() { while (~scanf("%d%lf", &n, &p) && n) { solve(); } return 0; }

좋은 웹페이지 즐겨찾기