P1020 导弹拦截 学习笔记
原来一道题的满分还不一定是一百分吗。。。
题目翻译:求一个序列的最长下降子序列,以及至少要几个互不相交的单调递减的子序列才能组成这个序列。
思路:
-
首先第一问十分简单,打一版判断条件反过来的
O(nlog_2n) 的LCS 算法就好了,难点在于第二问。 -
那怎么办呢?于是我们对样例进行了细细推导,有如下发现:
如果第二问答案为1,说明原序列是单调递减的,也就是说,最长上升子序列长度为1; 如果答案为2,说明原序列至少有一个元素不与其他元素满足单调递减关系,也就是说,最长上升子序列长度为2; 如果答案为3,说明原序列最长上升子序列长度能且仅能为3; 如果答案为4,说明原序列最长上升子序列长度为4; ……于是得出结论,一个序列由多少个互不相交的单调递减的子序列组成,取决于其最长上升子序列长度,所以第二问的答案可以直接打一版
LCS 求出。
总结:关键在于推导出第二问的逻辑
Code:
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int n, x, tot, a[N], st[N];
int fd(int i, bool fl) {
int l = 1, r = tot;
while (l <= r) {
int mid = l + r >> 1;
if ((st[mid] < a[i] && fl) || (st[mid] >= a[i]^fl)) r = mid - 1;
else l = mid + 1;
}
return l;
}
signed main() {
while (cin >> x) a[++n] = x;
for (int i = 1, k = 1; i <= n; i++) {
while (k <= tot && st[k] >= a[i]) k++;
(k > tot) ? st[++tot] = a[i] : st[fd(i, true)] = a[i];
}
cout << tot << '\n', tot = 0;
for (int i = 1, k = 1; i <= n; i++) {
while (k <= tot && st[k] < a[i]) k++;
(k > tot) ? st[++tot] = a[i] : st[fd(i, false)] = a[i];
}
cout << tot;
return 0;
}