题目链接
题目解法
询问长度为
2
2
2 的序列
{
x
,
y
}
(
x
≠
y
)
\{x,y\}\;(x\ne y)
{x,y}(x=y) 可以查询
x
x
x 是否在答案中排在
y
y
y 的前面。
由此,用 std :: stable_sort 或是归并排序对
1
1
1 到
N
N
N 进行排序即可。
时间复杂度 O ( N L o g N ) O(NLogN) O(NLogN) 。
#include "lcs.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 5;
void find_permutation(int n, int *res) {
for (int i = 0; i <= n - 1; i++) res[i] = i + 1;
stable_sort(res, res + n, [&] (int x, int y) {
int tmp[2] = {x, y};
return get_lcs(2, tmp) == 2;
});
}