본문 바로가기

Programmers/효율성 시간초과 모음

[프로그래머스] 무지의 먹방 라이브

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<intint> pp;
 
bool compare(const pp &a, const pp &b) {
    return a.second < b.second;
}
int solution(vector<int> food_times, long long k) {
    int answer = 0;
    
    vector<pp> rounds;
    
    for (int i = 0; i < food_times.size(); i++) {
        rounds.push_back(pp(food_times[i], i+1));
    }
 
    sort(rounds.begin(), rounds.end());
    //오름차순으로 정렬 
    
    int time =0;
    vector<pp>::iterator it;
    
    int size = food_times.size();
    
    for (it = rounds.begin(); it!=rounds.end(); it++size--) {
        long long  nowTimes = (long long)((it->first - time) * size); //정렬된 순서로 가장 짧은 시간을 갖고 있는 음식 부터 음식의 개수만큼 곱해서 한꺼번에 지운다. 
        if (nowTimes==0) {
            //다음 인덱스로 넘어간다. 
            continue;
        }
        if (nowTimes<=k) {
            k-=nowTimes;
            time = it->first;
        }
        else {
            k %= size;
            sort(it, rounds.end(), compare); //인덱스 순서대로 정렬 
            return (it+k)->second;
        }
    }
    return -1 ;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs