Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
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
Archives
Today
Total
관리 메뉴

차근차근

[백준 2751] 수 정렬하기 2 본문

대학교/Algorithm

[백준 2751] 수 정렬하기 2

SWKo 2020. 2. 13. 13:19

0. 제목

  • 백준 2751 수 정렬하기 2
  • BOJ 2751 수 정렬하기 2
  • C++ 2751 수 정렬하기 2

1. 문제

https://www.acmicpc.net/problem/2751


2. 풀이

  • O(N^2)인 정렬을 사용하면 안된다. 왜냐하면 수의 개수의 범위가 1,000,000까지이기 때문이다.
  • sort는 최대 O(NlogN)이기 때문에 시간내에 정렬이 가능하다.

3. 코드

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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
int main(int argc, const char * argv[]) {
    int N;
    int num;
    vector<int> arr;
    
    cin >> N;
    
    for(int i = 0; i < N; i++){
        cin >> num;
        arr.push_back(num);
    }
    
    sort(arr.begin(), arr.end());
    
    for(int i = 0; i < N; i++)
        cout << arr[i] << '\n';
    
    return 0;
}
 

'대학교 > Algorithm' 카테고리의 다른 글

[백준 1149] RGB거리  (0) 2020.02.13
[백준 11650] 좌표 정렬하기  (0) 2020.02.13
[백준 2667] 단지번호붙이기  (0) 2020.02.13
[백준 9466] 텀 프로젝트  (0) 2020.02.13
[백준 2579] 계단 오르기  (0) 2020.02.09
Comments