HLJUOJ1011(小根堆)

1011: Love Candy

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 64   Solved: 16
[ Submit][ Status][ Web Board]

Description

Ernest loves candy, but he has a habit of eating only minimal candy every time,because he felt the small to eat, the rest will be more and more. But things are not always so, because every few days Ernest will buy a new candy, please help him find the size of candy that he should eat .

Input

Input contains one test cases.Every line contains an order.”BUY N” represents that he buy a new candy,and the size of this candy is N(1<=N<=2^31). “EAT” represents thar he want to eat candy. “END” represents the end of input.The amount of Ernest’s candy always less than 300000.

Output

For each “EAT”, you should output the result in a line.

Sample Input

BUY 3
BUY 2
BUY 5
EAT
BUY 6
EAT
END

Sample Output

2
3



解题思路:
	题目大意就是BUY的话买一块糖,EAT的话我要吃掉并输出手里权值最小的糖。
	由于数据给的很大,所以nlog(n)的时间复杂度会超时,tyh说堆排的插入和删除操作可以达到log(n)····涨姿势·····
	最后特别说明,不要想裸堆排那样对一个数组进行堆排序,这样时间复杂度依然是nlog(n),会超时。
	经过tyh一番讲解后····百度了下····最后选择用STL写····竟然没超时····
 
 
完整代码:
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
char str[10];
int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    priority_queue<int, vector<int>, greater<int> > a;
    while(~scanf("%s",str))
    {
        if(strcmp(str , "END") == 0)
            break;
        if(strcmp(str , "BUY") == 0)
        {
            int k;
            scanf("%d",&k);
            a.push(k);
        }
        else if(strcmp(str , "EAT") == 0)
        {
            printf("%d\n",a.top());
            a.pop();
        }
    }
}


 
 
超时代码:
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;

LL a[1000001];
int n;
char str[10];

void swap(LL *a,LL *b)
{
    LL t;
    t=*a;
    *a=*b;
    *b=t;
}

void Adjust(LL a[],int i,int n)
{
    int lchild=2*i;
    int rchild=2*i+1;
    int max=i;
    if(i<=n/2)
    {
        if(lchild<=n&&a[lchild]>a[max])
        {
            max=lchild;
        }
        if(rchild<=n&&a[rchild]>a[max])
        {
            max=rchild;
        }
        if(max!=i)
        {
            swap(&a[i],&a[max]);
            Adjust(a,max,n);
        }
    }
}

void Build(LL a[],int n)
{
    int i;
    for(i=n/2;i>=1;i--)
    {
        Adjust(a,i,n);
    }
}

void Heap(LL a[],int n)
{
    int i;
    Build(a,n);
    for(i=n;i>=1;i--)
    {
        swap(&a[1],&a[i]);
        Adjust(a,1,i-1);
    }

}



int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    n = 1;
    while(~scanf("%s",str))
    {
        if(strcmp(str , "END") == 0)
            break;
        if(strcmp(str , "BUY") == 0)
        {
            LL k;
            scanf("%lld",&k);
            a[n++] = k;
        }
        else if(strcmp(str , "EAT") == 0)
        {
            Heap(a , n - 1);
            printf("%lld\n",a[1]);
            a[1] = a[n-1];
            n--;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值