有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. 10 个让人惊讶的 jQuery 插件

    说是让人惊讶,你可能会觉得我们没见过世面,但这里提及的一些 jQuery 的插件的确平时比较少见,用的人应该更少. Grid portfolio 使用竖排方式显示条目信息,现在很流行的的内容布局方式. ...

  2. HDOJ 2152 Fruit(母函数)

    Fruit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. C#与C++之间类型的对应{转}

    Windows Data Type   .NET Data Type BOOL, BOOLEAN   Boolean or Int32 BSTR    String BYTE    Byte CHAR ...

  4. mysql去除重复查询的SQL语句基本思路

    SELECT R.* FROM trans_flow R, (SELECT order_no, MAX(status_time) AS status_time FROM trans_flow GROU ...

  5. 集合类 Collection

    1.Collection接口有两个子接口: List:保存元素顺序的线性表,允许有重复元素. Set:不记录元素的保存顺序,不允许有重复元素.数学中的集合 Collection接口中的方法如下: Co ...

  6. 【动态规划】 之最长公共子序列LCS

    int lcs_len(char *a, char *b, int c[][N]){ int aLen=strlen(a), bLen=strlen(b), i,j; ; i<=aLen; i+ ...

  7. C# virtual和override

    本文转载来自于:http://bollaxu.iteye.com/blog/1662855 在函数的声明中,当有“virtual”修饰的时候,和没有virtual有什么区别呢?最重要的一点就是调用实例 ...

  8. Linux Shell查看磁盘分区,内存使用,CPU使用率

    Linux Shell查看磁盘分区,内存使用,CPU使用率 #!/bin/bash #disk_used_rate Location=/dev/xvdb Disk_Used_Rate=$(df -h ...

  9. 谈谈三层架构中Model的作用

    Model又叫实体类,这个东西,大家可能觉得不好分层.包括我以前在内,是这样理解的:UI<-->Model<-->BLL<-->Model<-->DAL ...

  10. CentOS系统常用命令

    1.进入目录命令 cd /usr/bin //进入etc目录 cd .. //切换到上一层目录 cd / //进入主目录 2.新建文件命令 比如进入某个目录后执行 vim test 2.查看运行的项目 ...