Supermarket

Supermarket

There are n items, the first i items marked as \ ((D_i, P_i) \) , indicates that the commodity shelf life \ (d_i \) days, the value of \ (P_i \) , now from the first day, only a day sell a commodity, maximize revenue, \ (the n-\ Leq 10000 \) .

solution

This is clearly a subject of collection and disorderly, then, first consider the sort, into orderly columns

Method 1: press \ (p_i \) descending order

After the series went from scanning, easy to know the value of the largest items first, the simple idea that tells us to choose a commodity, but the best will do? The answer from the perspective of the problem, imagine that each day is a box, and each item is a ball, some goods can only be placed in front of a particular case.

In the final scenario, the box should be put into the ball can not recapture any, now back to the question, if you do not choose the ball, on behalf of, freeing up the box to the other ball, while the other ball apparently low value , so the result is certainly not outstanding.

The question now is clearly the ball should be placed in one day? The simple idea that we should on the day after the number of days you can put in as much as possible by, because for two days \ (x, y, x < y \) , the back of the box only three possibilities, \ (the X-, y \) can be selected, \ (the X-\) can be selected, \ (y \) can not be selected, \ (the X-, y \) can be selected either case, the result must be the best.

So we just scanned from front to back, can choose to have the election, and after a few days to the election as possible by, in order to optimize \ (O (n ^ 2) \) scanning, you can use the tree line, but not good write, we may wish to use a balanced tree, the tree each day number is added, if one day choose to sell a commodity, then it deletes the corresponding number of days in the tree, if you need to check whether it can choose whether or not, as long as the shelf life of the corresponding first look in a balanced tree can be less than or equal to its time complexity \ (O (nlog (n-)) \) , the constant is too large, as a method of relatively poor.

Reference Code:

#include <iostream>
#include <cstdio>
#include <set>
#include <cstring>
#include <algorithm>
#define il inline
#define ri register
#define Size 15000
using namespace std;
struct item{
    int p,d;
    il bool operator<(const item&x)const{
        return p>x.p;
    }
}I[Size];
set<int>S;
set<int>::iterator m;
il void read(int&);
int main(){int n;
    while(scanf("%d",&n)!=EOF){int ans(0);S.clear();
        for(int i(1);i<=10000;++i)S.insert(i);
        for(int i(1);i<=n;++i)read(I[i].p),read(I[i].d);
        sort(I+1,I+n+1);
        for(int i(1);i<=n;++i){
            m=S.upper_bound(I[i].d);
            if(m==S.begin())continue;
            S.erase(--m),ans+=I[i].p;
        }printf("%d\n",ans);
    }
    return 0;
}
il void read(int &x){
    x^=x;ri char c;while(c=getchar(),c<'0'||c>'9');
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}

Act II: Press \ (d_i \)

After the series went from scanning, you just know that this is the shortest shelf life of an item, if one is optimality argument greedy (that is, the candidate selected by the best to worst candidates are selected can be chosen, such as krucal algorithm) , then the second is an alternative method greedy, simply, that is the beginning do not necessarily optimal decision, but later will continue to replace the worst solution, so that the result is optimal.

May wish to maintain a small heap root, sort keywords \ (D_i \) , front to back scanning sequence, apparently heap is always less than the current number of elements in the shelf life of goods, if the shelf life of the item is greater than the number of elements in the heap, Join the heap, or else will have to replace a commodity, apparently to replace the worst of goods, which is top of the heap, so it can be done relatively constant small \ (O (nlog (the n-)) \) (a method used to this condition for a few days, so we wilt).

By the way, why do not press \ (d_i \) sort not? Because the alternative greed there is a premise, you have to be able to replace, if you do not press (d_i \) \ sorting eventually lead you to replace the example you have only one day shelf life can not be replaced, and you replaced have dozens of days shelf life, but also by after commodity.

Reference Code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#define il inline
#define ri register
#define Size 10500
#define swap(x,y) x^=y^=x^=y
using namespace std;
struct big{
    int a[Size],n;
    il void push(int x){
        a[++n]=x;int p(n);
        while(p>1)
            if(a[p>>1]>a[p])
                swap(a[p>>1],a[p]),
                    p>>=1;
            else break;
    }
    il void pop(){
        swap(a[1],a[n]),--n;
        int p(1),s(p<<1);
        while(s<=n){
            if(s<n&&a[s+1]<a[s])++s;
            if(a[s]<a[p])swap(a[s],a[p]),
                             p=s,s=p<<1;
            else break;
        }
    }
}B;
struct pi{int p,d;
    il bool operator<(const pi&x)const{
        return d<x.d;
    }
}p[Size];
il void read(int&);
int main(){int n;
    while(scanf("%d",&n)!=EOF){
        for(int i(1);i<=n;++i)
            read(p[i].p),read(p[i].d);
        sort(p+1,p+n+1);
        for(int i(1);i<=n;++i){
            if(p[i].d>B.n)B.push(p[i].p);
            else if(B.a[1]<p[i].p)
                B.pop(),B.push(p[i].p);
        }int ans(0);
        for(int i(1);i<=B.n;++i)
            ans+=B.a[i];printf("%d\n",ans),B.n=0;
    }
    return 0;
}
il void read(int &x){
    x^=x;ri char c;while(c=getchar(),c<'0'||c>'9');
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}

Guess you like

Origin www.cnblogs.com/a1b3c7d9/p/11248155.html