贪心+并查集
将利润大的尽可能往后安排,尽可能多的卖出商品,用并查集维护一个一维数组记录位置的变化表示该日是否有商品
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e4 + 5;
int prt[maxn];
struct node
{
int p, d;
bool operator < (const node &m) const{
return p > m.p;
}
} a[maxn];
int getpa(int x)
{
return x == prt[x] ? x : prt[x] = getpa(prt[x]);
}
int main()
{
int n, ans;
while(scanf("%d",&n) != EOF){
ans = 0;
for(int i = 1; i <= maxn; i++) prt[i] = i;
for(int i = 1; i <= n; i++){
scanf("%d%d",&a[i].p,&a[i].d);
}
sort(a+1,a+n+1);
for(int i = 1; i <= n; i++){
int m = getpa(a[i].d);
if(m > 0){
ans += a[i].p;
prt[m] = m-1;
}
}
printf("%d\n",ans);
}
return 0;
}