※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 6138번 문제인 Exploration이다.
문제는 아래 링크를 확인하자.

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

 

6138번: Exploration

Bessie is traveling on a road teeming with interesting landmarks. The road is labeled just like a number line, and Bessie starts at the "origin" (x = 0). A total of N (1 <= N <= 50,000) landmarks are located at points x_1, x_2, ..., x_N  (-100,000 <= x_i

www.acmicpc.net

주어지는 각 랜드마크의 방문순서는 고정되어있으므로, 각 랜드마크들을 방문순서대로 나열한 뒤 직접 이동거리를 구해가며 문제를 해결하자. (문제 조건에 의해 원점으로부터 서로 같은 거리에 있는 랜드마크는 주어지지 않는다는 점을 확인하자.)

 

랜드마크들을 방문순서로 나열하기 위해, 각 원소들의 절댓값을 비교하는 함수 comp를 만들어 정렬을 하자.

 

아래는 제출한 소스코드이다.

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

vector<int> vec;
bool comp(int& x1, int& x2) {
	return abs(x1) < abs(x2);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int T, N; cin >> T >> N;
	while (N--) {
		int x; cin >> x;
		vec.emplace_back(x);
	}

	sort(vec.begin(), vec.end(), comp);
	
	int old = 0, ans = 0;
	for (auto& n : vec) {
		int dist = abs(n - old);
		if (T >= dist) T -= dist, old = n, ans++;
		else break;
	}

	cout << ans;
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 1348 // C++] 주차장  (0) 2022.08.08
[BOJ 15508 // C++] Xayahh-Rakann at Moloco (Easy)  (0) 2022.08.07
[BOJ 12755 // C++] 수면 장애  (0) 2022.08.07
[BOJ 14433 // C++] 한조 대기 중  (0) 2022.08.07
[BOJ 24609 // C++] Overdraft  (0) 2022.08.07

+ Recent posts