[Greedy] 2217번 - 로프(33일차)

bob.sort·2021년 6월 22일
0
post-thumbnail
#include <iostream>
//memset 쓰기 위한 cstring
#include <cstring>
//sort 쓰기 위한 algorithm
#include <algorithm>
using namespace std;

int main(){
    //로프의 개수 입력
    int n;
    cin >> n;
    //로프의 개수만큼 로프 중량값 저장 1차원 배열 동적 할당 후 0으로 초기화
    int *rope = new int[n];
    memset(rope, 0, sizeof(n));
    //로프 중량값 입력
    for(int i=0; i<n; i++){
        cin >> rope[i];
    }
    //사용된 로프의 개수를 세는 변수
    int rope_cnt = 1;
    //중량 최대값 저장 변수
    int weight = 0;
    //로프들을 중량값 기준으로 내림차순 정렬
    sort(rope, rope+n, greater<int>());
    //모든 로프에 대해서
    for(int j=0; j<n; j++){
        //해당 로프 중량 최대값 x 총 로프 개수가 중량 최대값보다 크면 중량값 업데이트
        if(rope_cnt * rope[j] >= weight){
            weight = rope_cnt * rope[j];
        }
        //사용된 로프 개수 추가
        rope_cnt++;
    }
    //저장된 중량 최대값 출력
    cout << weight;
}
profile
Interest in Computer Graphics and Computer Vision

0개의 댓글