LGP5204题解
@CF1327F
最小值看着有点怪,先转化成最大值吧。。。反正没啥区别。。。
考虑把最大值相同的区间和限制为这个最大值的区间都拿出来。然后离散化。问题变为让所有区间都满足最值为 \(c\)。
考虑 DP。设 \(dp[n][k]\) 表示到序列上的第 \(n\) 个位置后,上一个 \(c\) 在第 \(k\) 个位置。
设 \(L[n]\) 表示右端点为 \(n\) 的区间中,左端点最靠右的那个的左端点。如果没有就为 \(0\)。
转移:
\]
\]
可以直接令 \(dp[n]\) 继承 \(dp[n-1]\),然后动态维护这个有值的区间。每次操作的时候只需要支持全局乘和单点加就行了。
可以通过打标记的方法将复杂度降低至期望 \(O(n)\),具体见代码。
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<ctime>
typedef unsigned ui;
typedef __uint128_t LL;
typedef unsigned long long ull;
const ui M=1e5+5,mod=1e9+7;
ui n,k,c[M],V[M],iV[M],cl[M];std::vector<ui>t[M],Q[M];
struct Hash_Table{
const ui P[20]={
110881,581551,319477,140869,307759,536729,791159,503851,614693,375127,
450299,263429,300761,796303,397373,732731,847009,913687,435401,665201
};
ui mod;ull B;
ui cnt,h[1000000];
struct Node{
ui V,nx;
}t[M];
inline void init(){
ui id=rand()%20;
mod=P[id];B=((LL(1)<<64)+mod-1)/mod;
}
inline ui Find(const ui&x){
for(ui E=h[x-mod*ui(LL(B)*x>>64)];E;E=t[E].nx)if(t[E].V==x)return E;return-1;
}
inline void Insert(const ui&x){
ui&head=h[x-mod*ui(LL(B)*x>>64)];t[++cnt]=(Node){x,head};head=cnt;
}
}Hash;
ui L(1),R(0),q[M];
inline ui max(const ui&a,const ui&b){
return a>b?a:b;
}
inline ui Solve(const ui&x){
static ui L[M],q[M],p[M],dp[M],pre1[M],pre2[M];
const ui&v=V[x]-1,&iv=iV[x],&len=t[x].size();
for(ui id(1),i=0;i<Q[x].size();++i){
while(id<len&&t[x][id]<Q[x][i])++id;pre1[i]=id;
}
for(ui id(1),i=0;i<Q[x].size();++i){
while(id<len&&t[x][id]<=Q[x][i]+k-1)++id;pre2[i]=id-1;
}
for(ui i=0;i<len;++i)L[i]=q[i]=p[i]=0;
for(ui i=0;i<Q[x].size();++i)L[pre2[i]]=max(L[pre2[i]],pre1[i]);
ui l(0),sum(1),mul(1);dp[0]=1;
for(ui i=1;i<len;++i){
while(l<L[i-1]){
while(q[l]--)mul=1ull*mul*v%mod;while(p[l]--)mul=1ull*mul*iv%mod;
sum=(sum+1ull*(mod-mul)*dp[l++])%mod;
}
dp[i]=sum;sum=(1ull*sum*v+dp[i])%mod;++q[L[i-1]];++p[i];
}
while(l<L[len-1]){
while(q[l]--)mul=1ull*mul*v%mod;while(p[l]--)mul=1ull*mul*iv%mod;
sum=(sum+1ull*(mod-mul)*dp[l++])%mod;
}
return sum;
}
inline ui pow(ui a,ui b){
ui ans(1);for(;b;b>>=1,a=1ull*a*a%mod)if(b&1)ans=1ull*ans*a%mod;return ans;
}
inline void swap(ui&a,ui&b){
ui c=a;a=b;b=c;
}
inline void getinv(){
static ui s[M],t[M];const ui&n=Hash.cnt;s[0]=1;
for(ui i=1;i<=n;++i)t[i]=V[i]-1,s[i]=1ull*s[i-1]*t[i]%mod;s[n]=pow(s[n],mod-2);
for(ui i=n;i>=1;--i)swap(s[i],s[i-1]),s[i]=1ull*s[i]*s[i-1]%mod,s[i-1]=1ull*s[i-1]*t[i]%mod;
for(ui i=1;i<=n;++i)iV[i]=s[i];
}
signed main(){
srand(time(NULL));srand(rand()*rand());
ui ans(1);
scanf("%u%u",&n,&k);Hash.init();
for(ui i=1;i<=n-k+1;++i){
scanf("%u",c+i);c[i]=1000000000-c[i]+1;
cl[i]=Hash.Find(c[i]);
if(!~cl[i])Hash.Insert(c[i]),V[Hash.cnt]=c[i],t[Hash.cnt].push_back(0),cl[i]=Hash.cnt;
Q[cl[i]].push_back(i);
}
for(ui i=1;i<=n;++i){
if(L<=R&&q[L]+k<=i)++L;
if(i+k-1<=n){
while(L<=R&&c[q[R]]>=c[i])--R;q[++R]=i;
}
t[cl[q[L]]].push_back(i);
}
getinv();
for(ui i=1;i<=Hash.cnt;++i)ans=1ull*ans*Solve(i)%mod;printf("%u",ans);
}
LGP5204题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- C#中的字符串拼接@,$
转载自:https://blog.csdn.net/qq_40666620/article/details/101695138 一:@ @的意思是以@标注的字符出,其中所有的符号均为字符串符号,没有什 ...
- spring filter详解
一.Filter基本工作原理 1.Filter 程序是一个实现了特殊接口的 Java 类,与 Servlet 类似,也是由 Servlet 容器进行调用和执行的. 2.当在 web.xml 注册了一个 ...
- SpringBoot源码解读系列三——引导注解
我们再来看下SpringBoot应用的启动类: 查看代码 import org.springframework.boot.SpringApplication; import org.springfra ...
- 问题描述:Navicat连不上MySQL数据库
发现Navicat连不上MySQL后我首先觉得是MySQL的服务没有打开, 然后再cmd里面 启动MySQL服务 输入: net start mysql 发现没有此服务,然我网上找了一下 先初始化My ...
- Solution -「ARC 110E」Shorten ABC
\(\mathcal{Description}\) Link. 给定长度为 \(n\),包含 A, B, C 三种字符的字符串 \(S\),定义一次操作为将其中相邻两个不相同的字符替换为字符集 ...
- 01 MySQL数据库安装(Windows+Mac)
目录 MySQL数据库安装 Windows 1.主要版本简介 2.软件下载 3.文件目录简介 4.使用 4.1配置环境变量 4.2登录 制作MySQL服务端开机自启动 运行MySQL 4.3 密码修改 ...
- 黑客高端de浏览器使用秘籍
搜索引擎已经成为上网必不可少的工具之一,聪明的黑客们发现,搜索引擎也能成为发动网络攻击的工具. Google Hacking,原指利用Google搜索引擎搜索信息来进行入侵的技术和行为,如今已不再局限 ...
- [LeetCode]1470. 重新排列数组
给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列. 请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列, ...
- pytest(3)-测试命名规则
前言 在自动化测试项目中,单元测试框架运行时需要先搜索测试模块(即测试用例所在的.py文件),然后在测试模块中搜索测试类或测试函数,接着在测试类中搜索测试方法,最后加入到队列中,再按执行顺序执行测试. ...
- nginx加大缓存
http { server { listen 0.0.0.0:81; server_name localhost; -- proxy_buffer_size 128k; proxy_buffers 3 ...