UVA 12100 Printer Queue (print queue) simulation _uva

Source: Internet
Author: User
Tags static class

Train of thought: Each task is queued in the order in which it is entered, and the priority is saved in another array, and then sorted in order from large to small, or from little to large, representing the order of each execution. The queue is then manipulated, first from the queue to remove an element, to determine whether the index of the array and the number of equal, if equal, the printing time of +1, while the array subscript +1 or-1, if not equal, then directly to the end of the team, to start the next cycle.

(beginning to understand the wrong idea, that is, the highest priority in the queue to remove the task directly, while ignoring the queue can only one out)


Import Java.util.Arrays;
Import java.util.LinkedList;
Import Java.util.Queue;
Import Java.util.Scanner;

public class Main {public
	static void Main (string[] args) {
		Scanner scan = new Scanner (system.in);
		int t = Scan.nextint ();
		while (T--! =0) {
			int n = scan.nextint ();
			int m = Scan.nextint ();
			queue<num> q = new linkedlist<> ();
			int[] A = new Int[n];
			for (int i=0;i<n;i++) {
				int p = scan.nextint ();
				num num = new num (p,i);
				Q.add (num);
				A[i] = p;
			}
			Arrays.sort (A);
			int j = n-1;
			int cnt = 0;
			Boolean ok = false;
			while (!OK) {
				num num = Q.peek ();
				Q.poll ();
				if (Num.value==a[j]) {
					cnt++;
					j--;
					if (num.pos==m) {
						ok = true;
					}
				} else{
					q.add (num);
				}
				
			}
			SYSTEM.OUT.PRINTLN (CNT);
		}
	
	Static class num{
		int value;
		int pos;
		Public Num (int value,int pos) {
			this.value = value;
			This.pos = pos;
		}
	}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.