Problem Link - https://www.codechef.com/INOIPRAC/problems/INOI1902

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
#define MAXN 3010
#define MAXI 100100
#define INF 1e9
#define WL(t) while(t--)
#define FOR(i,s,e) for(int i=s; i<=e; i++)
int n, l, ans;
int a[MAXN], memo[MAXN][MAXI];
class fastio {
public:
fastio() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
}
} __fastio;
int f(int i, int p) {
if(p > l) return INF;
if(memo[i][p] != -1) return memo[i][p];
else {
int ans = -INF;
FOR(k,1,n) {
if(a[i] >= a[k]) {
ans = max(ans, (min(f(k,p+1), abs(i-k))));
}
}
return memo[i][p] = ans;
}
}
void solve() {
cin >> n >> l;
FOR(i,1,n) {
cin >> a[i];
}
int maxi = -INF;
memset(memo, -1, sizeof(memo));
FOR(i,1,n) {
maxi = max(maxi, f(i,1));
}
cout << maxi << endl;
}
int main() {
__fastio;
int t; cin >> t;
WL(t) {
solve();
}
return 0;
}