在线编程在线课堂在线测评Anycodes在线编程

编程论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

How to use bs4??
本帖最后由 carry0987 于
Double Queue 问题描述 : The new founded Balkan Investment Group Bank (
John 问题描述 : Little John is playing very funny game
linux-command Linux命令大全搜索工具,内容包含Linux命令
Coati 是一款跨平台的代码查看工具,适用于 C/C++ 和 Java。商业软件。特性:1. 索引
系统可承载海量并发,消息收发确认机制 保障消息必达 系统采用动态智
全平台视频监控,支持安卓苹果以及pcweb,支持海康大华等主流dvr,全部源码以及文档 单聊、群聊、商
如何访问类的私有属性? 下面以 TPathData 为例,
问题:从 XE4 以来,Firemonkey 曲线绘图在移动平台不平滑的问题一直令人诟病,提交到官方的 QC 也是族繁不及备载,官方似乎有意的
操作数据库(RODBC)   odbcConnect(dsn, uid="", p
数据模式:mode函数显示任何对象的模式。常见的单个的
系统可承载海量并发,消息收发确认机制 保障消息必达 系统采用动态智
RabbitMQ与PHP(一) 项
Iease团队扩编预备中,盼望能有Ruby或者java工程师加盟。全职兼职都可以。有爱好的伴侣请与我接洽。 邮件:i
ruby 怎么设置装备摆设GTK2,求教指导下!
#include #include #include #include using namespace std; int main() {
标题如图所示: 有n盏灯,编号1~n。一开端灯都是关着的
成熟的消息收发确认机制,支持万人大群 支持开发自定义的消息sdk接口,扩展性超强 支持单/
成熟的消息收发确认机制,支持万人大群 支持开发自定义的消息sdk接口,扩展性超强 支持单/
1. 注意列表和集合的区别 set 列表表现形式: list_1
Ajax   Ajax即“Asynchronous Javascript And
大师好,我比来在做布谷鸟优
分辨提取A和B图像Harris角点,接下来须要对

[POJ北大] POJ 3481 Double Queue [解题报告] Java

[复制链接]
发表于 2016-6-16 14:05:28 | 显示全部楼层 |阅读模式
Double Queue


问题描述 :

The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:
0The system needs to stop serving
1 K PAdd client K to the waiting list with priority P
2Serve the client with the highest priority and drop him or her from the waiting list
3Serve the client with the lowest priority and drop him or her from the waiting list
Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.
</p>
输入:

Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.
</p>
输出:

For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

样例输入:

  1. 2
  2. 1 20 14
  3. 1 30 3
  4. 2
  5. 1 10 99
  6. 3
  7. 2
  8. 2
  9. 0
复制代码
样例输出:

  1. 0
  2. 20
  3. 30
  4. 10
  5. 0
复制代码
提交代码   问题讨论   数据统计
解题代码:
  1. //* @author: Yeming Hu"[email protected]"
  2. import java.util.*;
  3. import java.io.*;

  4. public class Main
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         Scanner sc = new Scanner(new BufferedInputStream(System.in));
  9.         BinaryMinimumHeap minHeap = new BinaryMinimumHeap();
  10.         BinaryMaximumHeap maxHeap = new BinaryMaximumHeap();
  11.         
  12.         while(true)
  13.         {
  14.             int request = sc.nextInt();
  15.             if(request == 0)
  16.             {
  17.                 break;
  18.             }else if(request == 1)
  19.             {
  20.                 int k = sc.nextInt();
  21.                 int p = sc.nextInt();
  22.                 Client client = new Client(k,p);
  23.                 minHeap.offer(client);
  24.                 maxHeap.offer(client);
  25.             }else if(request == 2)
  26.             {
  27.                 if(maxHeap.size() == 0)
  28.                 {
  29.                     System.out.println(0);
  30.                 }else
  31.                 {
  32.                     Client client = maxHeap.poll();
  33.                     minHeap.remove(client);
  34.                     System.out.println(client);
  35.                 }
  36.             }else if(request == 3)
  37.             {
  38.                 if(minHeap.size() == 0)
  39.                 {
  40.                     System.out.println(0);
  41.                 }else
  42.                 {
  43.                     Client client = minHeap.poll();
  44.                     maxHeap.remove(client);
  45.                     System.out.println(client);
  46.                 }
  47.             }else
  48.             {
  49.                 throw new RuntimeException("No such type of request");
  50.             }
  51.         }
  52.     }
  53.    
  54. }

  55. class Client implements Comparable< Client>
  56. {
  57.     int id;
  58.     int priority;
  59.     int posInMinimumHeap;
  60.     int posInMaximumHeap;
  61.    
  62.     Client(int id, int priority)
  63.     {
  64.         this.id = id;
  65.         this.priority = priority;
  66.         this.posInMaximumHeap = 0;
  67.         this.posInMinimumHeap = 0;
  68.     }
  69.    
  70.     public int compareTo(Client client)
  71.     {
  72.         if(this.priority < client.priority)
  73.         {
  74.             return - 1;
  75.         }else if(this.priority == client.priority)
  76.         {
  77.             return 0;
  78.         }else
  79.         {
  80.             return 1;
  81.         }
  82.     }
  83.    
  84.     public String toString()
  85.     {
  86.         return Integer.toString(id);
  87.     }
  88. }

  89. class BinaryMinimumHeap
  90. {
  91.    
  92.     public static final int capacity = 1000001;
  93.     int count;
  94.     Client[] clients;
  95.     public BinaryMinimumHeap()
  96.     {
  97.         clients = new Client[capacity];
  98.         count = 0;
  99.     }
  100.    
  101.     public void offer(Client client)
  102.     {
  103.         if(count == capacity - 1)
  104.         {
  105.             throw new RuntimeException("Full Heap");
  106.         }
  107.         count++;
  108.         int i = count;
  109.         while(i > 1 && clients[i/2].compareTo(client) == 1)
  110.         {
  111.             clients[i] = clients[i/2];
  112.             clients[i].posInMinimumHeap = i;
  113.             i /= 2;
  114.         }
  115.         clients[i] = client;
  116.         clients[i].posInMinimumHeap = i;
  117.     }
  118.    
  119.     public Client poll()
  120.     {
  121.         if(count == 0)
  122.         {
  123.             throw new RuntimeException("Empty Heap");
  124.         }
  125.         Client result = clients[1];
  126.         result.posInMinimumHeap = 0;
  127.         Client last = clients[count];
  128.         count--;
  129.         int i = 1;
  130.         while(2*i  1 && clients[i/2].compareTo(client) == -1)
  131.         {
  132.             clients[i] = clients[i/2];
  133.             clients[i].posInMaximumHeap = i;
  134.             i /= 2;
  135.         }
  136.         clients[i] = client;
  137.         clients[i].posInMaximumHeap = i;
  138.     }
  139.    
  140.     public Client poll()
  141.     {
  142.         if(count == 0)
  143.         {
  144.             throw new RuntimeException("Empty Heap");
  145.         }
  146.         Client result = clients[1];
  147.         result.posInMaximumHeap = 0;
  148.         Client last = clients[count];
  149.         count--;
  150.         int i = 1;
  151.         while(2*i
复制代码
在线编程(http://www.anycodes.cn)&编程论坛(http://www.52exe.cn)感谢您的支持!
回复

使用道具 举报

发布主题 上个主题 下个主题 快速回复 返回列表 官方QQ群
在线客服
客 服 中 心
群 机 器 人
网站二维码
收 起 客 服

QQ|Archiver|手机版|小黑屋|Anycodes ( ICP14002806Anycodes在线编程

GMT+8, 2018-11-22 01:08 , Processed in 1.467187 second(s), 67 queries .

Powered by Anycodes

© 2001-2013 吉林市群龙科技有限公司 Inc.

快速回复 返回顶部 返回列表