[Codeforces-div.1 494C] Helping People

试题分析

不难注意到题目所给的性质是一棵树,所以肯定是树形dp。

那么期望没有办法合并,我们还有一种最笨的方法就是求出概率然后直接乘上权值累加。

然后也不难得出朴素的dp:\(f_{i,j}\)表示区间\(i\),最大值为\(mx_i+j\)的概率。

也可以很轻松的写出一个\(O(nm^2)\)的dp,还需优化。

发现转移其中一维是一个前缀和乘积的形式,那么我们改变一下状态:\(f_{i,j}\)表示区间\(i\),最大值\(\leq mx_i+j\)的概率。

带回式子中检查,它是正确的。

于是$$f[x][j]=p_x\prod f[v][mx_x+j-1-mx_v]+(1-p_x)\prod f[v][mx_x+j-mx_v]$$

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm> using namespace std;
#define LL long long inline int read(){
int x=0,f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int INF = 2147483600;
const int MAXN = 400010;
const double eps = 1e-6; struct data{int l,r; double p;}q[MAXN<<1];
int N,Q; int a[MAXN+1],pre[MAXN+1][31],nod;
vector<int> vec[MAXN<<1]; bool vis[MAXN+1];
double f[5001][11051]; int mx; inline bool cmp(data a,data b){
if(a.l!=b.l) return a.l<b.l;
return a.r>b.r;
}
inline void insert(int rt,int l,int r){
q[++nod].l=l; q[nod].r=r; q[nod].p=0; vec[rt].push_back(nod); return ;
}
inline int build(int rt,int fa,int ql,int qr,int fro){
if(rt) vec[fa].push_back(rt); vis[rt]=true; int x=-1;
for(int l=fro,r;l<=Q;l=r){
if(qr<q[l].r) {x=l; break;}
r=build(l,rt,q[l].l,q[l].r,l+1);
} return (x==-1?Q+1:x);
}
int Log2[MAXN+1];
inline int Qt(int l,int r){
if(l>r) return 0; int x=Log2[r-l+1];
return max(pre[l][x],pre[r-(1<<x)+1][x]);
}
inline int Query(int x){return Qt(q[x].l,q[x].r);}
inline void dfs(int k,int fa){
if(vec[k].empty()) {f[k][0]=1.0-q[k].p;
for(int i=1;i<=Q*2;i++) f[k][i]=1.0; return ;
} int Mx=Query(k);
for(int i=0;i<vec[k].size();i++) dfs(vec[k][i],k);
for(int i=0;i<=Q;i++) {
double t1,t2; t1=t2=1.0;
for(int j=0;j<vec[k].size();j++){
t1=t1*f[vec[k][j]][Mx+i-1-Query(vec[k][j])];
t2=t2*f[vec[k][j]][Mx+i-Query(vec[k][j])];
} f[k][i]=(i?q[k].p:0)*t1+(1.0-q[k].p)*t2;
} for(int i=Q+1;i<=Q*2;i++) f[k][i]=f[k][i-1];
return ;
}
inline double calc(){
double ret=mx;
for(int i=1;i<=Q;i++) ret+=(f[0][i]-f[0][i-1])*i; return ret;
} int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
N=read(),Q=read(); for(int i=1;i<=N;i++) pre[i][0]=a[i]=read(),mx=max(mx,a[i]);
if(mx>Q) for(int i=1;i<=N;i++) pre[i][0]=a[i]=max(0,a[i]+Q-mx); for(int i=2;i<=N;i++) Log2[i]=Log2[i>>1]+1;
for(int j=1;j<=21;j++) for(int i=1;i+(1<<j)-1<=N;i++) pre[i][j]=max(pre[i][j-1],pre[i+(1<<(j-1))][j-1]);
for(int i=1;i<=Q;i++) q[i].l=read(),q[i].r=read(),scanf("%lf",&q[i].p); sort(q+1,q+Q+1,cmp); nod=Q;
q[0].l=1,q[0].r=N;q[0].p=0; build(0,-1,1,N,1); dfs(0,-1); printf("%.10lf\n",calc());
return 0;
}

[Codeforces-div.1 494C] Helping People的更多相关文章

  1. Codeforces Round #533 (Div. 2) E - Helping Hiasat 最大团

    E - Helping Hiasat 裸的最大团,写了一种 2 ^ (m / 2)  * (m / 2)的复杂度的壮压, 应该还有更好的方法. #include<bits/stdc++.h> ...

  2. Codeforces #282 div 1 C Helping People 题解

    CF 282 C Helping People 题解 [原题] time limit per test 2 seconds memory limit per test 512 megabytes in ...

  3. Codeforces Round #533 (Div. 2) E. Helping Hiasat(最大独立集)

    题目链接:https://codeforces.com/contest/1105/problem/E 题意:有 n 个事件,op = 1 表示我可以修改昵称,op = 2 表示一个名为 s_i 的朋友 ...

  4. Codeforces 494C - Helping People

    题意 有一个长度为 \(n\) 的数列 \(a\),有 \(m\) 个 操作,每个操作是给 \(a[l_i,r_i]\) 中的数都加一,一个操作有 \(p_i\) 的概率执行(否则不执行).一个性质是 ...

  5. Codeforces Round #802 (Div. 2)C. Helping the Nature(差分)

    题目链接 题目大意: 给你一个有n个元素的数组a,你可以通过一下三种操作使数组的每一个值都为0: 选择一个下标i,然后让a[1],a[2]....a[ i ] 都减一; 选择一个下标i,然后让a[i] ...

  6. codeforces div.1 A

    A. Efim and Strange Grade time limit per test 1 second memory limit per test 256 megabytes input sta ...

  7. Codeforces div.2 B. The Child and Set

    题目例如以下: B. The Child and Set time limit per test 1 second memory limit per test 256 megabytes input ...

  8. codeforces泛做..

    前面说点什么.. 为了完成日常积累,傻逼呵呵的我决定来一发codeforces 挑水题 泛做.. 嗯对,就是泛做.. 主要就是把codeforces Div.1的ABCD都尝试一下吧0.0.. 挖坑0 ...

  9. [CF787D]遗产(Legacy)-线段树-优化Dijkstra(内含数据生成器)

    Problem 遗产 题目大意 给出一个带权有向图,有三种操作: 1.u->v添加一条权值为w的边 2.区间[l,r]->v添加权值为w的边 3.v->区间[l,r]添加权值为w的边 ...

随机推荐

  1. 【洛谷 P5110】 块速递推(矩阵加速,分块打表)

    题目链接 掌握了分块打表法了.原来以前一直想错了... 块的大小\(size=\sqrt n\),每隔\(size\)个数打一个表,还要在\(0\text{~}size-1\)每个数打一个表. 然后就 ...

  2. Commonjs,AMD,CMD和UMD的差异

    CommonJS 一种服务器端模块化的规范,Nodejs实现了这种规范,所以就说Nodejs支持CommonJS. CommonJS分为三部分: require 模块加载 exports 模块导出 m ...

  3. 静态资源(JS/CSS)存储在localStorage

    一.简单了解SEO SEO由英文Search Engine Optimization缩写而来, 中文意译为“搜索引擎优化”.SEO是指从自然搜索结果获得网站流量的技术和过程. 搜索引擎不优化的网站分为 ...

  4. response.getWriter().write()和 response.getWriter().print()的区别

    异步上传图片的代码.发现里面用了response.getWriter().print(),故联想到response.getWriter().writer(),经过一番api的查找与实操,总结如下: r ...

  5. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

  6. linux===进程操作

    ps -ef ps -aux|grep chat.js a:显示所有程序 u:以用户为主的格式来显示 x:显示所有程序,不以终端机来区分 kill -9 nohup python da.py & ...

  7. (转)关于bootstrap, boosting, bagging,Rand forest

    转自:https://blog.csdn.net/jlei_apple/article/details/8168856 这两天在看关于boosting算法时,看到一篇不错的文章讲bootstrap, ...

  8. JDBC原生数据库连接

    我们在开发JavaWeb项目时,常会需要连接数据库.我们以MySQL数据库为例,IDE工具为eclipse,讲述数据库连接与基本操作. 第一步,我们在Web项目的WebContent中建一个简单的前端 ...

  9. telent

    telnet    ip  空格 port ctrl+]  进入 命令后 quit 退出 在linux/unix下使用telnet hostname port连接上主机后会提示Escape chara ...

  10. hdu 1003(最大子段和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...