codeforces 488 D. Strip
来源:互联网 发布:惠州酷友网络 编辑:程序博客网 时间:2024/05/19 20:38
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 21 3 1 2 4 1 2
3
7 2 21 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.
思路:dp+线段树或平衡树优化,其中dp[i]表示以i为结尾的最少可以分成几块
/*======================================================# Author: whai# Last modified: 2015-12-06 10:23# Filename: d.cpp======================================================*/#include <iostream>#include <cstdio>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <set>#include <map>#include <queue>#include <stack>using namespace std;#define LL __int64#define PB push_back#define P pair<int, int>#define X first#define Y secondconst int N = 1e5 + 5;const int INF = 0x3f3f3f3f;int a[N];multiset<int> myset;int to_l[N];bool ok(int s) {return ((*myset.rbegin()) - (*myset.begin()) <= s);}void predo(int n, int s, int l) {myset.clear();myset.insert(a[1]);to_l[1] = 1;int p = 1;for(int i = 2; i <= n; ++i) {myset.insert(a[i]);while(p < i && !ok(s)) {myset.erase(myset.find(a[p]));++p;}to_l[i] = p;}//for(int i = 1; i <= n; ++i) {//cout<<to_l[i]<<' ';//}//cout<<endl;}int dp[N];void gao(int n, int s, int l) {predo(n, s, l);memset(dp, 0x3f, sizeof(dp));myset.clear();int p = 1;for(int i = 1; i <= n; ++i) {int L = to_l[i] - 1, R = i - l;if(R > 0) myset.insert(dp[R]);if(L > R) continue;if(L == 0) {dp[i] = 1;continue;}while(p < L) {myset.erase(myset.find(dp[p]));++p;}int minn = (*myset.begin());if(minn != INF)dp[i] = minn + 1;}//for(int i = 1; i <= n; ++i) {//cout<<dp[i]<<' ';//}//cout<<endl;if(dp[n] == INF) puts("-1");else {cout<<dp[n]<<endl;}}int main() {int n, s, l;while(scanf("%d%d%d", &n, &s, &l) != EOF) {for(int i = 1; i <= n; ++i) {scanf("%d", &a[i]);}gao(n, s, l);}return 0;}
- codeforces 488 D. Strip
- Codeforces 488D Strip【dp+RMQ--------ST】
- Codeforces 488D Strip【dp+RMQ--------ST】
- codeforces 488D Strip 线段树+dp+二分
- Codeforces Round #278 (Div. 2) D. Strip
- CodeForces 487B Strip
- codeforces 487 B. Strip
- CodeForces 487B Strip
- codeforces 487B Strip dp
- 【DP】 codeforces 487B Strip
- Codeforces 487B Strip(RMQ)
- Codeforces Round #278 (Div. 1) B. Strip
- strip
- strip
- strip
- CF #278 div2 D Strip 线段树+优先队列
- Codeforces 487B. Strip DP+线段树+二分
- Codeforces 487b Strip, dp + RMQ(经典)
- 实现u-boot对yaffs/yaffs2文件系统下载的支持
- 数组元素排序
- Scala JDBC连接mysql
- KMP 算法(上篇)
- Ubuntu防火墙设置
- codeforces 488 D. Strip
- 通过查看系统的ProgressBar样式来自定义旋转动画
- h2database
- 框架汇总
- 1214 线段覆盖
- Windows自带截图工具配置快捷键更方便使用
- system.exit(0) system.exit(1) 含义与区别
- HDU 汉诺塔一类问题 1997 2175 2184 2511
- Eclipse快捷键大全(转载)