Skip to content

251203 : [BOJ 1461] 도서관 - #2221

Merged
esc10946 merged 1 commit into
mainfrom
hojun/2219/1
Jan 12, 2026
Merged

esc10946 merged 1 commit into
mainfrom
hojun/2219/1

Conversation

@esc10946

@esc10946 esc10946 commented Dec 2, 2025 •

Copy link
Copy Markdown
Contributor

🚀 이슈 번호

Resolve: {#2219}

🧩 문제 해결

스스로 해결: ✅

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지 : 그리디, 정렬
  • 🔹 어떤 방식으로 접근했는지 : 해당 문제는 책을 가지고 있을때 가장 멀리있는 목적지 근처에 있는 것이 유리해보였다. 그래서 M개씩 멀리서부터 한 그룹으로 묶어서 저장한 후에 가장 먼 그룹 제외하고 모든 그룹에 * 2를 해서 풀었다.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O( K * n log n)
  • 이유: sort사용이 가장 시간복잡도가 길다

💻 구현 코드

#include <bits/stdc++.h>

using namespace std;

int main() {

	int N, M; cin >> N >> M;

	vector<int> arr(N);
	vector<int> arr1(N);
	vector<int> result;

	for (int i = 0; i < N; ++i) {
		int a; cin >> a;

		if (a > 0) arr[i] = a;
		else arr1[i] = -a;
	}

	sort(arr.begin(), arr.end(), greater<>());
	sort(arr1.begin(), arr1.end(), greater<>());

	for (int i = 0; i < arr.size(); i += M) {
		result.push_back(arr[i]);
	}
	for (int i = 0; i < arr1.size(); i += M) {
		result.push_back(arr1[i]);
	}

	int ans = 0;
	sort(result.begin(), result.end());

	for (int i = 0; i < result.size() - 1; ++i) {
		ans += result[i] * 2;
	}
	ans += result.back();

	cout << ans;

	return 0;
}

@esc10946 esc10946 self-assigned this Dec 2, 2025
@esc10946 esc10946 linked an issue Dec 2, 2025 that may be closed by this pull request
@esc10946
esc10946 merged commit 9f5a160 into main Jan 12, 2026
2 checks passed
@esc10946
esc10946 deleted the hojun/2219/1 branch January 12, 2026 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

251203 : 코딩테스트

1 participant