Gym - 101911K Medians and Partition(dp+思维)

Let median of some array be the number which would stand in the middle of this array if it was sorted beforehand. If the array has even length let median be smallest of of two middle elements. For example, median of the array [10,3,2,3,2][10,3,2,3,2] is 33 (i.e. [2,2,3–,3,10][2,2,3_,3,10]). Median of the array [1,5,8,1][1,5,8,1] is 11 (i.e. [1,1–,5,8][1,1_,5,8]).

Let array be mm-good if its median is greater or equal than mm.

Let the partition of array [a1,a2,…,an][a1,a2,…,an] be a set of subarrays {b1,b2,…,bk}{b1,b2,…,bk} such that b1=[a1,a2,…,ai1]b1=[a1,a2,…,ai1], b2=[ai1+1,ai1+2,…,ai2]b2=[ai1+1,ai1+2,…,ai2], ..., bk=[aik−1+1,aik−1+2,…,an]bk=[aik−1+1,aik−1+2,…,an]. For example, array [10,3,2,3,2][10,3,2,3,2] can be partitioned as follows: {[10,3,2,3,2]}{[10,3,2,3,2]} or {[10],[3],[2],[3],[2]}{[10],[3],[2],[3],[2]}, or {[10],[3,2,3,2]}{[10],[3,2,3,2]}, or {[10,3],[2],[3,2]}{[10,3],[2],[3,2]} and so on.

You are given array aa of length nn and integer mm. Find the partition of aa into maximum number of subarrays such that each subarray is mm-good.

Input

The first line contains two integers nn and mm (1≤n≤50001≤n≤5000, 1≤m≤50001≤m≤5000) — length of array aa and constant mm.

The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤50001≤ai≤5000)— array aa.

Output

If there is no valid partition of array aa into mm-good subarrays, print 00. Otherwise print maximum number of subarrays in partition of array aa such that each subarray is mm-good.

Examples

Input

5 2
10 3 2 3 2

Output

5

Input

5 3
10 3 2 3 2

Output

1

Input

5 4
10 3 2 3 2

Output

0

Note

In the first example array can be partitioned into 55 subarrays: {[10],[3],[2],[3],[2]}{[10],[3],[2],[3],[2]}. Medians of each part greater of equal than 22.

In the second example we can't partition array into several subarrays since medians of [2][2], [3,2][3,2], [2,3,2][2,3,2] and [3,2,3,2][3,2,3,2] are less than 33.

题意:把n个数分成几个连续的部分,每一部分的中位数都比m大,如果为偶数取中间靠前面那一个,求最多可以分成几个部分

题解:dp题,跟求最长上升子序列差不多,简单思维一下就可以啦,难受的是,比赛的时候没做出来,我日~~

上代码:

import java.util.*;
import java.math.*;
public class Main {
	static Scanner cin = new Scanner (System.in);
	static int [] dp = new int [6666];
	static int [] sum = new int [6666];
	public static void main(String[] args) {
		int n,m;
		n=cin.nextInt();m=cin.nextInt();
		for (int i = 1 ; i <= n;i++) {
			int x=cin.nextInt();
			if(x>=m) sum[i]+=sum[i-1]+1;//求前缀和
			else sum[i]=sum[i-1];
		}
		for (int i = 1; i <= n;i++) {
			for (int j = 0; j < i;j++) {
				if(sum[i]-sum[j]>=(i-j)/2+1&&(j==0||dp[j]!=0)) { //&&与后面的判断很重要,给个样例:模拟一下就知道了
					/* 
					 * 输入:
					  6 2
					  2 1 1 2 1 2
					 *输出:
					   0
					 */
					dp[i]=Math.max(dp[i], dp[j]+1);
				}	
			}
		}
		System.out.println(dp[n]);
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值