๐Ÿฑ์นด์นด์˜ค - ๋ฌด์ง€์˜ ๋จน๋ฐฉ ๋ผ์ด๋ธŒ

๋ฌธ์ œ

์นด์นด์˜ค ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

ํ’€์ด

ํ›„,,,, ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๋ฐ ๋‹ต์„ ๋ณด๊ณ  ํ’€๋ฉด ์•ˆ๋˜๋Š”๋ฐใ… ใ…  ์•„์ง๋„ ์‹ค๋ ฅ์ด ์•ˆ๋˜๊ธด ํ•˜๋‚˜๋ณด๋‹ค.
์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด ์•Œ๊ฒŒ๋œ ๊ฒƒ์€ ์šฐ์„ ์ˆœ์œ„ ํ(priority_queue)์ด๋‹ค.
์ผ๋ฐ˜์ ์ธ ํ๋Š” FIFO์˜ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๋Š”๋ฐ, ์šฐ์„ ์ˆœ์œ„ ํ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ํŒ๋‹จ์— ๋”ฐ๋ผ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋‘๋Š” ๊ฒƒ์ด๋‹ค.
์ด ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ํ์— pair๊ตฌ์กฐ๋ฅผ ์“ฐ๋Š”๋ฐ(๊ฐ ์Œ์‹์„ ๋จน๋Š” ์‹œ๊ฐ„๊ณผ ์ธ๋ฑ์Šค), default ์šฐ์„ ์ˆœ์œ„๋Š” max heap์ด๋ผ ํฐ ๊ฐ’๋ถ€ํ„ฐ ์ •๋ ฌ์„ ํ•ด์ค€๋‹ค(pair์˜ first๋ฅผ ๊ธฐ์ค€).
๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” ๋‘๊ฐ€์ง€๋งŒ ์•Œ๋ฉด ๋œ๋‹ค. less์™€ greater์ด ์žˆ๋Š”๋ฐ less๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ, greater์€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด functional ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ include ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
์ด ๋ฌธ์ œ์—์„œ๋Š” ์Œ์‹๋จน๋Š” ์‹œ๊ฐ„์ด ์งง์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ์„ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ•ด์ฃผ์—ˆ๋‹ค. ๊ตฌ๊ธ€๋ง์„ ํ•˜๋Š”๋ฐ ์‹ ๋ฐ•ํ•˜ ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๋Š”๋ฐ, default ์—ฐ์‚ฐ์ž๋กœ pushํ•ด์ฃผ๋Š”๋ฐ foot_time์— ์Œ์ˆ˜๋ฅผ ๋„ฃ์–ด์„œ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•ด์ฃผ๊ณ  ํ›„์— ๊ณ„์‚ฐํ•  ๋•Œ ๋‹ค์‹œ -๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ์ด๋‹ค.

#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>

using namespace std;
typedef long long ll;

struct compare {
	bool operator()(pair<int, int> a, pair<int, int> b) {
		return a.first < b.first;
	}
};

int solution(vector<int> food_times, long long k) {
	ll sum = 0, before = 0;
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pfood_times;

	for (int i = 0; i < food_times.size(); i++) {
		sum += food_times[i];
		pfood_times.push(make_pair(food_times[i], (i + 1)));
	}

	if (sum <= k)
		return -1;

	while ((pfood_times.top().first - before) * pfood_times.size() <= k) {
    		if (pfood_times.size() == 1)
			return (pfood_times.top().first);
		k -= ((pfood_times.top().first - before) * pfood_times.size());
		before = pfood_times.top().first;
		pfood_times.pop();
	}
	vector<pair<int, int>> time;

	while (!pfood_times.empty()) {
		time.push_back(make_pair(pfood_times.top().second, pfood_times.top().first));
		pfood_times.pop();
	}

	sort(time.begin(), time.end());

	return time[k % time.size()].first;
}

int main()
{
	vector<int> v = { 3,1,2 };
	int n = solution(v, 5);
	cout << n;
}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ