1443. Printer Queue

來源:互聯網
上載者:User

題意:輸入一組資料,每個元素都有優先順序,優先順序從1到9.計算出當目標任務完成後,所需要的時間

思路:如果第一個元素的優先順序不是最高,就將這個元素放到隊列末,如果其優先順序最高,但不是目標任務的話,就將其刪掉,時間就+1,如果是目標任務的話,就輸出時間

#include<stdio.h>#include<queue>#include<stdlib.h>#include<algorithm>using namespace std;struct job{job(int i, int k)//初始化,相當於建構函式{id = i;v = k;}int id, v;};int main(){int a;scanf("%d", &a);while(a--){int n, value;int a[105];queue<job> jobs;scanf("%d %d", &n, &value);for(int i = 0; i < n; i++){int t;scanf("%d", &t);jobs.push(job(i, t));a[i] = t;}sort(a, a + n);int p = n - 1;int cnt = 0;while(true){if(jobs.front().v == a[p]){//判斷隊列頭的任務的等級是否位最高if(jobs.front().id == value){//判斷是否跟指定的位置相同printf("%d\n", ++cnt);break;}else{cnt++;jobs.pop();//將隊列頭刪除p--;}}else{jobs.push(jobs.front());//將隊列頭放到隊尾jobs.pop();}}while(!jobs.empty())jobs.pop();}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.