Study with book/Algorithms

[백준]K번째 수


백준저지

[K번째 수]https://www.acmicpc.net/problem/11004

 

C++algorithm STL에 있는 nth_element()함수를 이용해서 문제를 풀었다.

nth_element()k번째 수까지 정렬을 하는 좋은 함수이다.

시간도 메모리도 상대적으로 많이 썼다.

시간이 되면 nth_element함수를 구현해 볼 생각이다.

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

int main(void) {
	int N, K;
	scanf("%d%d", &N, &K);
	vector<long long int> vecArr(N, 0);

	for (int i = 0; i < N; i++) {
		long long int in; scanf("%lld", &in);
		vecArr[i] = in;
	}
	
	nth_element(vecArr.begin(), vecArr.begin() + (K-1), vecArr.end());
	printf("%lld", vecArr[K - 1]);

	return 0;
}

'Study with book > Algorithms' 카테고리의 다른 글

[백준]동전 0  (0) 2017.03.23
[백준]피보나치 함수  (3) 2017.03.11
[백준]이진탐색트리  (2) 2017.01.12
[백준]트리  (2) 2017.01.02
[백준]힙 정렬  (0) 2016.12.30