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

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值