您的位置:首页 > 其它

[HDU 5349] MZL's simple problem 神题

2015-08-06 15:28 176 查看
http://acm.hdu.edu.cn/showproblem.php?pid=5349

题意:开始输入一个 n 表示有 n 个操作

1 x :插入一个数 x 。

2:删除最小的数

3:查询最大的数。

思路:题目看完有木有觉得很像平衡树?,相用平衡树,multiset,优先队列神马的都去面壁吧。。。我刚刚面壁回来了。。。

其实这是个非常简单的题,我觉得这题出的很好,非常好。。做题越多的人越容易想复杂,,通过这题我发现我的思想已经被禁锢了。。。今天在群里有个人问了这题,,我看了他的写法觉得这尼玛很有问题啊(主要是写的太简单。。),但是又找不到问题。。QwQ,,最后仔细一想还真就这么简单。。

思路:因为删除是最小的,查找是最大的,所以操作过程中是不会删除掉最大值(除非删除最后一个),只要保存最大的值就行了。还加个计数的,数量是 0 查询的时候就输出 0。就这么简单 有木有,有木有。。。

[code]#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int inf = (1 << 31) - 1;

int main()
{
    int n;
    while(~scanf("%d", &n)){
        int x, counts = 0, maxn = -inf;
        while(n--){
            scanf("%d",&x);
            if(x == 1){
                scanf("%d", &x);
                maxn = x > maxn ? x : maxn;
                counts ++;
            }
            else if(x == 2){
                if(counts){
                    counts --;
                    if(counts == 0)
                        maxn = -inf;
                }
            }
            else if(x == 3){
                if(counts == 0)
                    printf("0\n");
                else
                    printf("%d\n", maxn);
            }
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: