The Chef is sleeping now. He tries to cook new kind of meals in his dream.

These meals are arranged in a row and numbered from 1 to N consecutively. For each meal i (1≤i≤N) there 

is given one integer f(i) which denotes the time needed to cook it. Initially, all meals are uncooked. Each assistant 

of The Chef (there are infinite number of them) can help him with cooking.

The abilities of all assistants are same. There can be at most one assistant cooking at each moment. He must choose some 

continuous subsequence of meals with length K(any such subsequence can be chosen). And if there are uncooked meals in 

it, he will cook all uncooked meals which has the minimum cooking time among uncooked meals in the chosen subsequence. 

Nothing done to another meals.

The dream was so interesting that he tried to solve such a problem: What is the minimum number of assistants which can 

cook all the meals assuming that each of them will cook at most once?
But since the bell rings and Chef's friends has come to visit him, he will wake up after 2 seconds. Your program 

should calculate the answer before The Chef will come to himself.

------ Input ------ 

First line of input file contains two integers N (1≤N≤10^{5}) and K (1≤K≤N), 

followed by a line containing N integers. The i^{th} integer denotes f(i)-the cooking time of 

meal number i (1≤f(i)≤10^{9})

------ Output ------ 

Print minimum number of assistans which can cook all the meals in one line.

----- Sample Input 1 ------ 
5 3
40 30 40 30 40
----- Sample Output 1 ------ 
3
----- explanation 1 ------ 
3 assistants are enough to cook all the meals. They can work in following schedule:
1st assistant chooses interval [2,4] and cooks meals 2 and 4.
2nd assistant chooses interval [1,3] and cooks meals 1 and 3.
3rd assistant chooses interval [3,5] and cooks meal 5.
Other schedules can also be possible.

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Greedy algorithms

Constructive algorithms


Solutions:
from sys import stdin, stdout
from collections import defaultdict
input = stdin.readline
(n, k) = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))
d = defaultdict(list)
for i in range(n):
	d[arr[i]].append(i)
ans = 0
for key in d:
	val = d[key]
	val.append(1000000)
	prev = val[0]
	for index in val:
		if index > prev + k - 1:
			ans += 1
			prev = index
print(ans)


(n, K) = map(int, input().split())
a = list(map(int, input().split()))
d = {}
for i in range(n):
	if a[i] not in d:
		d[a[i]] = [i]
	else:
		d[a[i]].append(i)
ans = 0
for i in d:
	h = d[i]
	j = 0
	k = 0
	while j < len(h):
		while k < len(h) and h[k] - h[j] < K:
			k = k + 1
		ans += 1
		j = k
print(ans)


(N, K) = input().split()
N = int(N)
K = int(K)
f = list(map(int, input().split()))
hashmap = {}
count = 0
for i in range(N):
	try:
		hashmap[f[i]].append(i)
	except:
		hashmap[f[i]] = [i]
for i in hashmap:
	h = hashmap[i]
	j = 0
	k = 0
	while j < len(h):
		while k < len(h) and h[k] - h[j] < K:
			k = k + 1
		count = count + 1
		j = k
print(count)