sicily 1443 队列基本操作

1443. Printer Queue

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

The only printer in the computer science students‘ union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer queue and you may have to wait for hours to get a single page of output.

Because some jobs are more important than others, the Hacker General has invented and implemented a simple priority system for the print job queue. Now, each job is assigned a priority between 1 and 9 (with 9 being the highest priority, 
and 1 being the lowest), and the printer operates as follows.

  • The first job J in queue is taken from the queue.
  • If there is some job in the queue with a higher priority than job J, thenmove J to the end of the queue without printing it.
  • Otherwise, print job J (and do not put it back in the queue).

In this way, all those importantmuffin recipes that the Hacker General is printing get printed very quickly. Of course, those annoying term papers that others are printing may have to wait for quite some time to get printed, but that‘s life.

Your problem with the new policy is that it has become quite tricky to determine when your print job will actually be completed. You decide to write a program to figure this out. The program will be given the current queue (as a list of priorities) as well as the position of your job in the queue, and must then calculate how long it will take until your job is printed, assuming that no additional jobs will be added to the queue. To simplifymatters, we assume that printing a job always takes exactly one minute, and that adding and removing jobs from the queue is instantaneous.

Input

One line with a positive integer: the number of test cases (at most 100). Then for each test case:

  • One line with two integers n and m, where n is the number of jobs in the queue (1 ≤ n ≤ 100) and m is the position of your job (0 ≤ m ≤ n −1). The first position in the queue is number 0, the second is number 1, and so on.
  • One linewith n integers in the range 1 to 9, giving the priorities of the jobs in the queue. The first integer gives the priority of the first job, the second integer the priority of the second job, and so on.

Output

For each test case, print one line with a single integer; the number of minutes until your job is completely printed, assuming that no additional print jobs will arrive.

Sample Input

3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1

Sample Output

1
2
5
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

//结构图Paper用于存放原始的输入的每个paper的优先级和位置,不进行排序,实际进行操作
typedef struct paper {
	int priority;
	int position;
} Paper; 

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

int main() {
	int cases;
	cin >> cases;

	while (cases-- > 0) {
		int number;
		int target_p;
		int minute = 0;
		vector<int> papers; //根据优先级进行排序产生的降序优先级队列
		vector<Paper> o_papers; //存放原始的输入的每个paper的优先级和位置,不进行排序,实际模拟打印机操作
		int paper;

		cin >> number >> target_p;

		for (int i = 0; i < number; i++) {
			cin >> paper;
			papers.push_back(paper);

			Paper temp;
			temp.priority = paper;
			temp.position = i;
			o_papers.push_back(temp);
		}

		sort(papers.begin(), papers.end(), cmp);

		for (int i = 0; i < papers.size(); i++) {
			//根据优先级由高到低的顺序打印
			while (o_papers[0].priority != papers[i]) {//寻找优先级最高的paper
				o_papers.push_back(o_papers[0]);
				o_papers.erase(o_papers.begin());
			}
			//找到之后
			if (o_papers[0].position == target_p) {//是目标paper退出循环
				minute++;
				break;
			} else {//不是目标paper,从o_papers队列中删除,即进行了打印操作
				minute++;
				o_papers.erase(o_papers.begin());
			}
		}
		cout << minute << endl;
	}
	return 0;
}

  

时间: 2024-10-14 06:10:21

sicily 1443 队列基本操作的相关文章

循环队列基本操作

/* * 循环队列基本操作. * 少用一个元素空间,约定以"队列头指针在队列尾指针的下一个位置"作为队列满的标志. * "队列头指针等于队列尾指针"作为队列空的标志. */ #include <stdio.h> #include <stdbool.h> #include <malloc.h> #define MAXQSIZE 100 typedef char ElemType; typedef struct { ElemType

队列基本操作—出队与入队

#include<stdio.h> #include<stdlib.h> typedef struct QNode { //构造结点类型 int data; struct QNode *next; }*QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue; void CreateQueue(LinkQueue &Q);//创建队列 void EnQueue(LinkQueue &Q,

C语言 链队列基本操作

C语言链队列基本操作 #include <stdio.h> #include <stdlib.h> #include <malloc.h> /* C语言链队列基本操作 2014年7月11日10:11:41 */ typedef int qType; typedef struct node { qType data; struct node *pNext; }Node,*pNode; typedef struct queue { pNode front; pNode re

链式队列基本操作的实现问题

问题描述:用链式存储方式实现队列的基本操作 涉及变量:front:Node型自定义变量,指向队首元素 rear:Node型自定义变量,指向队尾元素 涉及教材:<数据结构--Java语言描述(第2版)> 清华大学出版社 大致思路: 链式存储结构不害怕出队列会浪费空间,因此也不需要要循环结构来节约空间 front为指向队首结点的指针 rear为指向队尾结点的指针 初始化时它们均指向空 初始化代码如下: 队列的置空方法与初始化相类似 而判断队列是否为空只需要判断队首指针是否指向非空元素即可 代码如下

C++链式队列基本操作

1 #include <iostream> 2 #define QUEUEELEMENTTYPE int 3 using namespace std; 4 5 /*结点*/ 6 typedef struct Node 7 { 8 QUEUEELEMENTTYPE data; /*数据域*/ 9 Node * next; /*指针域*/ 10 }LinkQueueNode; 11 12 /*队列*/ 13 struct LinkQueue 14 { 15 LinkQueueNode * fron

链队列基本操作

#include<stdio.h> #include<Stdlib.h> typedef int elemtype; typedef struct QueueNode { elemtype data; struct QueueNode *next; }LinkedQueueNode; typedef struct LQueue { LinkedQueueNode *front; LinkedQueueNode *rear; }LQueue, * LinkedQueue; Linke

09.循环队列与链队列

一.队列与循环队列 1.队列 (1)队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表.队列是一种先进先出(Fiirst In First Out)的线性表,简称FIFO.允许插入的一端称为队尾,允许删除的一端称为队头. 从队列的定义可知,队列的入队操作,其实就是在队尾追加一个元素,不需要移动任何元素,因此时间复杂度为O(1).队列的删除操作,与栈不同的是,队列元素的出列是在队头,即小标为0的位置,若要删除一个元素的话,需要移动队列的所有元素,因此事件复杂度为O(n).

栈和队列入门

栈 栈是只能在某一端插入和删除的特殊线性表. 用桶堆积物品,先堆进来的压在底下,随后一件一件往上堆.取走时,只能从上面一件一件取.堆和取都在顶部进行,底部一般是不动的. 栈就是一种类似桶堆积物品的数据结构,进行删除和插入的一端称栈顶,另一端称栈底.插入一般称为进栈(PUSH),删除则称为退栈(POP). 栈也称为后进先出表(LIFO表). 栈的基本操作如下: include<stack>//头文件 stack<数据类型>变量名//定义栈 如:stack<int>a; a

C 循环队列实现

一个循环队列的C语言实现,数据类型Queue定义如下,注意在 typedef struct{...}Queue; 中Queue为数据类型,而在struct {...}Queue; 中Queue为一个变量名. front 为队首元素下标,始终指向队首元素,tail 为队尾元素的下一个位置的下标.初始状态为front=tail=0 typedef struct { int size,eleNum; int* array; int front,tail; //front指向第一个元素,rear指向最后