poj 3125 Printer Queue

 水一水

poj链接:http://poj.org/problem?id=3125

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

const int maxn = 110;
int que[maxn];
int used[maxn];
int cnt[10];

int main()
{
    //freopen("in.txt", "r", stdin);

    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n, m;
        scanf("%d%d", &n, &m);

        memset(used, 0, sizeof(used));
        memset(cnt, 0, sizeof(cnt));

        for(int i = 0; i < n; i++)
        {
            scanf("%d", &que[i]);
            cnt[que[i]]++;
        }

        int ans = 0;
        int pt = 0;
        bool found = false;

        for(int k = 9; k >= 1 && !found; k--)
        {
            for(int i = 0; i < cnt[k]; i++)
            {
                while(used[pt] == 1 || que[pt] != k)
                {
                    pt++;
                    pt %= n;
                }
                used[pt] = 1;
                ans++;

                if(pt == m)
                {
                    found = true;
                    break;
                }

            }
        }

        printf("%d
", ans);

    }

    return 0;
}
原文地址:https://www.cnblogs.com/dishu/p/4263884.html