알고리즘/백준

[백준/c++] 9237번: 이장님 초대

녕이 2022. 7. 9. 18:11
728x90

 

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

 

9237번: 이장님 초대

입력은 두 줄로 이루어져 있다. 첫째 줄에는 묘목의 수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄에는 각 나무가 다 자라는데 며칠이 걸리는지를 나타낸 ti가 주어진다. (1 ≤ ti ≤ 1,000,000)

www.acmicpc.net

 

나무 N개, 하나 심는 데 걸리는 시간 1일

마지막 나무가 다 자라고 다음날 이장님 초대

며칠에 초대 가능?

 

내림차순으로 정렬하고, 나무들을 쭉 돌면서 최댓값을 구하면 된다.

나무가 자라는데 걸리는 시간 + 시작한 날

 

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

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, cnt=0;
    cin >> n;
    vector<int> tree;
    for(int i=0; i<n; i++){
        int t;
        cin >> t;
        tree.push_back(t);
    }
    sort(tree.begin(), tree.end(), greater());
    for(int i=0; i<n; i++) cnt = max(cnt, tree[i]+i+1);
    cout << cnt+1 << '\n';
    return 0;
}

 

 

 

 

 

 

728x90