코딩 테스트/알고리즘 풀이
[백준 - 2217] 로프
김띠띵
2022. 7. 23. 03:52
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
int main()
{
int N;
cin >> N;
vector<int> ropeList;
for (int i = 0; i < N; ++i)
{
int rope = 0;
scanf("%d", &rope);
ropeList.push_back(rope);
}
sort(ropeList.begin(), ropeList.end(), greater<>());
int maxW = 0;
for (int i = 0; i < ropeList.size(); ++i)
{
int temp = ropeList[i] * (i + 1);
if (maxW <= temp) maxW = temp;
}
cout << maxW;
return 0;
}
분명 맞다고 생각했는데 여러번 틀렸다
틀린 부분은 저 마지막 for문에서 최대값 구하는 부분인데
이전 코드에서는 최대값이 아니면 바로 끝냈기 때문.
"5 1 1 1 3 1" 같은 입력값을 예로 들면
먼저 내림차순을 한다. 3 1 1 1 1 (5는 로프 개수 이므로 제외)
3 최대 3
1 최대 2
나는 여기서 멈추었었는데 끝까지 가보면
1 최대 3
1 최대 4
1 최대 5
으로 최대 중량 5를 구할 수 있다.