jschang   4년 전

이 문제를 풀때 STL 우선 순위 큐를 사용하였습니다. 문제가 안풀려 https://blog.naver.com/PostView.nhn?blogId=jhc9639&logNo=221771452298&categoryNo=110&parentCategoryNo=0&viewDate=¤tPage=1&postListTopCurrentPage=1&from=search 를 참고하고 작서하였습니다. 아래에 있는 코드로 제출했을 때는 정답이라고 됩니다.

#include <bits/stdc++.h>

using namespace std;

typedef long long ull;

struct cashier {
    int id, maxTime, cashierId;
};

struct comp {
    bool operator() (cashier a, cashier b) {
        if(a.maxTime==b.maxTime) return a.cashierId > b.cashierId;
        return a.maxTime > b.maxTime;
    }
};

bool compV(cashier a, cashier b) {
    if(a.maxTime==b.maxTime) return a.cashierId > b.cashierId;
    return a.maxTime < b.maxTime;
}

int main(int argc, const char * argv[]) {
    int n, cashierNum;
    cin >> n >> cashierNum;
    priority_queue<cashier, vector<cashier>, comp> line;
    vector<cashier> popLine;
    ull ret = 0;
    for (int i=0; i<n; i++) {
        int id, w;
        cin >> id >> w;
        if(i<cashierNum) {
            line.push({id, w, i+1});
            continue;
        }
        line.push({id, w+line.top().maxTime, line.top().cashierId});
        popLine.push_back(line.top());
        line.pop();
    }
    while (line.size()) {
        popLine.push_back(line.top());
        line.pop();
    }
    sort(popLine.begin(), popLine.end(), compV);
    for (int i=0; i<popLine.size(); i++) {
        ret += (ull)((i+1)*(ull)popLine[i].id);
    }
    cout << ret << "\n";
    return 0;
}

보시는 것과 같이 정답인 코드의 42번째 줄을 !line.empty()로 바꾸게 되면 틀렸습니다라고 됩니다. 혹시 두개의 차이가 무엇인지 알려주실 수 있으지요. 

틀렸습니다로 되는 코드도 첨부합니다.

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;

struct cashier {
    int id, maxTime, cashierId;
};

struct comp {
    bool operator() (cashier a, cashier b) {
        if(a.maxTime==b.maxTime) return a.cashierId > b.cashierId;
        return a.maxTime > b.maxTime;
    }
};

bool compV(cashier a, cashier b) {
    if(a.maxTime==b.maxTime) return a.cashierId > b.cashierId;
    return a.maxTime < b.maxTime;
}

int main(int argc, const char * argv[]) {
    int n, cashierNum;
    cin >> n >> cashierNum;
    priority_queue<cashier, vector<cashier>, comp> line;
    vector<cashier> popLine;
    ull ret = 0;
    for (int i=0; i<n; i++) {
        int id, w;
        cin >> id >> w;
        if(i<cashierNum) {
            line.push({id, w, i+1});
            continue;
        }
        line.push({id, w+line.top().maxTime, line.top().cashierId});
        popLine.push_back(line.top());
        line.pop();
    }
    while (!line.empty()) { // 이 줄만 수정했습니다.
        popLine.push_back(line.top());
        line.pop();
    }
    sort(popLine.begin(), popLine.end(), compV);
    for (int i=0; i<n; i++) {
        ret += (ull)((i+1)*popLine[i].id);
    }
    cout << ret << "\n";
    return 0;
}

감사합니다!! 

kyo20111   4년 전

위 코드에서 정말 그 부분만 !line.empty() 로 수정했을때는 AC를 받습니다.

jschang   4년 전

그렇네요. 잘 확인하지 않은 제가 부끄럽네요... 답변 감사합니다!!

댓글을 작성하려면 로그인해야 합니다.