题目链接:点击这里
题目大意:
给定一个长度为
n
n
n 的序列
a
i
a_i
ai ,你可以执行一个操作:
选定一个区间
[
l
,
r
]
(
长
度
小
于
等
于
k
)
[l,r](长度小于等于 k)
[l,r](长度小于等于k) ,然后让
a
[
l
]
−
=
1
,
a
[
l
+
1
]
−
=
2
,
.
.
.
,
a
[
r
]
−
=
(
r
−
l
+
1
)
a[l]-=1,a[l+1]-=2,...,a[r]-=(r-l+1)
a[l]−=1,a[l+1]−=2,...,a[r]−=(r−l+1) ,即区间减去一个首项为
1
1
1 公差为
1
1
1 的等差数列
求最小操作次数使得所有的
a
[
i
]
≤
0
a[i]\le0
a[i]≤0
题目分析:
众所周知线段树是可以维护等差数列的,例题->点击这里
因为等差数列越往后数列值越大,所以我们需要从后往前看,让每一个扫过的值都作为等差数列的最后一项减到小于等于
0
0
0 即可
具体细节见代码:
// Problem: D. Progressions Covering
// Contest: Codeforces - Educational Codeforces Round 126 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1661/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<unordered_map>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
#define int ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
int res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 1e6+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
struct node{
int val,lazy;
}nod[maxn<<2];
int n,k,a[maxn];
void pushup(int root)
{
nod[root].val = nod[root<<1].val+nod[root<<1|1].val;
}
void build(int root,int l,int r)
{
if(l == r)
{
nod[root].val = a[l];
return ;
}
int mid = l+r>>1;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
pushup(root);
}
void pushdown(int root,int l,int r)
{
int tmp = nod[root].lazy,mid = l+r>>1;
if(tmp)
{
nod[root].lazy = 0;
nod[root<<1].lazy += tmp;
nod[root<<1|1].lazy += tmp;
nod[root<<1].val += tmp*(mid-l+1);
nod[root<<1|1].val += tmp*(r-mid);
}
}
void update(int root,int l,int r,int ql,int qr,int val)
{
if(l>qr || r<ql) return ;
if(l>=ql && r<=qr)
{
nod[root].val += (r-l+1)*val;
nod[root].lazy += val;
return ;
}
pushdown(root,l,r);
int mid = l+r>>1;
update(root<<1,l,mid,ql,qr,val);
update(root<<1|1,mid+1,r,ql,qr,val);
pushup(root);
}
int query(int root,int l,int r,int ql,int qr)
{
if(l>qr || r<ql) return 0;
if(l>=ql && r<=qr) return nod[root].val;
pushdown(root,l,r);
int mid = l+r>>1;
return query(root<<1,l,mid,ql,qr)+query(root<<1|1,mid+1,r,ql,qr);
}
signed main()
{
n = read(),k = read();
for(int i = 1;i <= n;i++) a[i] = read();
for(int i = n;i;i--) a[i] -= a[i-1];
build(1,1,n);
int res = 0;
for(int i = n;i;i--)
{
int cur = query(1,1,n,1,i);
if(cur <= 0) continue;
int l = max(1LL,i-k+1),len = i-l+1;
int cnt = (cur+len-1)/len;
res += cnt;
update(1,1,n,l,l,-cnt);
update(1,1,n,l+1,i,-cnt);
update(1,1,n,i+1,i+1,cnt+cnt*(i-l));
}
cout<<res<<endl;
return 0;
}