经典套路。

题意

你将随机生成一个长度为 \(k\) 的数组 \(a\),其中 \(a_i\) 有 \(p_i\) 概率为 \(1\),否则为 \(-1\)。定义其前缀和数组 \(s_i = \sum\limits_{j = 1}^{i}a_j, i \in [0, k]\)。如果前缀和数组的最大值为 \(t(t \in [0, k])\) 那么你将获得 \(h_t\) 的权值。给定数组 \(p_{1 \dots n}, h_{1 \dots n}\),对于所有 \(k = 1 \dots n\),请输出你获得权值的数学期望。对 \(10^9+7\) 取模。

数据范围:\(1 \le n \le 5000, 0\le h_i \le 10^9\)。

做法

考虑直接 DP,那么你必须记录当前值和先前的最大值两个信息,状态是 \(\Theta(n^3)\) 的,埋了。接下来考虑容斥,考虑了一年,没做出来。怎么办呢?

首先将问题抽象为一个二维平面上随机生成一串线段,要么向上斜要么向下斜,然后头尾接起来形成的形状的最高点是要考虑的东西。你可以联想到那种一排矩形的问题可以竖过来一层一层 DP。这里同理。不要考虑当前这一步在先前积累下的影响下要怎么走,而是去考虑在开头添加一步对后面的影响。那么我只关心后面的最高高度,它会被平移一下;而当前位置的高度是确定的——\(0\)。这样就好办了,直接倒过来 DP 就行了。

但是很不幸你必须枚举 \(f_{k, 0} = 1\) 作为转移起点再 DP,这样还是三方。但是没关系,我们发现最终状态是确定且唯一的。所以直接把 DP 转移的方向也全部倒过来,把系数 \(h_i\) 填到 \(f_{0, i}\) 作为初始状态,然后对于每个 \(k\) 统计 \(f_{k, 0}\) 的值即可。

代码

// Author: kyEEcccccc

#include <bits/stdc++.h>

using namespace std;

using LL = long long;
using ULL = unsigned long long; #define F(i, l, r) for (int i = (l); i <= (r); ++i)
#define FF(i, r, l) for (int i = (r); i >= (l); --i)
#define MAX(a, b) ((a) = max(a, b))
#define MIN(a, b) ((a) = min(a, b))
#define SZ(a) ((int)((a).size()) - 1) constexpr int N = 5005, MOD = 1000000007; LL kpow(LL x, LL k = MOD - 2)
{
x = x % MOD;
LL r = 1;
while (k)
{
if (k & 1) r = r * x % MOD;
x = x * x % MOD;
k >>= 1;
}
return r;
} int n, po[N];
int f[2][N]; void work(void)
{
cin >> n;
F(i, 1, n)
{
int x, y; cin >> x >> y;
po[i] = x * kpow(y) % MOD;
}
F(i, 0, n) cin >> f[0][i]; int ci = 1, pi = 0;
F(i, 1, n)
{
F(j, 1, n - i)
{
f[ci][j] = ((LL)f[pi][j + 1] * po[i]
+ (LL)f[pi][j - 1] * (1 - po[i] + MOD)) % MOD;
}
f[ci][0] = ((LL)f[pi][1] * po[i]
+ (LL)f[pi][0] * (1 - po[i] + MOD)) % MOD;
cout << f[ci][0] << " \n"[i == n];
swap(ci, pi);
}
} signed main(void)
{
// freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(nullptr); int _; cin >> _;
while (_--) work(); return 0;
}

CF1810G The Maximum Prefix的更多相关文章

  1. uva 11488 Hyper Prefix Sets(狂水)

    题意: 获得集合中最长前缀长度*有该前缀个数的最大值 Prefix goodness of a set string is length of longest common prefix*number ...

  2. HDU 11488 Hyper Prefix Sets (字符串-Trie树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  3. UVa11488-Hyper Prefix Sets(trie树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  4. uva 11488 - Hyper Prefix Sets(字典树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  5. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  6. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  7. Leetcode: Maximum XOR of Two Numbers in an Array

    Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  8. Maximum Subarray Sum

    Maximum Subarray Sum 题意 给你一个大小为N的数组和另外一个整数M.你的目标是找到每个子数组的和对M取余数的最大值.子数组是指原数组的任意连续元素的子集. 分析 参考 求出前缀和, ...

  9. LightOJ DNA Prefix(字典树+dfs)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/F F - DNA Prefix Time Limit:200 ...

  10. [LeetCode] Prefix and Suffix Search 前后缀搜索

    Given many words, words[i] has weight i. Design a class WordFilter that supports one function, WordF ...

随机推荐

  1. 随机分布和随机数生成——R语言

    在人们的生活中,很多场景都需要用到随机数,例如福利彩票,车牌摇号,公共用房分配等.在用数学模型, 包括概率统计模型处理实际应用中的问题时, 我们希望建立的模型能够尽可能地符合实际情况.但是,实际情况是 ...

  2. [PKM] 个人知识管理

    1 个人知识管理的需求 1.1 背景 随着信息大爆炸,碎片化的知识越来越多,原来中小学阶段在学校中习得的.传统的.基于纸质笔记的知识管理方式已不能满足当前的诉求. 传统的基于纸质笔记的知识管理方式 工 ...

  3. 全网最详细中英文ChatGPT-GPT-4示例文档-智能多功能学习机从0到1快速入门——官网推荐的48种最佳应用场景(附python/node.js/curl命令源代码,小白也能学)

    目录 Introduce 简介 setting 设置 Prompt 提示 Sample response 回复样本 API request 接口请求 python接口请求示例 node.js接口请求示 ...

  4. Go For Web:踏入Web大门的第一步——Web 的工作方式

    前言: 本文作为解决如何通过 Golang 来编写 Web 应用这个问题的前瞻,对 Golang 中的 Web 基础部分进行一个简单的介绍.目前 Go 拥有成熟的 Http 处理包,所以我们去编写一个 ...

  5. 解决CKEditor中img标签自动添加style样式的问题-禁止自动设置width和height 帝国cms编辑器图片自动加宽高

    在使用CKEditor的过程中发现,每次上传或添加图片的时候,总会自动给img标签添加width和height的style内联样式.由于网站本身对图片有进行自适应处理(添加了自适应的CSS),所以im ...

  6. vue侦听器(引入vue.js写法)

    首先在html中引入vue.js,具体怎么下载可以参考https://blog.csdn.net/lvoelife/article/details/129254906,下载后在html中引入: 侦听d ...

  7. 如何在现有项目中使用`Masa MiniApi`?

    首先我们现有创建一个空的WebApi的项目模板,这个项目模板和MasaFramework本身没有任何关联,我们本博客只是使用的MasaFramework的MiniApi的包 创建Asp.NET Cor ...

  8. rails的接口查询详解

    Retrieving Objects from the Database find "find"是一种常用的数据库查询方法,在Rails中被用于从数据库中查找单个记录.它可以接收一 ...

  9. ASP.NET Core Web API 流式返回,逐字显示

    Websocket.SSE(Server-Sent Events)和长轮询(Long Polling)都是用于网页和服务端通信的技术. Websocket是一种全双工通信协议,能够实现客户端和服务端之 ...

  10. 【Docker】镜像制作和管理

    一.Docker镜像说明 二.基于容器通过 docker commit 手动制作镜像 1.基于容器手动制作镜像步骤 1.下载官方系统镜像 2.基于官方基础镜像启动容器,并进入容器 3.在容器中进行配置 ...