【DP】 HDOJ 5236 Article

令dp[i]代表打出i个字符所需的期望,res[i]代表一口气打出i个字符的期望。对于res的转移有res[i] = (res[i-1]+1) * (1-p) + (res[i]+res[i-1]+1) * p。对于dp有dp[i] = min(dp[i], dp[j] + res[i-j] + x)。。由于收敛性,枚举的j不会超过10.

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005 
#define maxm 200005
#define eps 1e-7
#define mod 998244353
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

double res[maxn];
double dp[maxn];
int n, x;
double p;

void work(int _)
{
	scanf("%d%lf%d", &n, &p, &x);
	res[1] = 1.0 / (1.0 - p);
	int cnt = 200;
	double mx = (res[1] + x) * n;
	for(int i = 2; i <= 200; i++) {
		res[i] = (res[i-1] + 1.0) / (1.0 -  p);
		if(res[i] > mx) {
			cnt = i;
			break;
		}
	}
	dp[1] = res[1] + x;
	for(int i = 2; i <= n; i++) {
		dp[i] = dp[i-1] + res[1] + x;
		if(i <= cnt) dp[i] = min(dp[i], res[i] + x);
		for(int j = i-1; j > 0 && j >= i - cnt; j--)
			dp[i] = min(dp[i], dp[j] + res[i - j] + x);
	}
	printf("Case #%d: %.6f\n", _, dp[n]);
}

int main()
{
	int _;
	scanf("%d", &_);
	for(int i = 1; i <= _; i++) work(i);
	
	return 0;
}


你可能感兴趣的:(dp)