UVa 12100 打印队列

来源:互联网 发布:沙特购买中国武器知乎 编辑:程序博客网 时间:2024/05/18 01:58
分析:这个题可以说是个水题吧,但我感觉这样的题用STL简直是大材小用了,用一个数组模拟一个队列即可。
这个题的小技巧就在于跟踪要打印任务的下标。

代码如下:
#include <cstdio>
int a[5005],n,m;
int panduan(int t,int r)
{
    for(int i=t+1;i<r+1;i++)
        if (a[i]>a[t]) return 1;//检查是该输出还是该扔了后面去
    return 0;
}
int main()
{
    int tot;
    scanf("%d",&tot);
    while (tot--)
    {
        scanf("%d%d",&n,&m);
        for (int i=0;i<n;i++)
            scanf("%d",&a[i]);
        int ans=0;
        int f=0,r=n-1;
        while (1)
        {
            if (panduan(f,r))
            {
                a[++r]=a[f];
                if (f==m) m=r;//跟踪下标
                f++;
            }
            else
            {
                if (m==f) {printf("%d\n",ans+1);break;}
                ans++;f++;
            }
        }
    }
    return 0;
}
0 0
原创粉丝点击