Problem:
Score : 700 points

Problem Statement

Snuke has a permutation (P_0,P_1,\cdots,P_{N-1}) of (0,1,\cdots,N-1).

Now, he will perform the following operation exactly once:

Choose K consecutive elements in P and sort them in ascending order.

Find the number of permutations that can be produced as P after the operation.

Constraints

2 \leq N \leq 200000

2 \leq K \leq N

0 \leq P_i \leq N-1

P_0,P_1,\cdots,P_{N-1} are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_0 P_1 \cdots P_{N-1}

Output

Print the number of permutations that can be produced as P after the operation.

Sample Input 1

5 3
0 2 1 4 3

Sample Output 1

2

Two permutations can be produced as P after the operation: (0,1,2,4,3) and (0,2,1,3,4).

Sample Input 2

4 4
0 1 2 3

Sample Output 2

1

Sample Input 3

10 4
2 0 1 3 7 5 4 6 8 9

Sample Output 3

6

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#define FNAME ""

#undef __STRICT_ANSI__

#include <bits/stdc++.h> 

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define fst first
#define snd second
#define sz(x) (int)((x).size()) 
#define forn(i,n) for (int i = 0; (i) < (n); ++i)
#define fornr(i,n) for (int i = (int)(n) - 1; (i) >= 0; --i)
#define forab(i,a,b) for (int i = (a); (i) < (b); ++i)
#define forba(i,a,b) for (int i = (int)(b) - 1; (i) >= (a); --i)
#define forit(it,c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(),(c).end()

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) static_cast<void>(0)   
#endif

#ifdef _WIN32
	#define I64 "%I64d"
	#define U64 "%I64u"
#else
	#define I64 "%lld"
	#define U64 "%llu"
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef unsigned int uint;
typedef vector <int> vi;
typedef pair <int, int> pii;

const int N = 2e5 + 100;

int p[N], rightMin[N], leftMax[N], isSorted[N];
int p2[N];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, k;
	cin >> n >> k;
	forn(i, n) {
		cin >> p[i];
	}
	stack<pii> s;
	s.push(mp(p[0], 0));
	forn(i, n) {
		leftMax[0] = -1;
	}
	forab(i, 1, n) {
		while(!s.empty() && s.top().fst < p[i]) {
			s.pop();
		}
		if (!s.empty()) {
			leftMax[i] = s.top().snd;
		}
		s.push(mp(p[i], i));
	}
	forn(i, n) {
		rightMin[i] = n;
	}
	stack<pii> s2;
	s2.push(mp(p[n - 1], n - 1));
	fornr(i, n - 1) {
		while(!s2.empty() && s2.top().fst > p[i]) {
			s2.pop();
		}
		if (!s2.empty()) {
			rightMin[i] = s2.top().snd;
		}
		s2.push(mp(p[i], i));
	}
	int ans = 1, cntSorted = 0;
	for (int i = 0; i + k < n; i++) {
		if (rightMin[i] < i + k || leftMax[i + k] > i) {
			ans++;
		}
	}
	forn(i, n - 1) {
		if (p[i] < p[i + 1]) {
			isSorted[i] = 1;
		}
	}
	int sortedElements = 0;
	forn(i, k - 1) {
		sortedElements += isSorted[i];	
	}
	if (sortedElements == k - 1) {
		cntSorted++;
	}
	for (int i = 0; i + k < n; i++) {
		sortedElements -= isSorted[i];
		sortedElements += isSorted[i + k - 1];
		if (sortedElements == k - 1) {
			cntSorted++;
		}
	}
	cout << ans - max(0, cntSorted - 1) << '\n';
	return 0;
}