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 ...
随机推荐
- c++继承实例
#include <iostream> #include <vector> #include <string> using namespace std; class ...
- mysql安装设置mysql字符集utf8及修改密码
MySQL的下载,建议下载MySQL的解压缩版本 MySQL官网下载推荐别下最新版本的原因是因为很多之前用的jar包和工具类不兼容最新版本的 可以下5.多的和六点多的 这样的压缩包解压再配置就行了 安 ...
- D. Nastya Is Buying Lunch
链接 [https://codeforces.com/contest/1136/problem/D] 题意 有N个人,a[i]表示第i个人的编号,m个二元组. 当前一个在后一个的前面一个位置时二者可以 ...
- 用PhoneGap创建第一个项目
1.在eclipse中新建Android Project2.在项目的目录下,建两个文件夹:/libs/assets/www3.进入将刚刚下载并解压的PhoneGap包里Anroid目录,我们需要的资源 ...
- eclipse如何添加web dynamic project
很多eclipse版本是不能直接新建web dynamic project的,需要从网上找插件或更新. 比较方便的是在Help → Install-New-Software,点击add按钮,在Loca ...
- semantic-ui 输入框
1.标准输入框 semantic-ui中定义输入框需要将input标签包含于另外一个标签内,外层标签的class为ui input,注意外层标签可以是div,span.p.i. <div cla ...
- Squid配置之使用帐号密码验证
转自: https://blog.csdn.net/atco/article/details/43448885 1.安装squid使用root用户进行操作.先使用rpm检测是否已经安装了sql ...
- lumen 5.6 设置APP_KEY为32位长的随机字符串
在 App\Console\Commands下 添加以下内容的KeyGenerateCommand.php文件 <?php namespace App\Console\Commands; use ...
- Oracle 不小心删除undo数据文件以及磁盘空间不足导致不能登录的解决办法
在一次测试中,由于导入的数据量过大导致事务一直提交失败因为磁盘空间不够用了,一检查发现是undo表空间不够用,于是重新创建了一个表空间,准备把之前的undo表空间删除,删除时却发现一直删不掉,因为它一 ...
- object-fit 属性的用法介绍
这个要在宽,高都是100%的情况下才能提现 object-fit 属性的用法介绍 fill(不保持纵横比缩放图片,使图片完全适应) contain(保持纵横比缩放图片,使图片的长边能完全显示出来) c ...