题意:
每种商品有利润以及保质期,需要在保质期内卖出,一天只能卖一种商品,求做多可以得到多少利润。
思路:
贪心,按照价格从大到小排序。遍历时如果当前日期已经被占就放在前一天。
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
struct node{
int w;
int day;
}pd[12000];
int vis[12000];
bool cmp(node a, node b)
{
if (a.w != b.w)
return a.w > b.w;
else
return a.day > b.day;
}
int main(void)
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
while (~scanf("%d", &n))
{
memset(vis,0,sizeof(vis));
for (int i = 0; i < n; i++)
scanf("%d %d", &pd[i].w, &pd[i].day);
int ans = 0;
sort(pd,pd+n,cmp);
for (int i = 0; i < n; i++)
{
int p = pd[i].day;
while (vis[p]==1)
p--;
if (p==0)
continue;
vis[p]=1;
ans += pd[i].w;
}
printf("%d\n", ans);
}
return 0;
}