Codeforces1037F Maximum Reduction 【单调栈】

题目分析:

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

 

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn = 1005000;
 5 const int mod = 1000000007;
 6 
 7 int n,k;
 8 int pre[maxn],suf[maxn],a[maxn];
 9 
10 stack <int> sta;
11 void getpre(){
12     for(int i=n-1;i>=0;i--){
13     while(!sta.empty()&&a[sta.top()]<=a[i])pre[sta.top()]=i+1,sta.pop();
14     sta.push(i);
15     }
16     while(!sta.empty()) pre[sta.top()]=0,sta.pop();
17 }
18 void getsuf(){
19     for(int i=0;i<n;i++){
20     while(!sta.empty()&&a[sta.top()]<a[i])suf[sta.top()]=i-1,sta.pop();
21     sta.push(i);
22     }
23     while(!sta.empty()) suf[sta.top()]=n-1,sta.pop();
24 }
25 
26 void read(){
27     scanf("%d%d",&n,&k);
28     for(int i=0;i<n;i++) scanf("%d",&a[i]);
29     getpre(); getsuf();
30 }
31 
32 int getsize(int len){
33     int p = (len-1)/k;
34     int ans = (1ll*len*p%mod-1ll*(1+p)*p/2%mod*k%mod+mod)%mod;
35     return ans;
36 }
37 
38 void work(){
39     int ans = 0;k--;
40     for(int i=0;i<n;i++){
41     int tot = getsize(suf[i]-pre[i]+1);
42     tot -= getsize(suf[i]-i); tot += mod; tot %= mod;
43     tot -= getsize(i-pre[i]); tot += mod; tot %= mod;
44     ans += 1ll*tot*a[i]%mod; ans %= mod;
45     }
46     printf("%d",ans);
47 }
48 
49 int main(){
50     read();
51     work();
52     return 0;
53 }

 

转载于:https://www.cnblogs.com/Menhera/p/9637224.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值