Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- AVL 시간 복잡도
- 백준
- 자료구조
- 구현
- 1764
- heap
- 백준 17822
- qorwns
- 게리멘더링2
- 백준 2447
- 백준 5397
- c#
- Stack 이란
- 백준 1158
- 버킷 정렬
- dfs
- 백준 17471
- 해시구현
- 5397
- 백준 1406
- 해시 구현
- 백준 17779
- 풀이
- 별 찍기 10
- 원판 돌리기
- 시간 복잡도
- 스택의 특징
- C/C++ 구현
- 조세퍼스 순열
- ㅣ풀이
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 2230] 수 고르기 본문
분류 : 정렬
요구사항
두 수를 골랏을때 차이가 m이상인 두 수중 차이가 최소인 값 구하기
풀이
1. 정렬한다
2. low ,high를 잡아 조건대로 계산한다
3. m이상일 경우 low 증가, 아닐경우 high 증가
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
int n;
long long m;
vector<int> v;
long long ans = 20000000001;
int main() {
//cout << ans << endl;
scanf("%d%lld", &n, &m);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
v.push_back(x);
}
sort(v.begin(), v.end());
int low = 0;
int high = 0;
int flag = 0;
while (low<n && high<n) {
long long diff = abs(v[high] - v[low]);
if (diff >= m) {
ans = min(ans, diff);
if (high <v.size())
low++;
}
else {
if (low < v.size())
high++;
}
}
//cout << "!" << endl;
cout << ans << endl;
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 2022] 사다리 (0) | 2020.04.04 |
---|---|
[백준 2406] 안정적인 네트워크 (0) | 2020.03.31 |
[백준 2012] 등수 매기기 (0) | 2020.03.30 |
[백준 1063] 킹 (0) | 2020.03.30 |
[백준 11812] K진 트리 (0) | 2020.03.30 |
Comments