首页 > 其他分享 >[CF1810G] The Maximum Prefix

[CF1810G] The Maximum Prefix

时间:2023-09-25 11:45:32浏览次数:36  
标签:le int contains CF1810G Maximum Prefix ch each array

题目描述

You're going to generate an array $ a $ with a length of at most $ n $ , where each $ a_{i} $ equals either $ 1 $ or $ -1 $ .

You generate this array in the following way.

  • First, you choose some integer $ k $ ( $ 1\le k \le n $ ), which decides the length of $ a $ .
  • Then, for each $ i $ ( $ 1\le i \le k $ ), you set $ a_{i} = 1 $ with probability $ p_{i} $ , otherwise set $ a_{i} = -1 $ (with probability $ 1 - p_{i} $ ).

After the array is generated, you calculate $ s_{i} = a_{1} + a_{2} + a_{3}+ \ldots + a_{i} $ . Specially, $ s_{0} = 0 $ . Then you let $ S $ equal to $ \displaystyle \max_{i=0}^{k}{s_{i}} $ . That is, $ S $ is the maximum prefix sum of the array $ a $ .

You are given $ n+1 $ integers $ h_{0} , h_{1}, \ldots ,h_{n} $ . The score of an array $ a $ with maximum prefix sum $ S $ is $ h_{S} $ . Now, for each $ k $ , you want to know the expected score for an array of length $ k $ modulo $ 10^9+7 $ .

输入格式

Each test contains multiple test cases. The first line contains a single integer $ t $ ( $ 1 \le t \le 5000 $ ) — the number of test cases. Their description follows.

The first line contains an integer $ n $ ( $ 1\le n \le 5000 $ ).

Then for the following $ n $ lines, each line contains two integers $ x_{i} $ and $ y_{i} $ ( $ 0 \le x_{i} < 10^9 + 7 $ , $ 1\le y_{i} < 10^9 + 7 $ , $ x_{i} \le y_{i} $ ), indicating $ p_{i} = \frac{x_{i}}{y_{i}} $ .

The next line contains $ n+1 $ integers $ h_{0},h_{1}, \ldots, h_{n} $ ( $ 0 \le h_{i} < 10^9 + 7 $ ).

It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 5000 $ .

蠢题,我更蠢。

最大值看起来很难处理。

有一个暴力:枚举最大值,定义 \(dp_{i,j,0/1}\) 为目前到 \(i\) 前缀和为 \(j\),目前是否达到过,复杂度 \(O(n^3)\)

考虑优化,把他们放在一起 dp. 定义 \(dp_{i,j,0/1}\) 为现在在 \(i\),离上限差 \(j\) ,是否到达过上界。

#include<bits/stdc++.h>
using namespace std;
const int P=1e9+7,N=5005;
int pown(int x,int y)
{
	if(!y)
		return 1;
	int t=pown(x,y>>1);
	if(y&1)
		return 1LL*t*t%P*x%P;
	return 1LL*t*t%P;
}
int read()
{
	int s=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')
		ch=getchar();
	while(ch>='0'&&ch<='9')
		s=s*10+ch-48,ch=getchar();
	return s;
}
int dp[N][N][2],n,T,p[N];
int main()
{
	T=read();
	while(T--)
	{
		n=read();
		for(int i=1,x,y;i<=n;i++)
			x=read(),y=read(),p[i]=1LL*x*pown(y,P-2)%P;
		dp[0][n+1][0]=dp[0][n+1][1]=0;
		for(int i=0;i<=n;i++)
			dp[0][i][!i]=read();
		for(int i=1;i<=n;i++)
		{
			dp[i][n+1][0]=dp[i][n+1][1]=0;
			int ans=0;
			for(int j=0;j<=n;j++)
			{
				if(j)
				{
					dp[i][j][0]=(dp[i-1][j-1][0]*(1LL+P-p[i])+dp[i-1][j+1][0]*1LL*p[i])%P;
					dp[i][j][1]=(dp[i-1][j-1][1]*(1LL+P-p[i])+dp[i-1][j+1][1]*1LL*p[i])%P;
				}
				else
					dp[i][j][1]=(dp[i-1][j+1][0]+dp[i-1][j+1][1])*1LL*p[i]%P,dp[i][j][0]=0;
				(ans+=dp[i][j][1])%=P;
				//printf("%d %d %d %d\n",i,j,dp[i][j][0],dp[i][j][1]);
			}
			printf("%d ",ans);
		}
		puts("");
	}
}

标签:le,int,contains,CF1810G,Maximum,Prefix,ch,each,array
From: https://www.cnblogs.com/mekoszc/p/17727611.html

相关文章

  • [LeetCode] 1353. Maximum Number of Events That Can Be Attended 最多可以参加的会
    Youaregivenanarrayof events where events[i]=[startDayi,endDayi].Everyevent i startsat startDayi andendsat endDayi.Youcanattendanevent i atanyday d where startTimei<=d<=endTimei.Youcanonlyattendoneeventatanytime ......
  • hadoop中mapred.tasktracker.map.tasks.maximum的设置
    目前,我们邮件的一部分log已经迁移到Hadoop集群上并由Hive来执行相关的查询hadoop中默认的mapred.tasktracker.map.tasks.maximum设置是2也即:每一个tasktracker同时运行的map任务数为2照此默认设置,查询80天某用户的操作日志,耗时5mins,45sec经过测试,发现将mapred.tasktracker.map.ta......
  • HBase HFile与Prefix Compression内部实现全解--KeyValue格式
    1.引子 HFile(HBaseFile)是HBase使用的一种文件存储格式的抽象, 目前存在两种版本的HFile:HFileV1和HFileV2 HBase0.92之前的版本仅支持HFileV1,HBase0.92/0.94同时支持HFileV1和HFileV2。 以下分别是HFileV1/V2的结构图: HFileV1HFileV2(注:这两个图片在hbase......
  • 洛谷 AT_maximum_cup_2018_a フィギュアスケート界の貴公子埼大選手 の 题解
    这道题是一道水题,所以你的代码很可能与我相似或相同,如果你的代码出现了问题,你很可能在我的题解里找出答案。这道题大概思路是一旦$10$秒后运动员会接触到毛绒玩具,那么就加上在这个点上毛绒玩具的数量。但是!这道题有一道巨坑的点!由于这道题比较远古,所以说你即使是正解,你也要在......
  • 【CF1364C】Ehab and Prefix MEXs(构造)
    题目大意:给出长度为\(n(1\len\le10^5)\)的数组\(a\),构造数组\(b\)使得\(a_i=MEX\{b_1,b_2,...,b_1\}\)首先考虑当\(b_1,b_2,...,b_n\)为什么数时,\(a_n=MEX\{b_1,b_2,...,b_n\}\)。然后再考虑当\(b_1,b_2,...,b_{n-1}\)为什么数时,\(a_{n-1}=MEX\{b_1,b_2,...,b_{n-1}\}\)。......
  • All Pairs Maximum Flow题解
    前置知识:1.P3376【模板】网络最大流2.P4897【模板】最小割树(Gomory-HuTree)Ebola有一句很著名的话如果你乱搞过了我请你抽烟那么这道题肯定不能普通的dinic直接水过去,不然就不是紫题了,那么直接祭出最小割树,复杂度\(O(Tn^3m)\),但是因为dinic跑不满,所以是可以过的。......
  • Caused by: java.sql.SQLSyntaxErrorException: ORA-00923: 未找到要求的 FROM 关键字
    最终是,查询条件,入参为null,所导致。JDBCgetParameterTypecallfailed-usingfallbackmethodinsteadRA-00923:FROMkeywordnotfoundwhereexpected 进一步,这个错误,在job执行的时候,会导致,oracle游标不够ORA-01000maximumopencursorsexceeded   参考: ......
  • Maximum Diameter 题解
    MaximumDiameter题目大意定义长度为\(n\)的序列\(a\)的权值为:所有的\(n\)个点的第\(i\)个点的度数为\(a_i\)的树的直径最大值,如果不存在这样的树,其权值为\(0\)。给定\(n\),求所有长度为\(n\)的序列的权值和。思路分析\(n\)个点的树的边数为\(n-1\),总度数......
  • CF1839C Insert Zero and Invert Prefix 题解
    首先考虑无解的情况,很明显\(a_n\)必须为\(0\),否则没有解,因为如果最后一位为\(1\)那么必须有\(n\)这个数存在于\(b\)序列中,而这种情况时不符合题意的。然后考虑如何求解,先考虑一种比较特殊的情况,就是若干个\(1\)后面接着一个\(0\),这里假设\(1\)的数量有\(k\)个,这......
  • 【CF1519D】Maximum Sum of Products
    #include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;lln,a[5000+10],b[5000+10],abpre[5000+10],absuf[5000+10],ans;intmain(){ cin>>n; for(lli=1;i<=n;i++)cin>>a[i]; for(lli=1;i<=n;i++)cin>>b[i]; for(......