题意是,告诉你每个货物的利润和截止日期,让你求在最后一件货物过期前的最大利润。
分析:要想求最大利润,那么每一时间单位都只需要卖出最大利润的货物就可以,如果冲突,就是要并查集来快速查找上一个不矛盾点,来放置。
代码:
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
using namespace std;
const int maxn=10010;
struct Node
{
int p,d;
}node[maxn];
int root[maxn];
bool cmp(Node a,Node b)
{
return a.p>b.p;
}
int find(int x)
{
if(root[x]==-1) return x;
else return root[x]=find(root[x]);
}
int main()
{
int m;
while(scanf("%d",&m)!=EOF)
{
memset(root,-1,sizeof root);
for(int i=0;i<m;i++)
{
scanf("%d %d",&node[i].p,&node[i].d);
}
sort(node,node+m,cmp);
long long sum=0;
for(int i=0;i<m;i++)
{
int t=find(node[i].d);
if(t>0)
{
sum+=node[i].p;
root[t]=t-1;
}
}
printf("%lld\n",sum);
}
//system("pause");
return 0;
}