【POJ】 1456 supermarket
Given n items, the i-th item has the following information:
you can get p i when you sell it .
The expiration time is d i , and it cannot be sold after the expiration time.
It takes 1 time to sell an item, seeking the maximum profit.
A plurality of sets of data, each data line, a first pair of integer n and n number of P I , D I , to terminate the end character file.
0≤n≤10000
1≤p I , D I ≤ 10000.
Ideas
Sort by revenue first.
Use the remaining expiration time to do and check.
If it is not out of date.
Plus income, the remaining expiration time is -1;
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct jgt
{
int d,p;
}a[10010];
int n,f[10010];
int find(int x)//找代表值
{
if(x==f[x]) return x;
return f[x]=find(f[x]);
}
bool cmp(jgt t1,jgt t2)
{
return t1.p>t2.p;
}
int main()
{
int mx,ans,i,t;
while(cin>>n)
{
for(mx=0,i=0;i<n;mx=max(mx,a[i].d),i++)
scanf("%d%d",&a[i].p,&a[i].d);
for(i=0;i<=mx;f[i]=i,i++);
sort(a,a+n,cmp);//先按收益排序
for(ans=0,i=0;i<n;i++)
{
t=find(a[i].d);
if(t>0)
{
ans+=a[i].p;//加收益
f[t]=t-1;//剩余过期时间-1
}
}
printf("%d\n",ans);
}
return 0;
}