超市里有N个商品. 第i个商品必须在保质期(第di天)之前卖掉, 若卖掉可让超市获得pi的利润.
每天只能卖一个商品.
现在你要让超市获得最大的利润. Input多组数据.
每组数据第一行为一个整数N (0 <= N <= 10000), 即超市的商品数目
之后N行各有两个整数, 第i行为 pi, di (1 <= pi, di <= 10000)Output对于每一组数据, 输出当前条件下超市的最大利润
Sample Input
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
Hint
Translated by shleodai
#include<iostream>
#include<cstring>
#include<algorithm>
#define MAX 10010
using namespace std;
int f[MAX];
struct market{
int qi;
int di;
}m[MAX];//定义一下商品的钱数和天数
int cmp(market a,market b)
{
return a.qi>b.qi;//按价格排序
}
int root(int x)//寻找根结点
{
if(f[x]==-1)
return x;
else
{
f[x]=root(f[x]);
return f[x];
}
}
int main()
{
int n;
while(cin>>n)
{
memset(f,-1,sizeof(f));//把f中元素置为-1
for(int i=0;i<n;i++)
{
cin>>m[i].qi>>m[i].di;
}
sort(m,m+n,cmp);//排序
int sum=0;
for(int i=0;i<n;i++)
{
int temp=root(m[i].di);//返回m[i].di或它的根
if(temp>0)//如果天数没有被占用
{
sum+=m[i].qi;
f[temp]=temp-1;//第temp天被占用,f[temp]要等于temp-1
}
}
cout<<sum<<"\n";
}
return 0;
}