Stack and heap variants

Stack and heap variants

statement

Reference courseware and teaching from the Accelerator , the analysis also too lazy to hit him from

Heap elements deleted

Mark borrow ideas, and we maintain a primary heap same nature heap (root, rootlets) of each deletion put it thrown into the pile mark inside
when we need to pop, and if the top of the heap element and delete elements of the same top of the heap ,
then it shows that we remove this element had before, so we just delete the heap and the heap inside while pop, then examine the next element.
Easy to find the time complexity and space complexity no substantive changes.

question

luogu P3545 [POI2012] HUR-Warehouse Store

Our first thought is to sell, sell , but there are likely to be sold directly to a person and not enough rest GG
To avoid this, we put all the value has sold thrown into a heap, each heap out the top element and the current value of the comparison, if the top of the heap element than him , it would be "drained away" stuff before, in other words to him.
Although this increase can not answer, but you can increase inventory. There is a feeling of predecessors trees descendants cool 233

Because the subject did not say according to the order, so only one structure (Look solution to a problem can be lazy

#include<cstdio>
#include<queue>
using namespace std;
#define ll long long
const int MAX = 250000+9;

inline ll read() {
    char ch = getchar(); ll f = 1, x = 0;
    while(ch<'0' || ch>'9') {if(ch=='-') f = -1; ch = getchar();}
    while(ch>='0' && ch<='9') {x = x*10+ch-'0'; ch = getchar();}
    return x*f;
}

int n;
ll kucun;

struct time{
    ll arr, brr;//这里的arr,在day里面是第i天进货数,在p_q里面是记录的满足了第几天的客人 
    bool operator < (const time& xxx) const {
        return brr < xxx.brr;//大根堆:为了取出前面花费最多的
    }
}day[MAX];
priority_queue <time> q;


int main() {
    n = read();
    for(int i = 1; i <= n; i++) day[i].arr = read();
    for(int i = 1; i <= n; i++) day[i].brr = read();
    int t = 0;//表示满足了多少人 
    for(int i = 1; i <= n; i++) {
        kucun += day[i].arr;
        if(kucun >= day[i].brr) { //能买就买 
            kucun -= day[i].brr;
            t++;
            time now; now.arr = i, now.brr = day[i].brr;
            q.push(now); 
        } else {
            if(q.size() && q.top().brr > day[i].brr) {//不能买的时候再“退钱回去” 
                kucun += q.top().brr-day[i].brr;//这波不亏
                q.pop(); 
                time now; now.arr = i, now.brr = day[i].brr;
                q.push(now); 
            }
        }
    }
    printf("%d\n", t);
    while(!q.empty()) printf("%lld ",q.top().arr), q.pop();//里面装的都是满足了的 
    printf("\n");
}

Guess you like

Origin www.cnblogs.com/tyner/p/11443008.html