답은 알고리즘 뿐이야!

[BOJ 14247] 나무 자르기 본문

알고리즘/백준문제풀이

[BOJ 14247] 나무 자르기

skyde47 2020. 2. 14. 02:41

문제 출처 : https://www.acmicpc.net/problem/14247

 

풀이 :

그리디(탐욕법) 문제입니다.

나무를 최대로 잘라가는 문제입니다.

따라서 나무가 자라는 길이가 최소인 애들이 제일 밸류가 떨어지므로

길이가 최소인 애들부터 잘라서 들고가면 됩니다.

 

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
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<intint> p;
 
int n;
p arr[100000];
priority_queue<p, vector<p>, greater<p>> q;
ll ret;
 
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
 
    cin >> n;
    for (int i = 0; i < n; i++cin >> arr[i].second;
    for (int i = 0; i < n; i++) {
        cin >> arr[i].first;
        q.push(arr[i]);
    }
    for (int i = 0!q.empty();i++) {
        p now = q.top();
        q.pop();
        ret += now.first * i + now.second;
    }
    cout << ret;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

'알고리즘 > 백준문제풀이' 카테고리의 다른 글

[BOJ 2367] 파티  (0) 2020.03.08
[BOJ 13511] 트리와 쿼리 2  (0) 2020.02.19
[BOJ 15671] 오델로  (0) 2020.02.14
[BOJ 3006] 터보소트  (0) 2020.02.11
[BOJ 18111] 마인크래프트  (0) 2020.02.10
Comments