11#include < string>
22#include < vector>
3- #include < queue>
43#include < iostream>
4+ #include < queue>
55using namespace std ;
66
77int solution141 (vector<int > food_times, long long k) {
8- 8+ queue<pair<int , int >> q;
9+ for (int i = 0 ; i < food_times.size (); ++i)
10+ q.push ({ i + 1 ,food_times[i] });
11+ 12+ while (q.size () != 0 && k > 0 ) { // 남은 음식이 없거나, 시간이 다했을경우 탈출
13+ long long _size = q.size ();
14+ long long sub = k / _size;
15+ k %= _size;
16+ 17+ if (sub == 0 ) // 더 이상 한번에 뺄수 없으면 탈출
18+ break ;
19+ 20+ long long rest = 0 ;
21+ for (int i = 0 ; i < _size; ++i) {
22+ pair<int , int > a = q.front ();
23+ q.pop ();
24+ a.second -= sub;
25+ if (a.second <= 0 ) // 음식을 다먹었다면
26+ rest -= a.second ; // 초과된 시간만큼 저장해두고
27+ else
28+ q.push (a);
29+ }
30+ k += rest;
31+ }
32+ 33+ if (q.empty ()) // 남은 음식이 없으면
34+ return -1 ;
35+ else { // 음식이 있을 때
36+ if (k > 0 ) { // 시간이 남았을 때
37+ while (!q.empty ()) {
38+ --k;
39+ if (k == -1 )
40+ return q.front ().first ;
41+ q.pop ();
42+ }
43+ // return -1;
44+ }
45+ else // 시간이 끝났을 때
46+ return q.front ().first ;
47+ }
948}
1049
1150int main () {
12- cout << solution141 ({ 3 ,1 ,2 }, 5 );
51+ cout << solution141 ({ 1 ,1 ,2 }, 7 );
1352 return 0 ;
1453}
0 commit comments