Cookie Selection

Cookie Selection

时间: 10ms        内存:128M

描述:

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.

输入:

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).

输出:

A sequence of lines, each indicating the diameter in nm of a cookie sent for packaging, in the same order as they are sent.

示例输入:

1
2
3
4
#
#
#
#

示例输出:

3
2
4
1

提示:

参考答案(内存最优[1924]):

//Sample solution for the Cookie Selection problem in NCPC 2012 by Lukáš Poláček (lukasP)
#include <vector>
#include <cstdio>
#include <queue>
#include <string>
#include <iostream>
using namespace std;

typedef vector<int> vi;
int main()
{
    string s;
    priority_queue<int> a;
    priority_queue<int, vi, greater<int> > b;
    while (getline(cin, s))
    {
        if (s.empty())
            break;
        if (s[0] == '#')
        {
            printf("%d\n", a.top());
            a.pop();
            if (!b.empty() && a.size() <= b.size())
            {
                a.push(b.top());
                b.pop();
            }
        }
        else
        {
            int n;
            sscanf(s.c_str(), "%d", &n);
            if (!a.empty() && n > a.top())
            {
                b.push(n);
                if (a.size() <= b.size())
                {
                    a.push(b.top());
                    b.pop();
                }
            }
            else
            {
                a.push(n);
                if (a.size() > b.size() + 2)
                {
                    b.push(a.top());
                    a.pop();
                }
            }
        }
    }
}

参考答案(时间最优[496]):

#include <cstdio>  
#include <iostream>  
#include <algorithm>  
#include <cstring>  
#include <queue>  
using namespace std;  
  
#define maxn 300000  
#define inf 0x3fffffff  
int cnt = 0;  
char s[20];  
priority_queue<int> que1;  
priority_queue<int> que2;  
  
void balance(){  
    int len1 = que1.size(), len2 = que2.size();  
        if(len1>len2){  
            int temp = que1.top();  
            que1.pop();  
            que2.push(-temp);  
    }  
    if(que1.size()>0 && que2.size()>0){  
        int t1 = que1.top(), t2 = que2.top()*(-1);  
       // cout << "##" <<t1 <<" " <<t2<<endl;  
        if(t1 > t2){  
            que1.pop();  
            que2.pop();  
            que2.push(-t1);  
            que1.push(t2);  
        }  
    }  
}  
  
int main(){  
    while(gets(s)){  
        if(s[0]=='#'){  
            printf("%d\n",-que2.top());  
            que2.pop();  
            balance();  
        }else{  
            que1.push(atoi(s));  
            balance();  
        }  
    }  
    return 0;  
}  

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注