首页 > 代码库 > UvaLive 4254 Processor 优先队列

UvaLive 4254 Processor 优先队列

题目链接:点击打开链接

除了Integer, String等,其他(即对象)都是引用。。(就是地址,想要和C一样的效果要新建一个对象)



import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.Queue;

public class Main {
	public class Node implements Comparable<Node>{
		int l, r, w;
		Node(){}
		Node(int l, int r, int w){
			this.l = l;
			this.r = r;
			this.w = w;
		}
		public int compareTo(Node o){
			return Integer.compare(r, o.r);
		}
		public Node Clone(){
			return new Node(l,r,w);
		}
	}
	Comparator<Node> cmp = new Comparator<Node>(){
		public int compare(Node x, Node y){
			return Integer.compare(x.l, y.l);
		}
	};
	PriorityQueue<Node> q = new PriorityQueue();
	Node[] a = new Node[N];
	static int N = 10010;
	int n;
	boolean ok(int mid){
		int i = 0, j = 0;
		q.clear();
		while(true){
		    while(i<n&&a[i].l<=j) q.offer(a[i++].Clone());  
		    int now=mid;  
		    while(now!=0&&!q.isEmpty())  
		    {  
		    	
		        Node ita= q.poll().Clone();  
		        int m=min(now,ita.w);  
		        now-=m;  
		        ita.w-=m;  
		        if(ita.w!=0)  {
		        	q.offer(ita);
		        }
		    }  
		    j++;  
		    if(!q.isEmpty()&&q.peek().r<=j)  
		        return false;  
		    if(q.isEmpty()&&i==n)  
		        return true;  
		   
		}
	}
	void work() {
		int T = cin.nextInt();
		while(T-- > 0){
			n = cin.nextInt();
			for(int i = 0, l, r, w; i < n; i++)
			{
				l = cin.nextInt(); r = cin.nextInt(); w = cin.nextInt();
				a[i] = new Node(l, r, w);
			}
			Arrays.sort(a,0,n,cmp);
			
			int l = 0, r = 1000*1000, ans = r;
			while(l <= r){
				int mid = (l+r)>>1;
				if(ok(mid)){
					ans = mid;
					r = mid-1;
				}
				else l = mid+1;
			}
			out.println(ans);
		}
	}
	Main() {
		cin = new Scanner(System.in);
		out = new PrintWriter(System.out);
	}

	public static void main(String[] args) {
		Main e = new Main();
		e.work();
		out.close();
	}

	public Scanner cin;
	public static PrintWriter out;
	int upper_bound(int[] A, int l, int r, int val){//upper_bound(A+l,A+r,val)-A;
		int pos = r;
		r -- ;
		while(l <= r){
			int mid = (l+r)>>1;
			if(A[mid]<=val){
				l = mid+1;
			}
			else {
				pos = mid;
				r = mid-1;
			}
		}
		return pos;
	}
	class Queue {
		int[] queue = new int[N+10];
		int front, rear;

		// front <= rear
		Queue() {
		//	queue = new int[x];
		}

		void clear() {
			front = rear = 1;
		}

		boolean empty() {
			return front == rear;
		}

		int size() {
			return rear - front;
		}

		int front() {
			return queue[front];
		}

		int rear() {
			return queue[rear - 1];
		}

		void push_rear(int x) {
			queue[rear++] = x;
		}

		void pop_front() {
			front++;
		}

		void pop_rear() {
			rear--;
		}
	}

	int max(int x, int y) {
		return x > y ? x : y;
	}

	int min(int x, int y) {
		return x < y ? x : y;
	}

	double max(double x, double y) {
		return x > y ? x : y;
	}

	double min(double x, double y) {
		return x < y ? x : y;
	}

	static double eps = 1e-8;

	int abs(int x) {
		return x > 0 ? x : -x;
	}

	double abs(double x) {
		return x > 0 ? x : -x;
	}

	boolean zero(double x) {
		return abs(x) < eps;
	}
}


UvaLive 4254 Processor 优先队列