- 문제 링크 : boj.kr/2513
- 난이도 : G3
- 태그 : 그리디, 정렬
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, k, s;
cin >> n >> k >> s;
vector<pii> arr1, arr2;
for(int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
if(a < s) {
arr1.push_back({s-a, b});
}
else {
arr2.push_back({a-s, b});
}
}
sort(all(arr1), greater<pii>());
sort(all(arr2), greater<pii>());
int ans = 0;
int temp = k;
for(auto u : arr1) {
temp += u.second;
while(temp > k) {
temp -= k;
ans += u.first;
}
}
temp = k;
for(auto u : arr2) {
temp += u.second;
while(temp > k) {
temp -= k;
ans += u.first;
}
}
cout << ans * 2;
return 0;
}
풀이
학교를 기준으로 양쪽으로 나눈다.
가장 먼 아파트부터 인원을 최대한 채워서 돌아오는 것이 최적이다.
728x90
'PS' 카테고리의 다른 글
BOJ 1052 : 물병 (0) | 2024.07.02 |
---|---|
BOJ 24146 : 分数 (Fraction) (0) | 2024.07.01 |
BOJ 13019 : A를 B로 (0) | 2024.06.29 |
BOJ 27923 : 햄버거최대 몇개드실수있나요? (0) | 2024.06.28 |
BOJ 1374 : 강의실 (0) | 2024.06.27 |