Gym 100112C Cookie Selection ,不停插入输出中位数

27 篇文章 0 订阅
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 first 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 Sample Output 1
1
2
3
4
#
#
#
#
3
2
4
1
NCPC 2012 Problem C: Cookie Selection 5
NCPC 2012
Sample Input 2 Sample Output 2
1
#
2
#
3
#
4
#

题意:
给你曲奇饼的直径,有#时就去取出中位数输出;
解;
一个less 优先队列,一个greater队列
取中位数优先插less队列,

#include<bits/stdc++.h>>
using namespace std;

int main()
{
priority_queue<int,vector<int>,less<int> > q1;
priority_queue<int,vector<int>,greater<int> > q2;
    while(!q1.empty()){q1.pop();}
    while(!q2.empty()){q2.pop();}
    char s[1111];
    int i,j;
    while(~scanf("%s",s))
    {
        if(s[0]=='#')
        {
            int num;
            if(q1.size()==q2.size())
            {
                 num=q2.top();
                q2.pop();

            }
            else
            {
                num=q1.top();
                q1.pop();
            }
            cout<<num<<endl;
        }
        else
        {
            int x=0;
            for(i=0;i<strlen(s);i++)
            {
                x*=10;
                x+=(s[i]-'0');
            }
           // cout<<"x= "<<x<<endl;
            q1.push(x);
            int num=q1.top();
            q1.pop();
            q2.push(num);
            num=q2.top();
            q2.pop();
            if(q1.size()==q2.size())
                q1.push(num);
            else q2.push(num);
        }
      //if(s[0]=='4')break;
    }
    while(!q1.empty())
    {
        int num=q1.top();
        q1.pop();
        cout<<num<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值