Codeforces1037F Maximum Reduction 【单调栈】
题目分析:
没啥好说的,会单调栈就会做。
代码:
#include<bits/stdc++.h>
using namespace std; const int maxn = ;
const int mod = ; int n,k;
int pre[maxn],suf[maxn],a[maxn]; stack <int> sta;
void getpre(){
for(int i=n-;i>=;i--){
while(!sta.empty()&&a[sta.top()]<=a[i])pre[sta.top()]=i+,sta.pop();
sta.push(i);
}
while(!sta.empty()) pre[sta.top()]=,sta.pop();
}
void getsuf(){
for(int i=;i<n;i++){
while(!sta.empty()&&a[sta.top()]<a[i])suf[sta.top()]=i-,sta.pop();
sta.push(i);
}
while(!sta.empty()) suf[sta.top()]=n-,sta.pop();
} void read(){
scanf("%d%d",&n,&k);
for(int i=;i<n;i++) scanf("%d",&a[i]);
getpre(); getsuf();
} int getsize(int len){
int p = (len-)/k;
int ans = (1ll*len*p%mod-1ll*(+p)*p/%mod*k%mod+mod)%mod;
return ans;
} void work(){
int ans = ;k--;
for(int i=;i<n;i++){
int tot = getsize(suf[i]-pre[i]+);
tot -= getsize(suf[i]-i); tot += mod; tot %= mod;
tot -= getsize(i-pre[i]); tot += mod; tot %= mod;
ans += 1ll*tot*a[i]%mod; ans %= mod;
}
printf("%d",ans);
} int main(){
read();
work();
return ;
}
Codeforces1037F Maximum Reduction 【单调栈】的更多相关文章
- Codeforces 1107G Vasya and Maximum Profit [单调栈]
洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...
- Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈
Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...
- Maximum Xor Secondary CodeForces - 281D (单调栈)
Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...
- CodeForces 548D 单调栈
Mike and Feet Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Subm ...
- Codeforces548D:Mike and Feet(单调栈)
Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...
- Imbalanced Array CodeForces - 817D (思维+单调栈)
You are given an array a consisting of n elements. The imbalance value of some subsegment of this ar ...
- Educational Codeforces Round 23 D. Imbalanced Array 单调栈
D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 单调队列 Monotonic Queue / 单调栈 Monotonic Stack
2018-11-16 22:45:48 一.单调队列 Monotone Queue 239. Sliding Window Maximum 问题描述: 问题求解: 本题是一个经典的可以使用双端队列或者 ...
- spoj MINSUB 单调栈+二分
题目链接:点击传送 MINSUB - Largest Submatrix no tags You are given an matrix M (consisting of nonnegative i ...
随机推荐
- ASp.Net Mvc Core 重定向
在之前老版本的MVC中.重定向直接写 HttpContext.Response.Redirect("/404.html") 就好了,程序走到这里会自动返回302然后跳转了, 但是这 ...
- 关于 HTTP GET/POST 请求参数长度最大值的一个理解误区(转载)
1. Get方法长度限制 Http Get方法提交的数据大小长度并没有限制,HTTP协议规范没有对URL长度进行限制.这个限制是特定的浏览器及服务器对它的限制.下面就是对各种浏览器和服务器的最大处理能 ...
- form,ajax注册,logging日志使用
一.form表单类型提交注册信息 二.ajax版本提交注册信息 <!DOCTYPE html> <html lang="en"> <head> ...
- H5 68-伪元素选择器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- A direct formulation for sparse PCA using semidefinite programming
目录 背景 Sparse eigenvectors(单个向量的稀疏化) 初始问题(low-rank的思想?) 等价问题 最小化\(\lambda\) 得到下列问题(易推) 再来一个等价问题 条件放松( ...
- js中的join(),reverse()与 split()函数用法解析
<script> /* * 1:arrayObject.reverse() * 注意: 该方法会改变原来的数组,而不会创建新的数组.此函数可以将数组倒序排列 * 2:arrayObject ...
- Python之缩进块
pycharm编辑器识别冒号,当换行后下一行默认是缩进块的位置:
- 关于Fatal error: Paletter image not supported by webp 报错
报错提示 Fatal error: Paletter image not supported by webp 原因是由于图片被非法编辑过(相对PHP来说)造成, 有可能是某些编辑图片的软件的格式与PH ...
- [转帖]linux 清空history以及记录原理
linux 清空history以及记录原理 自己的linux 里面总是一堆 乱七八槽输错的命令 用这个办法 可以清空 linux的内容. 清爽一些. 1.当前session执行的命令,放置缓存中,执行 ...
- [转帖]前端-chromeF12 谷歌开发者工具详解 Sources篇
前端-chromeF12 谷歌开发者工具详解 Sources篇 原贴地址:https://blog.csdn.net/qq_39892932/article/details/82498748 cons ...