Computer Game
题面翻译
有n个游戏,每个游戏有收益ai,升级后的收益bi,每次成功概率pi。每秒可以玩一个游戏,如果成功则得到当前收益,并且可以升级任意某个游戏。求t秒后的期望收益的最大值。n≤1e5,t≤1e10,a<b
题目描述
Ivan plays some computer game. There are $ n $ quests in the game. Each quest can be upgraded once, this increases the reward for its completion. Each quest has $ 3 $ parameters $ a_{i} $ , $ b_{i} $ , $ p_{i} $ : reward for completing quest before upgrade, reward for completing quest after upgrade ( $ a_{i} < b_{i} $ ) and probability of successful completing the quest.
Each second Ivan can try to complete one quest and he will succeed with probability $ p_{i} $ . In case of success Ivan will get the reward and opportunity to upgrade any one quest (not necessary the one he just completed). In case of failure he gets nothing. Quests do not vanish after completing.
Ivan has $ t $ seconds. He wants to maximize expected value of his total gain after $ t $ seconds. Help him to calculate this value.
输入格式
First line contains $ 2 $ integers $ n $ ( $ 1 \le n \le 10^{5} $ ) and $ t $ ( $ 1 \le t \le 10^{10} $ ) — number of quests and total time.
Following $ n $ lines contain description of quests. Each description is $ 3 $ numbers $ a_{i} $ $ b_{i} $ $ p_{i} $ ( $ 1 \le a_{i} < b_{i} \le 10^{8} $ , $ 0 < p_{i} < 1 $ ) — reward for completing quest before upgrade, reward for completing quest after upgrade and probability of successful completing of quest. $ a_{i} $ and $ b_{i} $ are integers. All probabilities are given with at most $ 9 $ decimal places.
输出格式
Print the expected value.
Your answer will be accepted if absolute or relative error does not exceed $ 10^{-6} $ . Formally, let your answer be $ a $ , and the jury's answer be $ b $ . Your answer is considered correct if $ \frac{|a-b|}{max(b, ,, 1)} \le 10^{-6} $ .
样例 #1
样例输入 #1
3 2
3 1000 0.5
1 2 0.48
3 20 0.3
样例输出 #1
252.2500000000000
样例 #2
样例输入 #2
2 2
1 1000 0.1
2 3 0.2
样例输出 #2
20.7200000000000
容易发现,当某一次游戏成功后,一定是一直选择 \(p_ib_i\) 最大的游戏玩。设 \(s=\max\limits_{i=1}^n p_ib_i\)
定义 \(dp_i\) 为还有 \(i\) 次操作时,最大的期望。
那么 \(dp_i=\max\limits_{j=1}^n(1-p_j)dp_{i-1}+p_j(a_j+(i-1)s)=dp_{i-1}+p_j((i-1)s-dp_{i-1})+a_j\)
求 \(dp_{i}\) 的答案时,我们发现要想办法找到这个 \(j\)。把第 \(j\) 个游戏看为平面直角坐标系上 \(p_j,p_j\times b_j\) 的一个点的话,你会发现所有可能的决策一定在这些点的凸包上。容易证明 \((i-1)s-dp_{i-1}\) 一定是单调递增的,所以有决策单调性,可以扫过去判断哪个是最优决策。复杂度 \(O(n+T)\)
然后考虑把 \(T\) 给省掉。由于很长的一段 \(dp\) 都是一样的操作,这个操作可以用矩阵快速幂加速,所以考虑上矩阵乘法。倍增结束位置(也可以推数学),然后矩阵乘法计算判断有没有超过斜率的范围,复杂度降到 \(O(nlogT)\)
注意卡精度,把斜率的除法要全部换成乘法。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+5;
const double eps=1e-12;
struct matrix{
double a[3][3];
matrix operator*(const matrix&m)const{
matrix s;
memset(s.a,0,sizeof(s.a));
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
s.a[i][k]+=a[i][j]*m.a[j][k];
return s;
}
void operator=(const matrix&m){
memcpy(a,m.a,sizeof(a));
}
}pw[50],h,k;
struct line{
double k,b;
int id;
}g[N];
int n,st[N],tp=1,a[N];
LL t,nw;
double s,p[N],dp;
int cmp1(line x,line y)
{
if(fabs(x.k-y.k)>eps)
return x.k<y.k;
return x.b>y.b;
}
double sl(int x,int y)
{
return (g[y].b-g[x].b)/(g[x].k-g[y].k);
}
int main()
{
scanf("%d%lld",&n,&t);
for(int i=1,b;i<=n;i++)
scanf("%d%d%lf",a+i,&b,p+i),s=max(s,p[i]*b);
for(int i=1;i<=n;i++)
g[i]=(line){p[i],p[i]*a[i],i};
sort(g+1,g+n+1,cmp1);
st[1]=1;
for(int i=2;i<=n;i++)
{
if(fabs(g[i].k-g[st[tp]].k)<=eps)
continue;
while(tp^1&&(g[i].b-g[st[tp]].b)*(g[st[tp-1]].k-g[st[tp]].k)<(g[st[tp]].b-g[st[tp-1]].b)*(g[st[tp]].k-g[i].k))
--tp;
st[++tp]=i;
}
pw[0].a[1][1]=1;
pw[0].a[2][1]=1;
pw[0].a[2][2]=1;
h.a[0][2]=1;
for(int i=1;i<=tp;i++)
{
while(i^tp&&(nw*s-h.a[0][0])*(g[st[i+1]].k-g[st[i]].k)>g[st[i]].b-g[st[i+1]].b)
++i;
pw[0].a[0][0]=1-p[g[st[i]].id];
pw[0].a[1][0]=p[g[st[i]].id]*s;
pw[0].a[2][0]=p[g[st[i]].id]*a[g[st[i]].id];
for(int i=1;i<=40;i++)
pw[i]=pw[i-1]*pw[i-1];
for(int j=40;~j;--j)
{
if(nw+(1LL<<j)<t&&(i==tp||((nw+(1LL<<j))*s-(h*pw[j]).a[0][0])*(g[st[i+1]].k-g[st[i]].k)<=g[st[i]].b-g[st[i+1]].b))
{
nw+=1LL<<j;
h=h*pw[j];
}
}
h=h*pw[0],++nw;
if(nw==t)
break;
}
printf("%.6lf",h.a[0][0]);
}