BOJ : https://www.acmicpc.net/problem/14003
14003번: 가장 긴 증가하는 부분 수열 5
첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에는 수열 A를 이루고 있는 Ai가 주어진다. (-1,000,000,000 ≤ Ai ≤ 1,000,000,000)
www.acmicpc.net
4번 문제와 다른점은 배열의 개수와, 표현범위가 10억이라는 점이다.
4번 : https://junho0956.tistory.com/9?category=825556
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
int lis, n;
long long arr[1000001];
long long Lis[1000001];
vector<pair<int, long long> > pii;
stack<long long> s;
int lower_bound(int start, int end, long long key)
{
int mid;
while (end - start > 0)
{
mid = (start + end) / 2;
if (Lis[mid] < key)
start = mid + 1;
else
end = mid;
}
return end + 1;
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%lld", &arr[i]);
Lis[0] = arr[0];
pii.push_back(make_pair(0, Lis[0]));
for (int i = 1; i < n; i++) {
if (Lis[lis] < arr[i])
{
Lis[lis + 1] = arr[i];
lis++;
pii.push_back(make_pair(lis, arr[i]));
}
else
{
int ans = lower_bound(0, lis, arr[i]);
Lis[ans - 1] = arr[i];
pii.push_back(make_pair(ans - 1, arr[i]));
}
}
printf("%d\n", lis + 1);
for (int i = pii.size(); i >= 0; i--) {
if (pii[i - 1].first == lis) {
s.push(pii[i - 1].second);
lis--;
}
if (lis == -1) break;
}
while (!s.empty()) {
printf("%lld ", s.top()), s.pop();
}
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
'algorithm > BOJ' 카테고리의 다른 글
BOJ 1965번 상자넣기 (0) | 2019.04.07 |
---|---|
BOJ 1937번 욕심쟁이 판다 (0) | 2019.04.06 |
BOJ 14002번 가장 긴 증가하는 부분수열4 (0) | 2019.04.06 |
가장 긴 증가하는 부분수열3(12738) - Junnnho (0) | 2019.04.06 |
가장 긴 증가하는 부분수열2(12015) - Junnnho (0) | 2019.04.06 |