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 ...
随机推荐
- 跨界 - Omi 发布多端统一框架 Omip 打通小程序与 Web
Omip 今天,Omi 不仅仅可以开发桌面 Web.移动 H5,还可以直接开发小程序!直接开发小程序!直接开发小程序! Github Omi 简介 Omi 框架是微信支付线研发部研发的下一代前端框架, ...
- Python全栈开发之路 【第三篇】:Python基础之字符编码和文件操作
本节内容 一.三元运算 三元运算又称三目运算,是对简单的条件语句的简写,如: 简单条件语句: if 条件成立: val = 1 else: val = 2 改成三元运算: val = 1 if 条件成 ...
- 03-HTML之body标签
body标签 HTML标签按作用主要分为两类:字体标签和排版标签 HTML标签按级别主要分为两类:文本级标签和容器级标签 文本级标签:p.span.a.b.i.u.em.文本标签里只能放文字.图片.表 ...
- Makes And The Product CodeForces - 817B (思维+构造)
B. Makes And The Product time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- win64位安装python-mysqldb1.2.3
在其他版本的mysqldb里面时间查询有问题最后确定还是在 1.2.5 版本下来解决,需要解决的问题就是这个:“Cannot open include file: 'config-win.h': No ...
- js基础语法之函数
普通函数 function foo(a, b){ return a + b; } foo(10, 20) >>> 30 匿名函数 var f = function(){console ...
- SpringMVC controller 时间 T
Spring MVC 之 处理Date类型 - carl.zhao的专栏 - CSDN博客https://blog.csdn.net/u012410733/article/details/727730 ...
- Python3练习题 035:Project Euler 007:第10001个素数
import time def f(x): #判断 x 是否为素数,返回bool值 if x == 2: return True elif x <= 1: return False else: ...
- composer 自动加载 通过classmap自动架子啊
https://github.com/brady-wang/composer github地址 composer加载自己写的类 放入一个目录下 更改composer.json "autolo ...
- [转帖]50个必知的Linux命令技巧,你都掌握了吗?
50个必知的Linux命令技巧,你都掌握了吗? https://blog.51cto.com/lizhenliang/2131141 https://blog.51cto.com/lizhenlian ...