[BOJ] 2217번 : 로프

2024. 9. 6. 14:07Algorithm

1. problem : 

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

 

2. solution 1 :

#include <bits/stdc++.h>
using namespace std;
int n;
int weights[100005]; 
int d[100005];

int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0); 
	cin >> n; 
	for (int i = 0; i < n; i++) cin >> weights[i]; 
	sort(weights, weights + n); 
	for (int i = 0; i < n; i++) {
		d[i] = weights[i] * (n-i);
	}
	cout << *max_element(d, d + n) << '\n';
}

weights를 sort 해서 오름차순으로 정렬한 다음, 범위를 하나씩 좁혀가면서 무게의 분산을 고려해 최대 무게를 구했다. 

greedy 알고리즘을 이용했다. 

 

'Algorithm' 카테고리의 다른 글

[BOJ] 12865번 : 평범한 배낭  (0) 2024.09.06
[BOJ] 1026번 : 보물  (0) 2024.09.06
[BOJ] 1931번 : 회의실 배정  (0) 2024.09.06
[BOJ] 11047번 : 동전 0  (3) 2024.09.06
[BOJ] 2011번 : 암호코드  (0) 2024.09.05