题目分析:

没啥好说的,会单调栈就会做。

代码:

 #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 【单调栈】的更多相关文章

  1. Codeforces 1107G Vasya and Maximum Profit [单调栈]

    洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...

  2. Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈

    Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...

  3. Maximum Xor Secondary CodeForces - 281D (单调栈)

    Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...

  4. CodeForces 548D 单调栈

    Mike and Feet Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Subm ...

  5. Codeforces548D:Mike and Feet(单调栈)

    Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...

  6. Imbalanced Array CodeForces - 817D (思维+单调栈)

    You are given an array a consisting of n elements. The imbalance value of some subsegment of this ar ...

  7. Educational Codeforces Round 23 D. Imbalanced Array 单调栈

    D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. 单调队列 Monotonic Queue / 单调栈 Monotonic Stack

    2018-11-16 22:45:48 一.单调队列 Monotone Queue 239. Sliding Window Maximum 问题描述: 问题求解: 本题是一个经典的可以使用双端队列或者 ...

  9. spoj MINSUB 单调栈+二分

    题目链接:点击传送 MINSUB - Largest Submatrix no tags  You are given an matrix M (consisting of nonnegative i ...

随机推荐

  1. 朱晔和你聊Spring系列S1E5:Spring WebFlux小探

    阅读PDF版本 本文会来做一些应用对比Spring MVC和Spring WebFlux,观察线程模型的区别,然后做一下简单的压力测试. 创建一个传统的Spring MVC应用 先来创建一个新的web ...

  2. VMware(威睿)后端开发笔试题总结

    1.   Linux中查看系统的发行版本信息 的命令? cat/etc/issue    和    lsb_release 2.   linux 挂载一个共享文件夹: mount  -t  cifc ...

  3. Python-Requests库详解

    查看一下是否安装requests库 什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关 ...

  4. java高精度学习笔记

    高精度基本用法 valueOf(parament)     将参数转换为指定的类型 add()   相加   subtract() 相减    multiply()  相乘    divide()  ...

  5. scrapy之多环境的选择使用

    scrapy之多环境的选择使用 个人主机主机上可能存在多个python环境,当在终端中使用scrapy时,容易产生错误,无法使用到自己想使用的那个python,如何解决这个问题呢? 出现这类问题时,直 ...

  6. Vue之小入门

    Vue之小入门 <div id="app">{{ greeting }}</div> <script> let oDiv = document. ...

  7. Linux sed使用方法

    目录 sed处理流程 测试数据 sed命令格式 sed命令行格式 行定位 定位1行 定位区间行(多行) 定位某一行之外的行 定位有跨度的行 操作命令 -a (新增行) -i(插入行) -c(替代行) ...

  8. awr format

    AWR-Format工具 在Chrome高版本中配置使用AWR-Format for Chrome插件

  9. oracle服务端安装与配置

    从oracle官网下载oracle服务端的安装包. 下载下来是两个压缩文件,两个压缩文件都解压(缺一不可)到同一目录下,最后会得到一个database文件夹. 双击database文件夹下的setup ...

  10. 头文件带和不带.h的区别

    所有C++标准库的头文件都是没有.h结尾的.这么做是为了区分,C标准库的头文件和C++标准库的头文件.比如最具代表性的: #include <string.h> // C 标准库头文件,包 ...