12100 - Printer Queue (UVA)

题目链接如下:

Online Judge

我的代码如下:

#include 
#include 
#include 
#include 
// #define debug

struct job{
    int priority;
    bool flag = false;
    job(int _pri): priority(_pri){}
};
int kase, n, m, k, pivotPriority, cnt;
std::queue q;
int nbr[10];

int main(){
    #ifdef debug
    freopen("0.txt", "r", stdin);
    freopen("1.txt", "w", stdout);
    #endif
    scanf("%d", &kase);
    while (kase--){
        scanf("%d %d", &n, &m);
        std::queue empty;
        std::swap(q, empty);
        std::fill(nbr, nbr + 10, 0);
        for (int i = 0; i < n; ++i){
            scanf("%d", &k);
            nbr[k]++;
            q.push(job(k));
            if (i == m){
                pivotPriority = k;
                q.back().flag = true;
            }
        }
        cnt = 0;
        for (int i = 9; i >= pivotPriority; --i){
            while (nbr[i]){
                if (q.front().priority == i){
                    cnt++;
                    if (q.front().flag){
                        printf("%d\n", cnt);
                        i = -1;
                        break;
                    }
                    q.pop();
                    nbr[i]--;
                } else{
                    q.push(q.front());
                    q.pop();
                }
            }
        }
    }
    #ifdef debug
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

你可能感兴趣的:(UVA,c++)