有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少

这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润

朴素的做法是:

按照每件商品的利润从大到小排序,有一个busy数组记录那天是否有东西卖出。对于每件商品,从它的截止日期开始遍历,如果那天有东西卖就看看前一天是否有卖东西,直到有一天没有东西卖或者前面的天数都有卖的。

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
struct Product
{
int p, d;
bool operator< (const Product& a) const
{
return p > a.p || (p == a.p && d > a.d);
}
}products[maxn];
bool busy[maxn]; int main(void)
{
#ifdef LOCAL
freopen("1456in.txt", "r", stdin);
#endif int n;
while(scanf("%d", &n) == )
{
memset(busy, false, sizeof(busy));
for(int i = ; i < n; ++i)
scanf("%d%d", &products[i].p, &products[i].d);
sort(products, products + n); int ans = ;
for(int i = ; i < n; ++i)
{
for(int j = products[i].d; j > ; --j)
{
if(!busy[j])
{
ans += products[i].p;
busy[j] = true;
break;
}
}
}
printf("%d\n", ans);
}
return ;
}

代码君一

并查集优化:

这里parent数组相当于之前代码的busy数组的优化。因为对于第i件商品我们都要从它的期限往前遍历来找到不忙的那天来卖,parent[i]存放第i天最近的能卖物品的天数。

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
struct Product
{
int p, d;
bool operator< (const Product& a) const
{
return p > a.p || (p == a.p && d > a.d);
}
}products[maxn];
int parent[maxn]; int Find(int a)
{
return parent[a] == a ? a : parent[a] = Find(parent[a]);
} int main(void)
{
#ifdef LOCAL
freopen("1456in.txt", "r", stdin);
#endif int n, maxday;
while(scanf("%d", &n) == )
{
maxday = ;
for(int i = ; i < n; ++i)
{
scanf("%d%d", &products[i].p, &products[i].d);
if(products[i].d > maxday) maxday = products[i].d;
} sort(products, products + n); int ans = ;
for(int i = ; i <= maxday; ++i)
parent[i] = i;
for(int i = ; i < n; ++i)
{
int pi = Find(products[i].d);
if(pi > )
{
parent[pi] = Find(pi - );
ans += products[i].p;
}
}
printf("%d\n", ans);
}
return ;
}

代码君二

POJ 1456 (贪心+并查集) Supermarket的更多相关文章

  1. POJ - 1456 贪心+并查集

    做法一:直接贪心,按照利润排序,然后直接尽量给每个活动安排到最晚的时间即可.时间复杂度O(n * d)当d都为10000时,很容易超时.由于这题数据比较水,所有贪心未超时. AC代码 #include ...

  2. Supermarket POJ - 1456 贪心+并查集

    #include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...

  3. POJ 1456 Supermarket(贪心+并查集)

    题目链接:http://poj.org/problem?id=1456 题目大意:有n件商品,每件商品都有它的价值和截止售卖日期(超过这个日期就不能再卖了).卖一件商品消耗一个单位时间,售卖顺序是可以 ...

  4. POJ 1456——Supermarket——————【贪心+并查集优化】

    Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  5. POJ 1456 Supermarket(贪心+并查集优化)

    一开始思路弄错了,刚开始想的时候误把所有截止时间为2的不一定一定要在2的时候买,而是可以在1的时候买. 举个例子: 50 2  10 1   20 2   10 1    50+20 50 2  40 ...

  6. poj1456 Supermarket 贪心+并查集

    题目链接:http://poj.org/problem?id=1456 题意:有n个物品(0 <= n <= 10000) ,每个物品有一个价格pi和一个保质期di (1 <= pi ...

  7. Supermarket(贪心/并查集)

    题目链接 原创的博客 题意: 超市里有N个商品. 第i个商品必须在保质期(第di天)之前卖掉, 若卖掉可让超市获得pi的利润. 每天只能卖一个商品. 现在你要让超市获得最大的利润. n , p[i], ...

  8. poj1456(贪心+并查集)

    题目链接: http://poj.org/problem?id=1456 题意: 有n个商品, 已知每个商品的价格和销售截止日期, 每销售一件商品需要花费一天, 即一天只能销售一件商品, 问最多能买多 ...

  9. poj1256(贪心+并查集)

    题目链接:http://poj.org/problem?id=1456 题意:给n件商品的价格和卖出截至时间,每一个单位时间最多只能卖出一件商品,求能获得的最大利润. 思路:首先是贪心,为获得最大利润 ...

随机推荐

  1. ASP.NET Session的七点认识

    原文:http://kb.cnblogs.com/page/108689/ ASP.NET Session的使用当中我们会遇到很多的问题,那么这里我们来谈下经常出现的一些常用ASP.NET Sessi ...

  2. ZOJ3229 Shoot the Bullet(有源汇的上下界最大流)

    #pragma warning(disable:4996) #include <iostream> #include <cstring> #include <string ...

  3. iOS后台运行

    http://www.cocoachina.com/bbs/read.php?tid=149564 文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后 ...

  4. POJ 1665

    #include<iostream>//chengdacaizi 08.11.12 #include<iomanip> #define p 3.1415927 using na ...

  5. 【HDU 5030】Rabbit's String (二分+后缀数组)

    Rabbit's String Problem Description Long long ago, there lived a lot of rabbits in the forest. One d ...

  6. AngularJS学习笔记1——什么是AngularJS?

    Angular JS是一个由Google维护的开源的Javascript框架,主要作者为: Misko Hevery(angular JS之父, Sr. Computer Scientist at G ...

  7. WPA/WAP2wifi 密码破解笔记

    前言: 相对于前一段时间脆弱的WEP路由器而言,当今的路由器加密方式也大都改变为WPA/WPA2,使得无线路由器的破解难度增加.虽然如此,但还是有很多漏洞层出不穷,如WPS.退一步来说,即使加密算法无 ...

  8. 【PSR规范专题(4)】PSR-3 日志接口规范

    本文转自:https://github.com/PizzaLiu/PHP-FIG/blob/master/PSR-3-logger-interface-cn.md 本文制定了日志类库的通用接口规范. ...

  9. jquery plug-in DataTable API中文文档参考

    前言:最近在做一个WEB后台,无意中发现这个插件,试用了一下觉得不错,但网上关于它的资料大多不全,所以利用一些时间将其API文档翻了一下,发在园子里供大家参考.(p.s:个人E文水平很差,对着灵格斯翻 ...

  10. 276. Paint Fence

    题目: There is a fence with n posts, each post can be painted with one of the k colors. You have to pa ...