[CF1748E] Yet Another Array Counting Problem
题目描述
The position of the leftmost maximum on the segment $ [l; r] $ of array $ x = [x_1, x_2, \ldots, x_n] $ is the smallest integer $ i $ such that $ l \le i \le r $ and $ x_i = \max(x_l, x_{l+1}, \ldots, x_r) $ .
You are given an array $ a = [a_1, a_2, \ldots, a_n] $ of length $ n $ . Find the number of integer arrays $ b = [b_1, b_2, \ldots, b_n] $ of length $ n $ that satisfy the following conditions:
- $ 1 \le b_i \le m $ for all $ 1 \le i \le n $ ;
- for all pairs of integers $ 1 \le l \le r \le n $ , the position of the leftmost maximum on the segment $ [l; r] $ of the array $ b $ is equal to the position of the leftmost maximum on the segment $ [l; r] $ of the array $ a $ .
Since the answer might be very large, print its remainder modulo $ 10^9+7 $ .
输入格式
Each test contains multiple test cases. The first line contains a single integer $ t $ ( $ 1 \le t \le 10^3 $ ) — the number of test cases.
The first line of each test case contains two integers $ n $ and $ m $ ( $ 2 \le n,m \le 2 \cdot 10^5 $ , $ n \cdot m \le 10^6 $ ).
The second line of each test case contains $ n $ integers $ a_1,a_2,\ldots,a_n $ ( $ 1 \le a_i \le m $ ) — the array $ a $ .
It is guaranteed that the sum of $ n \cdot m $ over all test cases doesn't exceed $ 10^6 $ .
输出格式
For each test case print one integer — the number of arrays $ b $ that satisfy the conditions from the statement, modulo $ 10^9+7 $ .
样例 #1
样例输入 #1
4
3 3
1 3 2
4 2
2 2 2 2
6 9
6 9 6 9 6 9
9 100
10 40 20 20 100 60 80 60 60
样例输出 #1
8
5
11880
351025663
提示
In the first test case, the following $ 8 $ arrays satisfy the conditions from the statement:
- $ [1,2,1] $ ;
- $ [1,2,2] $ ;
- $ [1,3,1] $ ;
- $ [1,3,2] $ ;
- $ [1,3,3] $ ;
- $ [2,3,1] $ ;
- $ [2,3,2] $ ;
- $ [2,3,3] $ .
In the second test case, the following $ 5 $ arrays satisfy the conditions from the statement:
- $ [1,1,1,1] $ ;
- $ [2,1,1,1] $ ;
- $ [2,2,1,1] $ ;
- $ [2,2,2,1] $ ;
- $ [2,2,2,2] $ .
不妨设现在递归到了区间 \([l,r]\)
此时,我们可以用 ST 表求出 \([l,r]\) 靠左的最大值所在的位置,设在 \(k\) 吧。那么所有跨越了 \(k\) 的区间的最左最大值都在 \(k\)。有一点区间dp的痕迹。
然后定义 \(dp_{l,r,x}\) 为区间 \([l,r]\) 中的最大值至多为 \(x\) 的方案数。\(dp_{l,r,x}=dp_{l,k-1,x-1}\times dp_{k+1,r,x}+dp_{l,r,x-1}\)。这是因为所有跨越了点 \(k\) 的区间的左最大值都要在 \(k\),所以我们枚举 \(k\) 填什么,然后递归到左右两边计算。那么我们有了一个 \(O(n^2m)\) 的算法。
注意到真正有用的区间只有 \(O(n)\) 个,所以我们可以给区间编个号,然后进行上面的 dp。复杂度降到 \(O(nm)\),就可以过了。注意由于题目保证的是 \(nm\le10^5\),所以要进行一个二维压一维。
#include<bits/stdc++.h>
const int N=2e5+5,P=1e9+7;
struct node{
int mx,wh;
node operator+(const node&n)const{
if(n.mx>mx)
return n;
return (node){mx,wh};
}
}st[N][25];
int t,n,m,dp[3000005],lg[N],idx,x;
node ask(int l,int r)
{
int k=lg[r-l+1];
return st[l][k]+st[r-(1<<k)+1][k];
}
int turn(int x,int y)
{
return x*(m+3)+y;
}
int dfs(int l,int r)
{
if(l>r)
return 0;
int o=++idx;
node k=ask(l,r);
int lc=dfs(l,k.wh-1);
int rc=dfs(k.wh+1,r);
// printf("%d %d %d %d %d\n",o,l,r,lc,rc);
for(int j=1;j<=m;j++)
(dp[turn(o,j)]=dp[turn(o,j-1)]+1LL*dp[turn(lc,j-1)]*dp[turn(rc,j)]%P)%=P;
// for(int j=1;j<=m;j++)
// printf("%d ",dp[turn(o,j)]);
// puts("");
return o;
}
int main()
{
scanf("%d",&t);
for(int i=2;i<N;i++)
lg[i]=lg[i>>1]+1;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&x),st[i][0]=(node){x,i};
for(int i=1;i<=lg[n];i++)
for(int j=1;j+(1<<i)-1<=n;j++)
st[j][i]=st[j][i-1]+st[j+(1<<i-1)][i-1];
idx=0;
memset(dp,0,sizeof(dp));
for(int i=0;i<=m;i++)
dp[turn(0,i)]=1;
dfs(1,n);
printf("%d\n",dp[turn(1,m)]);
}
}
[CF1748E] Yet Another Array Counting Problem的更多相关文章
- 1748E Yet Another Array Counting Problem
1748E Yet Another Array Counting Problem 目录 1748E Yet Another Array Counting Problem 题目大意: 做法 code 题 ...
- UVA 1640 The Counting Problem UVA1640 求[a,b]或者[b,a]区间内0~9在里面各个数的数位上出现的总次数。
/** 题目:UVA 1640 The Counting Problem UVA1640 链接:https://vjudge.net/problem/UVA-1640 题意:求[a,b]或者[b,a] ...
- 『The Counting Problem 数位dp』
The Counting Problem Description 求 [L,R]内每个数码出现的次数. Input Format 若干行,一行两个正整数 L 和 R. 最后一行 L=R=0,表示输入结 ...
- POJ2282 The Counting Problem
题意 Language:DefaultEspañol The Counting Problem Time Limit: 3000MS Memory Limit: 65536K Total Submis ...
- The Counting Problem
The Counting Problem 询问区间\([a,b]\)中\(1\sim 9\)出现的次数,0 < a, b < 100000000. 解 显然为数位递推,考虑试填法,现在关键 ...
- [Codeforces 863D]Yet Another Array Queries Problem
Description You are given an array a of size n, and q queries to it. There are queries of two types: ...
- UVa 1640 - The Counting Problem(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- [Algorithm] Array production problem
Given an array of integers, return a new array such that each element at index i of the new array is ...
- UVA 1640 The Counting Problem
https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...
- [POJ 2282] The Counting Problem
[题目链接] http://poj.org/problem?id=2282 [算法] 数位DP [代码] #include <algorithm> #include <bitset& ...
随机推荐
- Android-JAR包的引用
一.JAR的定义 JAR文件(Java归档,英语:Java Archive)是一种软件包文件格式,以ZIP格式构建,以.jar为文件扩展名,通常用于聚合大量的Java类文件.相关的元数据和资源 ...
- FastJson不成想还有个版本2啊:序列化大字符串报错
背景 发现陷入了一个怪圈,写文章的话,感觉只有大bug或比较值得写的内容才会写,每次一写就是几千字,争取写得透彻一些,但这样,我也挺费时间,读者也未必有这么多时间看. 我想着,日常遇到的小bug.平时 ...
- jQuery Ajax执行顺序问题
代码如下: $(document).ready(function () { var res; $.ajax({ type: 'post', url: 'GridDemo.aspx/PlaceOrder ...
- 58同城二手车数据爬虫——数字加密解码(Python原创)
一.基础首页爬取 def crawler(): # 设置cookie cookie = '''cisession=19dfd70a27ec0e t_f805f7762a9a237a0deac37015 ...
- 从 5s 到 0.5s!CompletableFuture 异步任务优化技巧,确实优雅!
一个接口可能需要调用 N 个其他服务的接口,这在项目开发中还是挺常见的.举个例子:用户请求获取订单信息,可能需要调用用户信息.商品详情.物流信息.商品推荐等接口,最后再汇总数据统一返回. 如果是串行( ...
- 探索抽象同步队列 AQS
by emanjusaka from https://www.emanjusaka.top/archives/8 彼岸花开可奈何 本文欢迎分享与聚合,全文转载请留下原文地址. 前言 AbstractQ ...
- Go 项目代码布局
Go 项目代码布局 目录 Go 项目代码布局 一.Go 语言"创世项目"结构 1.1 src 目录结构三个特点 二.Go 项目布局演进 2.1 演进一:Go 1.4 版本删除 pk ...
- ORACLE错误代码一览表,方便大家查询!
ORACLE错误一览表,方便大家查询! ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最 ...
- TIM-有感BLDC转速解析
TIM-有感BLDC转速解析 1.基本概念解析 霍尔传感器的原理:通电线圈产生的磁场会使得转子所在位置会产生磁场,其中离得最近的霍尔传感器的磁场最强,进而导致最近霍尔传感器会产生最大的电压信号,这个最 ...
- [SWPUCTF 2021 新生赛]老鼠走迷宫(详细版
附件下载 https://wwvc.lanzouj.com/iYLez1br84jg 解题思路 用pyinstxtrator解析exe 重点:将无后缀的5先修改后缀为pyc,然后随便找一个pyc文件补 ...