题意:
每种商品有利润以及保质期,需要在保质期内卖出,一天只能卖一种商品,求做多可以得到多少利润。
思路:
贪心,按照价格从大到小排序。遍历时如果当前日期已经被占就放在前一天。
#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;
}
本文介绍了一种通过贪心算法解决商品在有限保质期内如何安排销售以获得最大利润的问题。文章提供了一个C++实现的例子,该算法首先将商品按利润从高到低排序,并尽可能在保质期内安排销售。
1861

被折叠的 条评论
为什么被折叠?



