Supermarket (consolidated search and priority queue)

Supermarket (consolidated search and priority queue)

Original title link https://www.acwing.com/problem/content/description/147/

There are N items in the supermarket, and each item has a profit pi p_ipiAnd expiration time di d_idi Only one item can be sold per day, and expired items cannot be sold.

What is the maximum benefit that can be obtained in the case of a reasonable arrangement of the goods sold every day.

Input format

The input contains multiple sets of test cases.

Each set of test cases starts with an integer N, and then N pairs of pi p_ipiSum di d_idi, Respectively represent the profit and expiration time of the i-th commodity.

During the input, any spaces or blank lines can be interspersed freely between the data, and the input is terminated when the input reaches the end of the file to ensure that the data is correct.

Output format

For each group of products, output a maximum profit value of the group.

Each result is on one line.

data range

0≤N≤100000
1≤ p i p_i pi, d i d_i di≤10000
There are 14 sets of test samples at most

Input sample:

4  50 2  10 1   20 2   30 1

7  20 1   2 1   10 3  100 2   8 2
   5 20  50 10

Sample output:

80
185

A priority queue

Sort the n items in ascending order of expiration time, and then continuously insert them into the priority queue, inserting in two cases.
The number of items in the priority queue means the number of products sold, which is the number of days size

If the i-th number is inserted,

1,size< d i d_i di Insert directly, because the number of days you have passed at this time has not reached your shelf life

2,size>= d i d_i di At this time, the profit of the i-th product is compared with the one with the smallest profit in the queue, and the larger profit remains in the queue.

Because the expiration time is sorted from smallest to largest, if di d_idi <=size 的话 d i d_i diMust be equal to size;

#include <bits/stdc++.h>
using namespace std;
const int N=10100;
priority_queue<int,vector<int>,greater<int> > p;//小顶堆 
pair<int,int> a[N];
int n,m,i,j;
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if (n==0)
        {
            puts("0");
            continue;
        }
        for(int i=1; i<=n; i++)
        {
            cin>>a[i].second>>a[i].first;//pair排序是以first为第一关键字,因为我们是时间为第一关键字,所以我们读入权值是第二关键字
        }
        sort(a+1,a+1+n);//将日期从小到大排序 
        for(int i=1; i<=n; i++)
        {
            if (a[i].first>p.size())
                p.push(a[i].second);
            else if(a[i].first==p.size() && a[i].second>p.top())
            {
                p.pop();
                p.push(a[i].second);
            }
        }
        long long ans=0;
        while(!p.empty())
        {
            ans+=p.top();
            p.pop();
        }
        printf("%lld\n",ans);
    }
    return 0;
}


Two, and check

This idea is three times faster than priority queue

First, we sort the profit from largest to smallest, and scan one product at a time; look for vacancies forward, if there is one, we can sell it, otherwise we can’t

f[i] represents the vacancy in front of the i-th number, this union check is to connect the vacancy with the product

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 233;
pair<int,int> p[maxn];
int f[maxn];//f[i]表示第i个商品前的空位 
int ff(int x)//查找祖宗; 
{
    if(f[x] == x) return x;
    return f[x] = ff(f[x]);
}
bool cmp(pair<int ,int>a,pair<int ,int>b)
{
	return a.first>=b.first;
}
//并查集是将空位和第i个数连通 
int main()
{
    int n;
    while(cin >> n)
    {
        int ans = 0;


        for(int i = 0; i < n; i++)
        {
            cin>>p[i].first>>p[i].second;
        }
        for(int i = 0; i <= maxn; i++) f[i] = i;
        sort(p,p+n,cmp);
        for(int i = 0; i <n; i++)
        {
            int v = p[i].first, e = p[i].second;
            int pos = ff(e);//查找前面的空位 
            if(pos > 0)//有空位 
            {
                ans += v;
                f[pos] = pos - 1;//空位往前移动 
            }
        }
        printf("%d\n", ans);
    }
}

Guess you like

Origin blog.csdn.net/hhuhgfhggy/article/details/109258883