CodeForces 487B RMQ贪心
来源:互联网 发布:filezilla中软件怎么下 编辑:程序博客网 时间:2024/06/16 07:07
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.
点击打开链接
import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.util.Arrays;import java.util.StringTokenizer;public class Main {public static void main(String[] args) {new Task().solve();}}class Task {InputReader in = new InputReader(System.in);PrintWriter out = new PrintWriter(System.out);final int MAXN = 100010;int n ;int[][] cmax = new int[MAXN][20];int[][] cmin = new int[MAXN][20];int maxx, minx;int[] a = new int[MAXN];int[] tlog = new int[MAXN];{tlog[0] = tlog[1] = 0;for (int i = 2; i < MAXN; i++)tlog[i] = tlog[i >> 1] + 1;}void RMQ() {for (int i = 1; i <= n; i++)cmax[i][0] = cmin[i][0] = a[i];for (int j = 1; (1 << j) <= n; j++) {for (int i = 1; i + (1 << j) - 1 <= n; i++) {cmax[i][j] = Math.max(cmax[i][j - 1] , cmax[i + (1 << (j - 1))][j - 1]);cmin[i][j] = Math.min(cmin[i][j - 1] , cmin[i + (1 << (j - 1))][j - 1]);}}}int ask(int l, int r) {int i = tlog[r - l + 1];return Math.max(cmax[l][i], cmax[r - (1 << i) + 1][i]) - Math.min(cmin[l][i], cmin[r - (1 << i) + 1][i]) ;}int[] dp = new int[MAXN] ;void solve() {n = in.nextInt() ;int s = in.nextInt() ;int l = in.nextInt() ; for(int i = 1 ; i <= n ; i++) a[i] = in.nextInt() ; RMQ() ; Arrays.fill(dp , 0 , n+1 , Integer.MAX_VALUE) ;int partition = 0 ; dp[0] = 0 ;for(int i = l ; i <= n ; i++){while(dp[partition] == Integer.MAX_VALUE ||(i - partition >= l && ask(partition+1 , i) > s)) partition++ ;if(i - partition >= l && dp[partition] != Integer.MAX_VALUE)dp[i] = dp[partition] + 1 ;}out.println(dp[n] == Integer.MAX_VALUE ? -1 : dp[n]) ;out.flush() ; }}class InputReader {public BufferedReader reader;public StringTokenizer tokenizer;public InputReader(InputStream stream) {reader = new BufferedReader(new InputStreamReader(stream), 32768);tokenizer = new StringTokenizer("");}private void eat(String s) {tokenizer = new StringTokenizer(s);}public String nextLine() {try {return reader.readLine();} catch (Exception e) {return null;}}public boolean hasNext() {while (!tokenizer.hasMoreTokens()) {String s = nextLine();if (s == null)return false;eat(s);}return true;}public String next() {hasNext();return tokenizer.nextToken();}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}public double nextDouble() {return Double.parseDouble(next());}public BigInteger nextBigInteger() {return new BigInteger(next());}}
0 0
- CodeForces 487B RMQ贪心
- Codeforces 487B Strip(RMQ)
- codeforces 487B B. Strip(rmq+线段树+二分)
- Codeforces 487b Strip, dp + RMQ(经典)
- codeforces 332B B. Maximum Absurdity(rmq)
- codeforces 3B (贪心)
- codeforces 540B-贪心
- codeforces 604B (贪心)
- Codeforces 682B贪心
- CodeForces - 762B(贪心)
- 贪心-Codeforces 777B
- Codeforces 191B(贪心)
- Codeforces 700B 贪心
- codeforces 3B Lorry 贪心
- Codeforces 3B Lorry 贪心
- CodeForces 287B 二分贪心
- Codeforces 364B 背包+贪心
- CodeForceS#276-B(贪心)
- 求最大面积
- 是时候客观评价Retrofit了,Retrofit这几点你必须明白!
- 关于SSH登录非常慢的案例
- C#窗体程序入门,实现3秒倒计时关闭显示器,实现系统托盘,右键菜单。
- oracle实施问题Could not get a databaseId from dataSource
- CodeForces 487B RMQ贪心
- http协议的再介绍
- 大型分布式网站架构技术总结
- css3中的content字符编码
- QGraphicsView用法总结
- vim 常用命令
- Linux下mysql中文乱码
- jquery live()调用不存在的解决方法
- bzoj 2788: [Poi2012]Festival (差分约束+最短路+tarjan)