虽然不喜欢吃这个煎饼
题意:给出一列数列,随时求其中位数并删除。。
思路:用两个堆,,左边一个堆,,右边一个堆,并维持其个数差不超过1.
一开始自己写的堆,,自己测试的数据也是对我,就是一直WA,,
后来改用priority_queue。,,就直接简单粗暴 了
#include<iostream>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
#include<ctype.h>
#include<algorithm>
using namespace std;
#define N 100010
priority_queue<int,vector<int>,less<int> > l;
priority_queue<int,vector<int>,greater<int> > r;
char s[444];
int main()
{
int i,j,k;
int n;
while (scanf("%s",s)!=EOF)
{
if (isdigit(s[0]))
{
k=atof(s);
if (l.empty() || k<=r.top())
l.push(k);
else if (k>=r.top())
r.push(k);
}
else
{
printf("%d\n",r.top());
r.pop();
}
while (l.size()>r.size())
{
r.push(l.top());
l.pop();
}
while (l.size()+1<r.size())
{
l.push(r.top());
r.pop();
}
}
}
再贴自己没有过的代码。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctype.h>
using namespace std;
#define N 600010
char s[23];
int small[N],big[N];
int nsmall,nbig;
void insert_small(int k)
{
small[++nsmall]=k;
int cur=nsmall;
int father=cur>>1;
while (father>=1)
{
if (small[cur]>small[father])
{
swap(small[father],small[cur]);
cur=father;
father=cur>>1;
}
else
break;
}
}
void insert_big(int k)
{
big[++nbig]=k;
int cur=nbig;
int father=cur>>1;
while (father)
{
if (big[father]>big[cur])
{
swap(big[father],big[cur]);
cur=father;
father=cur>>1;
}
else
break;
}
}
void pop_small()
{
small[1]=small[nsmall--];
int cur=1;
int child=cur<<1;
while (child<=nsmall)
{
if (child+1<nsmall && small[child]<small[child+1])
child++;
if (small[child]>small[cur])
{
swap(small[child],small[cur]);
cur=child;
child<<=1;
}
else
break;
}
}
void pop_big()
{
big[1]=big[nbig--];
int cur=1;
int child=cur<<1;
while (child<=nbig)
{
if (child<nbig && big[child+1]<big[child])
child++;
if (big[child]<big[cur])
{
swap(big[child],big[cur]);
cur=child;
child<<=1;
}
else
break;
}
}
int main()
{
freopen("in.txt","r",stdin);
int i,j,k;
big[0]=0;
small[0]=0;
nsmall=nbig=0;
while (scanf("%s",s)!=EOF)
{
if (isdigit(s[0]))
{
k=atof(s);
//cout<<' '<<nsmall<<' '<<nbig<<endl;
if (nsmall<1 || k<=big[1])
insert_small(k);
else if (k>big[1])
insert_big(k);
}
else
{
printf("%d\n",big[1]);
pop_big();
}
//cout<<nsmall<<' '<<nbig<<endl;
while (nsmall>nbig )
{
insert_big(small[1]);
pop_small();
}
while (nbig>nsmall+1 )
{
insert_small(big[1]);
pop_big();
}
// cout<<nsmall<<' '<<nbig<<endl;
}
return 0;
}
Cookies Test
Time Limit: 3000 ms Memory Limit: 65535 kB Solved: 53 Tried: 196
Description
As chief programmer at a cookie production plant you have many responsibilities, one of them being that the cookies produced and packaged at the plant adhere to the very demanding quality standards of the Nordic Cookie Packaging Consortium (NCPC).
At any given time, your production line is producing new cookies which are stored in a holding area, awaiting packaging. From time to time there are also requests from the packaging unit to send a cookie from the holding area to be packaged. Naturally, you have programmed the system so that there are never any packaging requests sent if the holding area is empty. What complicates the matter is the fact that representatives of the NCPC might make a surprise inspection to check that your cookies are up to standard. Such an inspection consists of the NCPC representatives demanding that the next few cookies sent to the packaging unit instead be handed over to them; if they are convinced that these cookies look (and taste) the same, you pass the inspection, otherwise you fail.
Fortunately, the production plant has invested in a new measurement thingamajig capable of measuring cookie diameters with a precision of 1 nanometre (nm). Since you do not have the time to always be on the lookout for cookie craving NCPC inspectors, you have decided that a sensible strategy to cope with the inspections is to always send a cookie having the median diameter among all cookies currently in the holding area to the packaging unit on a request. If there is no cookie exhibiting the median diameter in the holding area, you instead send the smallest cookie larger than the median to the packaging unit, hoping it will please the NCPC inspectors. This means that, if the cookies were sorted in order of ascending diameter, and if there was an odd number c of cookies in the holding area, you would send the cookie at position (c + 1)/2 in the sorted sequence, while if there was an even number c of cookies in the holding area, you would send the cookie at position (c/2) + 1 in the sorted sequence to the packaging unit on a request.
Input
Each line of the input contains either a positive integer d, indicating that a freshly baked cookie with diameter d nm has arrived at the holding area, or the symbol ’#’, indicating a request from the packaging unit to send a cookie for packaging. There are at most 600 000 lines of input, and you may assume that the holding area is empty when the ?rst cookie in the input arrives to the holding area. Furthermore, you have read somewhere that the cookie oven at the plant cannot produce cookies that have a diameter larger than 30 centimetres (cm) (or 300 000 000 nm).
Output
A sequence of lines, each indicating the diameter in nm of a cookie sent for packaging, in the same order as they are sent.
Sample Input
1
2
3
4
#
#
#
#
Sample Output
3
2
4
1