1748E Yet Another Array Counting Problem
1748E Yet Another Array Counting Problem
题目大意:
现在有一个长度为\(n\)的数组\(a\) , 现在你要找到一个数组\(b\)的每一个数都\(<= m\) 使得在\(n\)中的任意区间\([l , r]\)满足\(max_{i = l} ^ ra_i = max_{i = l} ^ rb_i\) 求方案数 $ mod 1e9 + 7$
做法
设\(dp_{pos , i}\)表示\(pos\)作为左边最大值的位置,且\(b_{pos} <= i\) 的方案数
显然\(dp_{pos , i} = \sum _{j = 1} ^ i dp_{lpos , j - 1} * dp_{rpos , j}\)
可以用线段树维护\(lpos、rpos\)
code
#include<bits/stdc++.h>
#define fu(x , y , z) for(int x = y ; x <= z ; x ++)
#define LL long long
using namespace std;
const int N = 2e5 + 5;
const LL mod = 1e9 + 7;
vector<LL> dp[N];
int a[N] , n , m , rt , tr[N << 2];
int read () {
int val = 0 , fu = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') fu = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
val = val * 10 + (ch - '0');
ch = getchar ();
}
return val * fu;
}
void build (int p , int l , int r) {
if (l == r) {
tr[p] = l;
}
else {
int mid = l + r >> 1 , ll = p << 1 , rr = (p << 1) + 1;
build (ll , l , mid) , build (rr , mid + 1 , r);
if (a[tr[ll]] >= a[tr[rr]]) {
tr[p] = tr[ll];
}
else
tr[p] = tr[rr];
}
}
int query (int p , int l , int r , int L , int R) {
if (L <= l && R >= r) {
return tr[p];
}
else {
int mid = l + r >> 1 , ans1 = 0 , ans2 = 0 , ll = p << 1 , rr = (p << 1) + 1;
if (L <= mid)
ans1 = query (ll , l , mid , L , R);
if (mid < R)
ans2 = query (rr , mid + 1 , r , L , R);
return a[ans1] >= a[ans2] ? ans1 : ans2;
}
}
int dfs (int l , int r) {
if (l > r)
return -1;
if (l == r) {
fu(i , 1 , m)
dp[l][i] = i;
return l;
}
int id = query (1 , 1 , n , l , r);
int ll = dfs (l , id - 1) , rr = dfs (id + 1 , r);
if (ll == -1)
fu(i , 1 , m)
dp[id][i] = (dp[rr][i] + dp[id][i - 1]) % mod;
else if (rr == -1)
fu(i , 1 , m)
dp[id][i] = (dp[ll][i - 1] + dp[id][i - 1]) %mod;
else
fu(i , 1 , m)
dp[id][i] = (dp[ll][i - 1] * dp[rr][i] % mod + dp[id][i - 1]) % mod;
return id;
}
int main () {
int T = read ();
while (T --) {
n = read () , m = read ();
fu(i , 1 , n) {
a[i] = read ();
dp[i].clear ();
dp[i].resize(m + 1);
}
build (1 , 1 , n);
rt = dfs (1 , n);
printf ("%lld\n" , dp[rt][m]);
}
return 0;
}
1748E Yet Another Array Counting Problem的更多相关文章
- 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& ...
- 863D - Yet Another Array Queries Problem(思维)
原题连接:http://codeforces.com/problemset/problem/863/D 题意:对a数列有两种操作: 1 l r ,[l, r] 区间的数字滚动,即a[i+1]=a[i] ...
随机推荐
- mergehex tools安装
(1)nRF5x command line tools包括Jlink驱动以及Nordic自己开发的一些命令行工具,具体包括Jlink驱动,nrfjprog,nrfutil以及mergehex等. 下载 ...
- [小技巧]Win32 - VS中手动编辑RC文件
用win32进行窗口编程时,如果资源文件不妥善集中管理会一不小心会删除一些资源文件,这时再进行项目编译的时候会发现rc文件因为缺失某个资源文件导致无法打开,我们常用的rc图形编辑器等于废了. 这时就需 ...
- 1.3Dmax界面_试图操作
一.初始界面 1.菜单栏(软件的核心) 2.工具栏 3.石墨工具 4.命令板块 5.场景大纲 tools--> new Scene Explorer 创建的物体信息就会从出现在这里 6.视图窗口 ...
- 2.IDEA的快捷键
1.IDEA代码等式两边自动加空格:ctrl+alt+L
- Leecode 53.最大子数组和(Java 贪心算法、动态规划两种方法)
想法(没看解析之前想不出来) -----------------看了解析和答案 1.贪心算法,若当前元素的之前和<0,则丢弃当前元素之前的数列 设一个maxSum作为子序列最大和,一个sum ...
- IOS开发基础之核心动画 基础动画、关键帧、组动画案例
// // ViewController.m // 30-核心动画 // // #import "ViewController.h" @interface ViewControll ...
- gitee使用
1.github的国内跳转 github国内无法直接访问,所以直接使用gitee导入github工程 https://gitee.com/ 2.虚拟机配置ssh公钥 https://gitee ...
- Spring--事务角色+事务属性
事务管理员 发起事务方,在Spring中通常指代业务层开启事务的方法 也就是相当于这个: 事务协调员 加入事务方,在Spring中通常指代数据层方法,也可以是业务层方法 也就是相当于这个: 事务相关配 ...
- Linux0.11源码学习(三)
Linux0.11源码学习(三) linux0.11源码学习笔记 参考资料: https://github.com/sunym1993/flash-linux0.11-talk https://git ...
- 做bad apple第一步:超级好用的you-get下载各大网站音频!!!!
1 安装在cmd中输入pip3 install you-get 就行了 2 简单用法下载视频: 超级暴力,直接 you-get + "网站" 就没了,会下载到当前路径.我想下载到哪 ...