Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.
Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:
- Each piece should contain at least l numbers.
- The difference between the maximal and the minimal number on the piece should be at most s.
Please help Alexandra to find the minimal number of pieces meeting the condition above.
The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105).
The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
7 2 2 1 3 1 2 4 1 2
3
7 2 2 1 100 1 100 1 100 1
-1
For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].
For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists.
题意是给出一个长度为n的序列,问最少能够分割多少分。
使得,每一分的长度大于等于l,最大值与最少值的差值最大为s 。
我的方法是3颗线段树,2颗维护最大最小值, 1棵维护 dp[i-1]+1 最小的位置。
然后二分出尽量左的位置使得最大最小值差值最大不超过s ,
然后从这个位置到当前位置取出 dp[i-1]+1 最小的位置。然后再更新。
#include#include #include #include #include #include #include #include
之前一直不会set...
在cf上看别人的代码,被完爆码量。
#include#define X first#define Y second#define INF 1000000009using namespace std;typedef pair pii;int n, s, l, a[100005];int dp[100005];set S, R;int main(){ scanf("%d %d %d", &n, &s, &l); for(int i = 1; i <= n; i ++) scanf("%d", &a[i]); memset(dp, 0, sizeof dp); int j = 1; for(int i = 1; i <= n; i ++){ S.insert(pii(a[i], i)); while(!S.empty() && S.rbegin()->X - S.begin()->X > s){ S.erase(pii(a[j], j)); j ++; } if(i >= l && dp[i - l] != -1) R.insert(pii(dp[i - l], i - l)); while(!R.empty() && R.begin()->Y < j - 1) R.erase(R.begin()); if(R.empty()) dp[i] = -1; else dp[i] = R.begin()->X + 1; } printf("%d\n", dp[n]); return 0;}