1443. Printer Queue

本文介绍两种解决特定问题的方法:一种使用数组模拟队列,实现高效且简洁;另一种利用STL容器与pair,虽然涉及更多知识点但有助于加深对STL的理解。这两种方法都用于处理一个包含排序和队列操作的问题。

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;
//      分支语句啊,分支语句啊,独立的分支


    }
}
Here is the Printer class implementation that meets your requirements: ``` import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; public class Printer { private static Printer instance; private final Queue<String> printQueue; private boolean stateIsRunning; private Printer() { printQueue = new ArrayBlockingQueue<>(5); stateIsRunning = true; } public static synchronized Printer getInstance() { if (instance == null) { instance = new Printer(); } return instance; } public synchronized void addJob(String jobName) throws FullQueueException { if (printQueue.offer(jobName)) { System.out.println("Job " + jobName + " has been added to the print queue."); } else { throw new FullQueueException("Print queue is full. Cannot add job " + jobName); } } private synchronized String getJob() throws EmptyQueueException { String jobName = printQueue.poll(); if (jobName == null) { throw new EmptyQueueException("Print queue is empty."); } return jobName; } public synchronized void halt() { stateIsRunning = false; } public void run() { while (stateIsRunning) { try { String jobName = getJob(); System.out.println("Starting job " + jobName); int pageCount = 10; // Assuming each job has 10 pages for (int i = 1; i <= pageCount; i++) { Thread.sleep(500); // Sleep for 500ms per page } System.out.println("Job " + jobName + " has been completed."); } catch (EmptyQueueException e) { System.out.println("Printer is waiting for a job."); } catch (InterruptedException e) { System.err.println("InterruptedException occurred while processing print job: " + e.getMessage()); } } } } ``` In this implementation, we have a `printQueue` attribute which is an `ArrayBlockingQueue` of size 5. The `addJob` method adds a job to the queue and throws a `FullQueueException` if the queue is already full. The `getJob` method retrieves a job from the queue and throws an `EmptyQueueException` if the queue is empty. The `run` method continuously loops until the printer is halted (`stateIsRunning` is set to false). It retrieves a job from the queue, processes it by sleeping for 500ms per page, and then prints out that the job has been completed. If there are no jobs available, it prints out that the printer is waiting for a job. Note that we have implemented the `Printer` class as a Singleton by adding a private constructor and a `getInstance` method. This ensures that there is only one instance of `Printer` class throughout the application.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值