6. Greedy
[그리디] 백준 문제풀기 - 11399번 ATM
asdklfjlasdlfkj
2019. 12. 24. 10:43
https://www.acmicpc.net/problem/11399
11399번: ATM
첫째 줄에 사람의 수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄에는 각 사람이 돈을 인출하는데 걸리는 시간 Pi가 주어진다. (1 ≤ Pi ≤ 1,000)
www.acmicpc.net
사람의 순서에 따라 대기하는 시간을 최소로 하는 사람순서배치에 대한 문제였다. 가장 짧게 걸리는 사람부터 세우는게 전체 사람들의 소요 시간을 최소화 하는 방법이라는 그리디 알고리즘으로 풀 수 있다.
#include
#include
#include
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
vector times;
int N;
cin >> N;
for (int i = 0; i < N; i++)
{
int element;
cin >> element;
times.push_back(element);
}
sort(times.begin(), times.end()); // vector, deque만 있음. queue는 없음 (begin, end)
int sum = 0;
for (int i = 1; i <= N; i++){
for (int j = 0; j < i; j++){
sum += times[j];
}
}
cout << sum << '\n';
return 0;
}