对于所有评测用例,1 ≤ N, K ≤ 1000,1 ≤ w ≤ N,1 ≤ s ≤ 10000,1 ≤ c ≤ 100。
分析:按照题目要求,先根据时间进行排序再进行模拟即可,看代码吧:
import java.util.*; public class Main { static Scanner in = new Scanner(System.in); static int n,k; static int[] a = new int[1005]; //按照借钥匙时间排序 static Comparator<Node> com1 = new Comparator<Node>() { @Override public int compare(Node o1, Node o2) { return o1.s-o2.s; } }; //按照还钥匙时间排序 static Comparator<Node> com2 = new Comparator<Node>() { @Override public int compare(Node o1, Node o2) { return o1.e==o2.e?o1.num-o2.num:o1.e-o2.e;//题目规则 } }; //找到第一个-1就返回,题目说从左边开始还钥匙 static int findPos(int []a,int k) { for(int i = 1;i <= n;i++) if(a[i]==k) return i; return 0; } public static void main(String[] args) { n = in.nextInt(); k = in.nextInt(); PriorityQueue<Node> q1 = new PriorityQueue<Node>(com1); PriorityQueue<Node> q2 = new PriorityQueue<Node>(com2); int num,s,e,eno,p; for(int i = 0;i < k;i++) { num = in.nextInt(); s = in.nextInt(); e = in.nextInt() + s; Node no = new Node(num,s,e); q1.add(no); q2.add(no); } for(int i = 1;i <= n;i++) a[i] = i; while(q1.size()!=0&&q2.size()!=0) { //借的时间小于还的时间,说明此状态下钥匙是被借出的,用-1标记 if(q1.peek().s<q2.peek().e) { p = findPos(a, q1.peek().num); a[p] = -1; q1.poll(); } //借的时间大于等于还的时间,说明此状态下钥匙是钥匙是被归还的的 else if(q1.peek().s>=q2.peek().e) { eno = q2.peek().num; p = findPos(a, -1); a[p] = eno; q2.poll(); } } //借的时间肯定小于还的时间,那么对于剩下的没还的钥匙从左边一个一个挂上即可 while(q2.size()!=0) { eno = q2.peek().num; p = findPos(a, -1); a[p] = eno; q2.poll(); } for(int i = 1;i < n;i++) System.out.print(a[i]+" "); System.out.println(a[n]); } } class Node{ int num,s,e; Node(int num,int s,int e){ this.num = num; this.s = s; this.e = e; } }