-DP-贪心- [洛谷 5275]思路题(ak)

题目描述

Link

Solution

这道题的做法就是先按照a的大小排序,然后将此题转换为LIS问题,得用 $O(n \log n)​$ 的算法

Code

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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 100010
struct num {
int a, b, id;
}a[MAXN];
int n, d[MAXN], g[MAXN], top, k[MAXN], len;
bool cmp(num a, num b) {
return a.b > b.b || ((a.b == b.b) && a.a < b.a);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].a);
a[i].id = i;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].b);
}
std::sort(a + 1, a + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
if (a[i].a >= d[top]) d[++top] = a[i].a, g[a[i].id] = top;
else {
int j = std::upper_bound(d + 1, d + 1 + top, a[i].a) - d;
d[j] = a[i].a;
g[a[i].id] = j;
}
}
printf("%d\n", top);
len = top;
for (int i = n; i >= 1; i--) {
if (g[a[i].id] == len){
k[len--] = a[i].id;
}
if (len < 1) {
break;
}
}
for (int i = 1; i <= top; i++) {
printf("%d ", k[i]);
}
}
文章作者: RiverFun
文章链接: https://stevebraveman.github.io/blog/2019/04/02/73/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 RiverFun

评论