Black Box 分类: POJ 栈和队列 2015-08-05 14:07 2人阅读 评论(0) 收藏
Black Box
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8754 Accepted: 3599
Description
Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empty and i equals 0. This Black Box processes a sequence of commands (transactions). There are two types of transactions:
ADD (x): put element x into Black Box;
GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that i-minimum is a number located at i-th place after Black Box elements sorting by non- descending.
Let us examine a possible sequence of 11 transactions:
Example 1
N Transaction i Black Box contents after transaction Answer
(elements are arranged by non-descending)
1 ADD(3) 0 3
2 GET 1 3 3
3 ADD(1) 1 1, 3
4 GET 2 1, 3 3
5 ADD(-4) 2 -4, 1, 3
6 ADD(2) 2 -4, 1, 2, 3
7 ADD(8) 2 -4, 1, 2, 3, 8
8 ADD(-1000) 2 -1000, -4, 1, 2, 3, 8
9 GET 3 -1000, -4, 1, 2, 3, 8 1
10 GET 4 -1000, -4, 1, 2, 3, 8 2
11 ADD(2) 4 -1000, -4, 1, 2, 2, 3, 8
It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and GET transactions: 30000 of each type.
Let us describe the sequence of transactions by two integer arrays:
A(1), A(2), …, A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 by their absolute value, M <= 30000. For the Example we have A=(3, 1, -4, 2, 8, -1000, 2).
u(1), u(2), …, u(N): a sequence setting a number of elements which are being included into Black Box at the moment of first, second, … and N-transaction GET. For the Example we have u=(1, 2, 6, 6).
The Black Box algorithm supposes that natural number sequence u(1), u(2), …, u(N) is sorted in non-descending order, N <= M and for each p (1 <= p <= N) an inequality p <= u(p) <= M is valid. It follows from the fact that for the p-element of our u sequence we perform a GET transaction giving p-minimum number from our A(1), A(2), …, A(u(p)) sequence.
Input
Input contains (in given order): M, N, A(1), A(2), …, A(M), u(1), u(2), …, u(N). All numbers are divided by spaces and (or) carriage return characters.
Output
Write to the output Black Box answers sequence for a given sequence of transactions, one number each line.
Sample Input
7 4
3 1 -4 2 8 -1000 2
1 2 6 6
Sample Output
3
3
1
2
Source
Northeastern Europe 1996
题意:共有两种操作,一是添加一个元素,二是输出第i小的元素;
输入n,m.n代表有n个要添加的元素,m代表操作的个数,
在m个操作中a[i]=v,表示在第v次添加的时候输出第i小的元素
方法:用两个优先队列,q2从小到大出对,q1从大到小出对,q1储存的是前i-1小的数据,所以q2的队头就是第i小的数据;
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#pragma comment(linker, "/STACK:102400000")
#define WW freopen("output.txt","w",stdout)
const int MAX = 30010;
LL a[MAX];
int b;
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
priority_queue<LL >q1;
priority_queue<LL,vector<LL>,greater<LL> >q2;
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
}
int k=1;
for(int i=1; i<=m; i++)
{
scanf("%I64d",&b);
for(; k<=b; k++)
{
if(q1.empty()||q1.top()<a[k])
{
q2.push(a[k]);
}
else
{
q1.push(a[k]);
LL ans=q1.top();
q1.pop();
q2.push(ans);
}
}
LL ans=q2.top();
q2.pop();
printf("%I64d\n",ans);
q1.push(ans);
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Black Box 分类: POJ 栈和队列 2015-08-05 14:07 2人阅读 评论(0) 收藏的更多相关文章
- IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏
IIS上在主站点下搭建虚拟目录后,子站点中的<system.web>节点与主站点的<system.web>冲突解决方法: 在主站点的<system.web>上一级添 ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏
我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...
- THE DRUNK JAILER 分类: POJ 2015-06-10 14:50 13人阅读 评论(0) 收藏
THE DRUNK JAILER Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24918 Accepted: 1563 ...
- A Plug for UNIX 分类: POJ 图论 函数 2015-08-10 14:18 2人阅读 评论(0) 收藏
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14786 Accepted: 4994 Desc ...
- Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...
- 欧拉回路-Door Man 分类: 图论 POJ 2015-08-06 10:07 4人阅读 评论(0) 收藏
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2476 Accepted: 1001 Description ...
- Flip Game 分类: POJ 2015-06-15 14:59 22人阅读 评论(0) 收藏
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33519 Accepted: 14642 Descr ...
- Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
随机推荐
- 【转】DataSource高级应用
我们知道 DataSource能提供最高性能的对数据库的并发访问,但是,对DataSource的引用,也还有很多知识要弄清楚,获取Connection的方式是数据库性能最相关的技术,而对DataSou ...
- Git_Commands
- spring AutowireCapableBeanFactory 自动注入
文档:http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/config/Auto ...
- Spark朴素贝叶斯(naiveBayes)
朴素贝叶斯(Naïve Bayes) 介绍 Byesian算法是统计学的分类方法,它是一种利用概率统计知识进行分类的算法.在许多场合,朴素贝叶斯分类算法可以与决策树和神经网络分类算法想媲美,该算法能运 ...
- PAT乙级 1020. 月饼 (25)(只得到23分)
1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...
- 卸载PythonToolKit的方法
先借用官网的内容介绍一下: PythonToolkit (PTK) is an interactive environment for python. It was originally design ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON TestObjDef
zw版[转发·台湾nvp系列Delphi例程]HALCON TestObjDef procedure TForm1.Button1Click(Sender: TObject);var img : HU ...
- Python for z/OS
Install pythondev Install DB2 or server driver package easy_install ibm_db Get license file from tor ...
- sp_addlinkedserver 方法应用
EXEC sp_addlinkedserver @server='DBVIP',--被访问的服务器别名 @srvproduct='', @provider='SQLO ...
- Java的多线程+Socket
客户端: package com.wulala; import java.io.FileOutputStream;import java.io.IOException;import java.io.I ...