1443. Printer Queue

method 1:
数组模拟队列,优点:效率高,代码简单。

#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 1000;

bool cmp(int a,int b) {
    return a>b;
}

int main() {
    int t;
    cin>>t;
    while (t--) {
        int m,po;
        cin>>m>>po;
        int _sort_data[maxn],data[maxn];
        int _first = 0,_rear = m-1;
        for (int i=0;i<m;i++) {
            cin>>_sort_data[i];
            data[i] = _sort_data[i];
        }
        sort(_sort_data,_sort_data+m,cmp);
        int ans = 0,j=0;
        while (!(_first==po&&data[po]==_sort_data[j])) {
            if (data[_first]==_sort_data[j]) {
                j++;
                ans++;
                _first++;
            } else {

                _rear++;
                data[_rear] = data[_first];
                if (_first == po)  po = _rear;
                _first++;

            }
        }
        cout<<ans+1<<endl;

    }
}

method 2:
stl数组,pair,思想和一完全一样,不过用的知道较多,对于熟悉stl的知识比较有用处。

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int t;
    cin>>t;
    while (t--) {
        int m,po;
        cin>>m>>po;
        vector<pair<int,bool> > data;
        int _max = 0;
        for (int i=0;i<m;i++) {
            pair<int,bool> tmp;
            cin>>tmp.first;
            if (_max<tmp.first) _max = tmp.first;

            tmp.second = ((i==po)?1:0);
            data.push_back(tmp);
        }
        int ans = 0;
        while(!data.empty()) {
            pair<int,bool> tmp = data.front();
            if (tmp.second&&tmp.first==_max) {
                ans++;
                break;
            }
            else if(!tmp.second&&tmp.first==_max) {
                ans++;
                data.erase(data.begin());
                _max = data.front().first;
                for (pair<int,bool> it:data) {
                    if (_max<it.first) _max = it.first;
                }
            }
             else if (tmp.first!=_max) {
                data.push_back(data.front());
                data.erase(data.begin());
            }

        }
        cout<<ans<<endl;
//      分支语句啊,分支语句啊,独立的分支


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值