题目描述

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的更多相关文章

  1. 1748E Yet Another Array Counting Problem

    1748E Yet Another Array Counting Problem 目录 1748E Yet Another Array Counting Problem 题目大意: 做法 code 题 ...

  2. 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] ...

  3. 『The Counting Problem 数位dp』

    The Counting Problem Description 求 [L,R]内每个数码出现的次数. Input Format 若干行,一行两个正整数 L 和 R. 最后一行 L=R=0,表示输入结 ...

  4. POJ2282 The Counting Problem

    题意 Language:DefaultEspañol The Counting Problem Time Limit: 3000MS Memory Limit: 65536K Total Submis ...

  5. The Counting Problem

    The Counting Problem 询问区间\([a,b]\)中\(1\sim 9\)出现的次数,0 < a, b < 100000000. 解 显然为数位递推,考虑试填法,现在关键 ...

  6. [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: ...

  7. UVa 1640 - The Counting Problem(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. [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 ...

  9. UVA 1640 The Counting Problem

    https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...

  10. [POJ 2282] The Counting Problem

    [题目链接] http://poj.org/problem?id=2282 [算法] 数位DP [代码] #include <algorithm> #include <bitset& ...

随机推荐

  1. 【Qt6】工具提示以及调色板设置

    工具提示即 Tool Tip,当用户把鼠标移动到某个UI对象上并悬停片刻,就会出现一个"短小精悍"的窗口,显示一些说明性文本.一般就是功能描述,让用户知道这个XX是干啥用的. 在 ...

  2. Ubuntu Linux 更换国内源

    Ubuntu的官方源对于国内用户来说是比较慢的,可以将它的源换成国内的源,用起来就快很多了. # Ubuntu server 环境 ubuntu@ubuntu:~$ sudo su - [ sudo ...

  3. 开源 SD-Small 和 SD-Tiny 知识蒸馏代码与权重

    最近,人工智能社区在开发更大.更高性能的语言模型方面取得了显著的进展,例如 Falcon 40B.LLaMa-2 70B.Falcon 40B.MPT 30B; 以及在图像领域的模型,如 SD2.1 ...

  4. Stream流的应用

    Stream流的应用 Collectors.groupingBy(ShopCartItemDto::getShopId) stream()方法将该列表转化为一个流,可以对其中的元素进行操作. coll ...

  5. Mac上使用Royal TSX快速连接到OCI主机

    问题: 每次使用Royal TSX连接到OCI主机都要交互式输入opc这个用户名,次数多了也蛮烦. 那如何既指定用户名,又想要通过ssh私钥登陆机器呢? 这个需求确实很初级,但也着实困扰过我,因为开始 ...

  6. 深入浅出:SPI机制在JDK与Spring Boot中的应用

    本文分享自华为云社区<Spring高手之路14--深入浅出:SPI机制在JDK与Spring Boot中的应用>,作者:砖业洋__ . Spring Boot不仅是简化Spring应用开发 ...

  7. Matlab 实现连续PID环节与标记系统-3dB点

    Matlab 实现连续PID环节 连续PID环节传递函数: \[\frac{O(s)}{I(s)} = K_P \cdot \left( 1 + \frac{K_{I}}{s} + K_D\cdot ...

  8. Android Orm框架(GreenDao)

    Android Orm框架(GreenDao) 分类: android2014-04-10 14:29 723人阅读 评论(0) 收藏 举报 GreenDao与Ormlite对比 Ormlite:简单 ...

  9. win10系统单独编译和使用WebRTC的回声消除(AEC)、音频增益(AGC)、去噪(NS)模块

    一.简介 本人想单独编译并使用WebRTC的音频回声消除模块,奈何技术有限,于是在百度的海洋里大海捞针,发现了https://www.cnblogs.com/mod109/p/5827918.html ...

  10. Windows下音视频对讲演示程序(声学回音消除、噪音抑制、语音活动检测、自动增益控制、自适应抖动缓冲)(2023年07月13日更新)

    Windows下音视频对讲演示程序 必读说明 简介   本软件根据<道德经>为核心思想而设计,实现了两个设备之间进行音视频对讲,一般可用于楼宇对讲.智能门铃对讲.企业员工对讲.智能对讲机. ...