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 ...
随机推荐
- 杭电 1061 Rightmost Digit计算N^N次方的最后一位
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
- 2017湘潭大学邀请赛H题(树的直径)
链接:https://www.icpc.camp/contests/4mYguiUR8k0GKE H. Highway The input contains zero or more test cas ...
- Jmeter之上传数据流(图片、文本等)请求
MIME类型~Content-Type: 参数名称~name
- Servlet 使用ServletConfig对象来配置Servlet
ServletContext和ServletConfig的关联 相同点: 1.都可以用来配置Servlet 2.都可以写在web.xml中. 区别点: 1.ServletContext对象,对于所有的 ...
- this is incompatible with sql_mode=only_full_group_by
mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by - Jim_.NET - 博客园 http://www.c ...
- PHP的优化建议(仅借鉴)
转载: https://www.awaimai.com/1050.html 1 字符串 1.1 少用正则表达式 能用PHP内部字符串操作函数的情况下,尽量用他们,不要用正则表达式, 因为其效率高于正则 ...
- MySQL数据库性能优化思路与解决方法(二转)
原文:http://bbs.landingbj.com/t-0-242512-1.html 1.锁定表 尽管事务是维护数据库完整性的一个非常好的方法,但却因为它的独占性,有时会影响数据库的性能,尤其是 ...
- springmvc通过HttpServletRequest进行参数传递
@RequestMapping("/itemEdit") public String itemEdit(HttpServletRequest request, Model mode ...
- 在linux和本地系统之间进行数据传输的简单方法--lrzsz
lrzsz是一款在linux里可代替ftp上传和下载的程序. >>提君博客原创 http://www.cnblogs.com/tijun/ << 提君博客原创 安装和使用非 ...
- 判断浏览器是否支持H5
window.onload = function() { if (!window.applicationCache) { alert("请升级您的浏览器版本,你的浏览器不支持HTML5!&q ...