http://poj.org/problem?id=3481
题意:给你一些数,以及他们的键值,要你对其更具键值排序以及输出
1 :k p 代表k的键值为p
2 :输出键值最高的k,然后删除
3:输出键值最低的k,然后删除
如果这个题目用map来做的话就很简单,因为map在里面就根据它的值进行了排序,所以我们只需要取其队首以及队尾来做就可以了
然后可以写一个treap树来做,这个题目感觉就是一个treap树的裸题
1 import java.util.Map; 2 import java.util.Scanner; 3 import java.util.TreeMap; 4 5 public class Main{ 6 public static void main(String[] args) { 7 Scanner cin = new Scanner(System.in); 8 TreeMap<Integer, Integer> s = new TreeMap<Integer ,Integer>(); 9 int a,b,c; 10 while(true){ 11 a = cin.nextInt(); 12 if(a==0) 13 break; 14 if(a==1){ 15 b = cin.nextInt(); 16 c = cin.nextInt(); 17 s.put(c, b); 18 }else if(a==3){ 19 if(s.isEmpty()) 20 System.out.println("0"); 21 else { 22 System.out.println(s.get(s.firstKey())); 23 s.remove(s.firstKey()); 24 } 25 }else { 26 if(s.isEmpty()) 27 System.out.println("0"); 28 else { 29 System.out.println(s.get(s.lastKey())); 30 s.remove(s.lastKey()); 31 } 32 } 33 } 34 } 35 }