Printer Queue (UVA - 12100 )

版权声明:欢迎转载!拒绝抄袭. https://blog.csdn.net/qq_36257146/article/details/87562822
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

struct Node
{
    int x;
    bool vis;
    Node(int a,bool b)
    {
        x = a;vis = b;
    }
};
vector<Node>nodes;

//向前移动
void f(int n)
{
    for(int i = 0;i<n;i++)
    {
        nodes[i] = nodes[i+1];
    }
}

bool big(int x,int n)
{
    for(int i = 0;i<n;i++)
    {
        if(nodes[i].x > x)
            return 0;
    }
    return 1;
}

int main()
{
    int c;
    cin>>c;
    while(c--)
    {
        int n,a,x;
        nodes.clear();
        cin>>n>>a;
        for(int i = 0;i<n;i++)
        {
            cin>>x;
            if(i == a)
                nodes.push_back(Node(x,1));
            else
                nodes.push_back(Node(x,0));
        }
        int sum = 0,flag = 0;
        while(1)
        {
            Node t = nodes[0];
            n--;
            f(n);
            if(big(t.x,n))
            {
                sum++;
                if(t.vis) break;
            }
            else//不是最大的
            {
                n++;
                nodes[n-1] = t;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36257146/article/details/87562822