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