码迷,mamicode.com
首页 > 其他好文 > 详细

Sicily 14515. Cookie Selection

时间:2015-05-26 09:15:21      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

题目

技术分享

思路

用两个堆来放全部数就好了;
每个堆放一半;
题目说得这么长几个意思- -。

代码

#include <queue>
#include <vector>
#include <functional>
#include <stdio.h>
using namespace std;

int main() {

    priority_queue<int> FirstHalfPart;
    priority_queue<int, vector<int>, greater<int> > SecondHalfPart;
    char c[20];

    while (~scanf("%s", c)) {
        if (c[0] == ‘#‘) {
            printf("%d\n", FirstHalfPart.top());
            FirstHalfPart.pop();
            if (!SecondHalfPart.empty()
                && FirstHalfPart.size() <= SecondHalfPart.size()) {
                FirstHalfPart.push(SecondHalfPart.top());
                SecondHalfPart.pop();
            }
        }
        else {
            int size = 0;
            for (int i = 0; c[i] != ‘\0‘; i++) size = 10 * size + c[i] - ‘0‘;
            if (!FirstHalfPart.empty() && size > FirstHalfPart.top()) {
                SecondHalfPart.push(size);
                if (!SecondHalfPart.empty()
                    && FirstHalfPart.size() <= SecondHalfPart.size()) {
                    FirstHalfPart.push(SecondHalfPart.top());
                    SecondHalfPart.pop();
                }
            }
            else {
                FirstHalfPart.push(size);
                if (FirstHalfPart.size() > SecondHalfPart.size() + 2)
                {
                    SecondHalfPart.push(FirstHalfPart.top());
                    FirstHalfPart.pop();
                }
            }
        }
    }

    return 0;
}

Sicily 14515. Cookie Selection

标签:

原文地址:http://blog.csdn.net/u012925008/article/details/46003231

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!