CodeForces 1661D Progressions Covering(思维,线段树)

题目链接:点击这里

题目大意:
给定一个长度为 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]=(rl+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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值