Supermarket - greedy (disjoint-set optimization)

Topic Link

Meaning of the questions:

N give you a commodity, each commodity has two parameters pd, profit after p for goods sold, d indicates that the product can only be sold before the deadline, one day only to sell a commodity.
The goods are up to ask you how much profit can be obtained

answer:

greedy! ! !

 

 

According to profits descending order, on the same profits if the time limit descending order, so as to ensure more goods to sell within a certain period to obtain greater profits

After sequencing is complete, the end of the enumeration time for each item, and then forward violence (found from this period recently and can take up time), if the current time can not be sold vis array that is marked, you can then in the current time to sell

 

Code:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int vis[maxn];
int n;
struct node
{
    int p,d;
    bool operator < (const node &a)const
    {
        if(p==a.p)return d>a.d; 
        return p>a.p;
    }
}a[maxn];
int main()
{
    while(~scanf("%d",&n))
    {
        memset(vis,0,sizeof vis); 
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].p,&a[i].d);
        }
        sort(a+1,a+n+1);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int tmp=a[i].d;
            for(int j=tmp;j>=1;j--)
            {
                if(!vis[j])
                {
                    vis[j]=1;
                    ans+=a[i].p;
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }



    return 0;
}
View Code

 

In the case of greedy, can be optimized with disjoint-set found from this period recently and can take up time

f [] array to store the current period, to find a product f [x] = x, then the current time can be used, and then let the f [x] = x-1, represents the x term is marked, then his term x-1 ( after this time spent away from the nearest time period is the time x x-1) if f [x] = 0 indicates no position, and no longer record

 

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+5;
int f[maxn];
int n;
struct node
{
    int p,d;
    bool operator < (const node &a)const
    {
        if(p==a.p)return d>a.d; 
        return p>a.p;
    }
}a[maxn];
int Find(int x)
{
    return f[x]==x?x:f[x]=Find(f[x]);
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<maxn;i++)f[i]=i;

        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].p,&a[i].d);
        }
        sort(a+1,a+n+1);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int tmp=Find(a[i].d);
            if(tmp>0)
            {
                f[tmp]=tmp-1;
                ans+=a[i].p;
            }
        }
        printf("%d\n",ans);
    }



    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/j666/p/11613802.html