【UOJ531】【美團杯2020】最長公共子序列

題目鏈接

點擊打開鏈接

題目解法

詢問長度爲 22 的序列 {x,y}  (xy)\{x,y\}\;(x\ne y) 可以查詢 xx 是否在答案中排在 yy 的前面。
由此,用 std :: stable_sort 或是歸併排序對 11NN 進行排序即可。

時間複雜度 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;
	});
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章