数据结构 two-pointers 985E

先给个题目地址:https://codeforces.com/problemset/problem/985/E

题目大意:

给出一个数列a,要求将数列a中的元素分成若干个部分

要求:

1.每个部分的最大元素最多比最小元素大d

2.每个部分最少有k个元素

3.每个元素都恰好属于某一部分

求有无这样的划分

1 ≤ k ≤ n ≤ 5·1e5, 0 ≤ d ≤ 1e9,1 ≤ ai ≤ 1e9

思考:

当然第一步是排个序了,所以划分就相当于是划分成子数列了

然后嘛:

考虑到以第i个元素结尾的情况,

它的右边界是i,左边界是在[x,i-k+1]内,x是满足a[i]-a[x]<=d的最大x

也就是说,只有前一个子数列是以[x-1, (i-k+1 -1)] 结尾,那才有可能是以i结尾

因此dp[i]=( \sum_{j=x-1}^{i-k}dp[j] ) && 1

 

所以可以说单点的值影响了区间(后面的)的值,或者单点的值收到区间(前面的)的影响

于是需要单点修改,区间查询

这个时候愚笨的我第一想到的的就是线段树:

就知道线段树,这个时候要介绍一种数据结构?了:two - pointers

这玩意可以分两种:

1.单点修改,区间查询:

    其实这就是前缀和,单点修改就是修改原数组中的元素,

    需要区间查询时,就需要计算对应的前缀和,用L[j]-L[i-1]计算[i,j]内的和

2.区间修改,单点查询

    然而这玩意还是前缀和,对于每一个区间修改{le,ri,inc}

    就a[le]+=inc,a[ri+1]-=inc;

    然后计算前缀和,

    得出前缀和L[i]就是经过区间修改后第i个元素的值

 

然而:这玩意不是一个强的数据结构了,

要求(限制):

1.离线或顺序

    先做完全部修改,再查询

    或者像本题一样,修改的点是有序的,并且只查询修改过了的点(咋感觉有点像生产者-消费者问题捏)

2.没了

 

最后给个代码:

//Problem:
//Date:
//Skill:
//Bug:
/Definations/
//循环控制
#define CLR(a) memset((a),0,sizeof(a))
#define F(i,a,b) for(int i=a;i<=int(b);++i)
#define F2(i,a,b) for(int i=a;i>=int(b);--i)
#define RE(i,n)  for(int i=0;i<int(n);i++)
#define RE2(i,n) for(int i=1;i<=int(n);i++)
//输入输出
#define PII pair<int,int>
#define PB push_back
#define x first
#define y second
using namespace std;
const int inf = 0x3f3f3f3f;
const long long llinf = 0x3f3f3f3f3f3f3f3f;
Options//
typedef long long ll;
#define stdcpph
#define CPP_IO

#ifdef stdcpph
#include<bits/stdc++.h>
#else
#include<ctype.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<algorithm>
#include<functional>
#ifdef CPP_IO
#include<iostream>
#include<iomanip>
#include<string>
#else
#include<stdio.h>
#endif
#endif
Basic Functions//
template<typename INint>
inline void IN(INint &x)
{
	x = 0; int f = 1; char c; cin.get(c);
	while (c<'0' || c>'9') { if (c == '-')f = -1; cin.get(c); }
	while (c >= '0'&&c <= '9') { x = x * 10 + c - '0'; cin.get(c); }
	x *= f;
}
template<typename INint>
inline void OUT(INint x)
{
	if (x > 9)OUT(x / 10); cout.put(x % 10 + '0');
}
Added Functions//
const int maxn = int(5e5+4);
int a[maxn];
ll L[maxn];
int dp[maxn];//以i结尾
int n, k, d;
ll g(int x)
{
	return x == -1 ?  0 : L[x];
}
ll s(int x, int y)
{
	if (x > y)return 0;
	else return g(y) - g(x - 1);
}
Code/
int main()
{
	//freopen("C:\\Users\\VULCAN\\Desktop\\data.in", "r", stdin);
	int T(1), times(0);
#ifdef CPP_IO// CPP_IO
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//cin >> T;
#else
	//IN(T);
#endif 
	/
	while (++times, T--)
	{
		cin >> n >> k >> d;
		RE2(i, n)cin >> a[i];
		sort(a + 1, a + 1 + n);
		dp[0] = 1; L[0] = 1;
		//int r = 0;
		int le = 1;
		RE2(i, n)
		{
			while (a[i]-a[le]>d)++le;//r最大为i-1
			if(s(le-1,i-k))
			{
				dp[i] = 1;
			}
			L[i] = L[i - 1] + dp[i];
		}
		cout << (dp[n] ?  "YES" : "NO") << endl;
	}
	///
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值