zoukankan      html  css  js  c++  java
  • 2014 UESTC Training for Data Structures H

    H - Cookies Test

    Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
     

    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+12in the sorted sequence, while if there was an even number c of cookies in the holding area, you would send the cookie at position c2+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 600000 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 300000000 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 and output

    Sample InputSample Output
    1
    2
    3
    4
    #
    #
    #
    #
    3
    2
    4
    1

    题意大致是自己写一个数据结构支持修改o(lgn),且取出排序后中间数字o(lgn).

    使用两个优先队列,一个记录前n/2个元素的大根堆,一个记录剩下元素的小根堆,

    询问时就pop小根堆,修改时就比较一下top小根堆来确定是插入大根堆还是小根堆。

    询问和修改之后就要维护两个队列的大小使其满足n/2和n-n/2。如此就可AC。

     

    学习了优先级队列的STL

    priority_queue<int> front;//优先队列默认为vector储存且为大根堆;

    priority_queue<int, vector<int>, greater<int> > rear;//greater<int>能使其变为小根堆。

     

    开始在打的时候while (scanf("%s",s)!=EOF) 忘记了!=EOF,结果出来了个RF,结果又查了好久,。。。。。

    #include <iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<queue>
    #include<vector>
    #include<string.h>
    using namespace std;
    
    int main()
    {
        char s[10000];
        int g,h,num1=0,num2=0;
        priority_queue<int> fron;
        priority_queue<int, vector<int>, greater<int> > rear;
        while (scanf("%s",s)!=EOF){
            if (strcmp(s,"#")) {
                g=atoi(s);
                if (num2==0 || g>=rear.top()) {rear.push(g);num2++;}else {fron.push(g);num1++;}
                if (num1>num2){
                    rear.push(fron.top());num2++;
                    fron.pop();num1--;
                }
                if (num2>=num1+2){
                    fron.push(rear.top());num1++;
                    rear.pop();num2--;
                }
            }else{
                printf("%d
    ",rear.top());
                rear.pop();num2--;
                if (num1>num2) {
                    h=fron.top();
                    fron.pop();num1--;
                    rear.push(h);num2++;
                }
            }
        }
        return 0;
    }

     

  • 相关阅读:
    inotify-java linux系统监听文件发生变化,实时通知java程序
    设置模式之单例模式(附上一个Objective-C编写的播放音乐的单例类)
    设计模式之观察者模式(关于OC中的KVOKVCNSNotification)
    设计模式之原型模式(深入理解OC中的NSCopying协议以及浅拷贝、深拷贝)
    设计模式之模板方法模式&&迪米特法则(代码Objective-C展示)
    iOS开发:深入理解GCD 第一篇
    设计模式之工厂方法模式(代码用Objective-C展示)
    iOS开发:一个高仿美团的团购ipad客户端的设计和实现(功能:根据拼音进行检索并展示数据,离线缓存团购数据,浏览记录与收藏记录的批量删除等)
    Xcode一些好用的插件,以及这些插件的管理器
    综合出现NSScanner: nil string argument libc++abi.dylib: terminat错误的解决方案
  • 原文地址:https://www.cnblogs.com/Atlantis67/p/3702848.html
Copyright © 2011-2022 走看看