我是靠谱客的博主 曾经鱼,最近开发中收集的这篇文章主要介绍CodeForces 487B RMQ贪心,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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());
	}

}




最后

以上就是曾经鱼为你收集整理的CodeForces 487B RMQ贪心的全部内容,希望文章能够帮你解决CodeForces 487B RMQ贪心所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部