https://www.acmicpc.net/problem/3745
3745번: 오름세
입력은 여러개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스의 첫째 줄에는 주가를 관찰한 날의 수 N (N ≤ 100000)이 주어진다. 둘째 줄에는 관찰한 주가가 첫 날부터 순서대로 주어진다.
www.acmicpc.net
- lower_bound 사용하기
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
int n;
while (cin >> n) {
vector<int> vec;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
if (vec.empty() || vec.back() < num)
vec.push_back(num);
else {
// vector에서 입력받은 값과 같거나 큰 수 중 가장 작은 수의 위치
int tmp = lower_bound(vec.begin(), vec.end(), num) - vec.begin();
vec[tmp] = num;
}
}
cout << vec.size() << endl;
}
return 0;
}
- 시간초과
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
int n;
while (cin >> n) {
int arr[100001];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int MAX = 1;
int len[100001] = { 0 };
for (int i = 1; i < n; i++) {
len[i] = 1;
for (int j = i - 1; j >= 0; j--) {
if (arr[i] > arr[j])
len[i] = max(len[i], len[j] + 1);
if (len[i] > MAX)
MAX = len[i];
}
}
cout << MAX << "\n";
}
return 0;
}
'APS > 백준' 카테고리의 다른 글
[14716] 현수막 C++ (0) | 2022.03.05 |
---|---|
[10816] 숫자 카드2 C++ (0) | 2022.03.03 |
[17245] 서버실 C++ (0) | 2022.02.21 |
[2110] 공유기 설치 C++ (0) | 2022.02.13 |
[2805] 나무 자르기 C++ (0) | 2022.02.13 |