题意 :
- 给一个长为 n 的序列(distinct),将其分为 <= k 个连续的序列,对这些段任意组合,求能否得到递增序列。
思路 :
- 记录在原序列中每个distinct的位置,然后在结果序列中和原序列一一比对。
- 2s,且O(2^n)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <map>
#define endl '\n'
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const double pi = acos(-1);
typedef long long ll;
const int N = 1e5 + 10;
int a[N], b[N];
int main()
{
IOS;
int T;
cin >> T;
while (T -- )
{
int n, k;
cin >> n >> k;
unordered_map<int, int> ma;
for (int i = 1; i <= n; i ++ ) cin >> a[i], b[i] = a[i], ma[a[i]] = i;
sort(b + 1, b + n + 1);
int cnt = 0;
for (int i = 1; i <= n; i ++ )
{
cnt ++ ;
int j = ma[b[i]];
for ( ; j + 1 <= n; j ++ )
if (b[i + 1] == a[j + 1]) i ++ ;
else break;
}
if (cnt <= k) cout << "yes" << endl;
else cout << "no" << endl;
}
return 0;
}