zoukankan      html  css  js  c++  java
  • Hdoj 4089

    原题链接

    描述

    After 4 years' waiting, the game "Chinese Paladin 5" finally comes out. Tomato is a crazy fan, and luckily he got the first release. Now he is at home, ready to begin his journey.
    But before starting the game, he must first activate the product on the official site. There are too many passionate fans that the activation server cannot deal with all the requests at the same time, so all the players must wait in queue. Each time, the server deals with the request of the first player in the queue, and the result may be one of the following, each has a probability:
    1. Activation failed: This happens with the probability of p1. The queue remains unchanged and the server will try to deal with the same request the next time.
    2. Connection failed: This happens with the probability of p2. Something just happened and the first player in queue lost his connection with the server. The server will then remove his request from the queue. After that, the player will immediately connect to the server again and starts queuing at the tail of the queue.
    3. Activation succeeded: This happens with the probability of p3. Congratulations, the player will leave the queue and enjoy the game himself.
    4. Service unavailable: This happens with the probability of p4. Something just happened and the server is down. The website must shutdown the server at once. All the requests that are still in the queue will never be dealt.
    Tomato thinks it sucks if the server is down while he is still waiting in the queue and there are no more than K-1 guys before him. And he wants to know the probability that this ugly thing happens.
    To make it clear, we say three things may happen to Tomato: he succeeded activating the game; the server is down while he is in the queue and there are no more than K-1 guys before him; the server is down while he is in the queue and there are at least K guys before him.
    Now you are to calculate the probability of the second thing.

    输入

    There are no more than 40 test cases. Each case in one line, contains three integers and four real numbers: N, M (1 <= M <= N <= 2000), K (K >= 1), p1, p2, p3, p4 (0 <= p1, p2, p3, p4 <= 1, p1 + p2 + p3 + p4 = 1), indicating there are N guys in the queue (the positions are numbered from 1 to N), and at the beginning Tomato is at the Mth position, with the probability p1, p2, p3, p4 mentioned above.

    输出

    A real number in one line for each case, the probability that the ugly thing happens.
    The answer should be rounded to 5 digits after the decimal point.

    样例输入

    2 2 1 0.1 0.2 0.3 0.4
    3 2 1 0.4 0.3 0.2 0.1
    4 2 3 0.16 0.16 0.16 0.52

    样例输出

    0.30427
    0.23280
    0.90343

    思路

    算是一道动态规划的题目吧
    首先根据题意粗略写出状态转移方程
    设dp[i][j]表示队伍有i个人,tomato排在第j个位置时发生崩溃的概率(显然 j ≤ i)
    (mathtt{当 j=1 时,dp[i][1] = p1 * dp[i][1] + p2 * dp[i][i] + p4})
    (mathtt{当1<j≤k时,dp[i][j] = p1 * dp[i][j] + p2 * dp[i][j-1] + p3 * dp[i-1][j-1] + p4})
    (mathtt{当 j>k 时,dp[i][j] = p1 * dp[i][j] + p2 * dp[i][j-1] + p3 * dp[i-1][j-1]})
    (mathtt{由于递推求解,当求解dp[i][j]时,dp[i-1][j]全部是已知量,故p3 * dp[i-1][j-1]是定值,故用c来代替})
    (mathtt{当 j=1 时,c[1] = p41})
    (mathtt{当1<j≤k时,c[j] = p31 * dp[i-1][j-1] + p41})
    (mathtt{当 j>k 时,c[j] = p31 * dp[i-1][j-1]})
    (mathtt{令p21 = frac{p2}{1-p1},p31 = frac{p3}{1-p1},p41 = frac{p4}{1-p1}})
    (mathtt{故上式整理为})
    (mathtt{当 j=1 时,dp[i][1] = p21 * dp[i][i] + c[1]})
    (mathtt{当1<j≤k时,dp[i][j] = p21 * dp[i][j-1] + c[j]})
    (mathtt{当 j>k 时,dp[i][j] = p21 * dp[i][j-1] + c[j]})
    (mathtt{因此对于某个确定的i,有i个线性方程,由高斯消元法化简可求得dp[i][1],再递推求解每个值})
    第一次写完代码没考虑到double是八个字节,如果用二维数组会MLE,故考虑优化
    事实上,每个dp只和上一组dp有关,所以不需要二维数组,第一维可以省略,优化代码如下

    代码

    #include <bits/stdc++.h>
    #define maxn 2002
    using namespace std;
    
    double dp[maxn];
    double p[maxn], c[maxn];
    int n, m, k;
    double p1, p2, p3, p4;
    double p11, p21, p31, p41;
    
    int main()
    {
    	while(~scanf("%d%d%d", &n, &m, &k))
    	{
    		memset(dp, 0, sizeof(dp));
    		scanf("%lf%lf%lf%lf", &p1, &p2, &p3, &p4);
    		if(p4 < 0.00001) {printf("0.00000
    "); continue;}
    		p11 = 1.0 - p1; p21 = 1.0 * p2 / p11;
    		p31 = 1.0 * p3 / p11; p41 = 1.0 * p4 / p11;
    		p[0] = 1.0;
    		for(int i = 1; i <= n; i++) p[i] = p[i-1] * p21;
    		dp[1] = p4 / (1 - p1 - p2);
    		for(int i = 2; i <= n; i++)
    		{
    			for(int j = 1; j <= i; j++)
    			{
    				if(j <= k)
    					c[j] = p31 * dp[j-1] + p41;
    				else
    					c[j] = p31 * dp[j-1];
    			}
    			double tmp = 0;
    			for(int j = 1; j <= i; j++) tmp += p[i-j] * c[j];
    			dp[i] = tmp / (1 - p[i]);
    			dp[1] = p21 * dp[i] + p41;
    			for(int j = 2; j < i; j++)
    				dp[j] = p21 * dp[j-1] + c[j];
    		}
    		printf("%.5lf
    ", dp[m]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    java-继承的注意事项
    java-代码块-局部代码块、构造代码块、静态代码块
    java-源文件中可以有多个类,但是最多只能有一个public修饰
    java-权限修饰符的区别
    APP测试-adb简介
    APP测试-Android模拟器
    APP测试-SDK环境 for Windows
    自动化测试-流程
    接口测试-接口请求关联
    接口测试-moco+excel+requests框架
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8453316.html
Copyright © 2011-2022 走看看