Supermarket

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 10725 Accepted: 4688

Description

A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of time units starting from the moment the sale begins. Each product takes precisely one unit of time for being sold. A selling schedule is an ordered subset of products Sell ≤ Prod such that the selling of each product x∈Sell, according to the ordering of Sell, completes before the deadline dx or just when dx expires. The profit of the selling schedule is Profit(Sell)=Σx∈Sellpx. An optimal selling schedule is a schedule with a maximum profit.

For example, consider the products Prod={a,b,c,d} with (pa,da)=(50,2), (pb,db)=(10,1), (pc,dc)=(20,2), and (pd,dd)=(30,1). The possible selling schedules are listed in table 1. For instance, the schedule Sell={d,a} shows that the selling of product d starts at time 0 and ends at time 1, while the selling of product a starts at time 1 and ends at time 2. Each of these products is sold by its deadline. Sell is the optimal schedule and its profit is 80.

Write a program that reads sets of products from an input text file and computes the profit of an optimal selling schedule for each set of products.

Input

A set of products starts with an integer 0 <= n <= 10000, which is the number of products in the set, and continues with n pairs pi di of integers, 1 <= pi <= 10000 and 1 <= di <= 10000, that designate the profit and the selling deadline of the i-th product. White spaces can occur freely in input. Input data terminate with an end of file and are guaranteed correct.

Output

For each set of products, the program prints on the standard output the profit of an optimal selling schedule for the set. Each result is printed from the beginning of a separate line.

Sample Input

4 50 2 10 1 20 2 30 1

7 20 1 2 1 10 3 100 2 8 2

5 20 50 10

Sample Output

80

185

题意:

超市有n个商品,每个商品都是利润和保质期,每天只卖一种,问,怎么卖才可以获得最大利润,求出最大利润

思路:

贪心,这个题目我一开始看到,觉得可以用背包做啊,和背包题目很像啊。为什么这道题目可以用贪心做呢?这里说一下我对贪心和动态规划的理解:贪心就是每次都选取最优的结果就会是最优的,比如遇到背包问题,如果每个物品的体积都是1,那么就完全可以用贪心解决。但是如果每个物品的体积不一样呢?就不可以用贪心,贪心会错,自己体会。这是动态规划和贪心的区别。这道题目可以用贪心做,这里要贪两个地方,首先我先卖利润最大的商品,这是第一个贪。其次利润最大的商品应该尽量在后面卖,接近保质期的时间卖,这样前面的时间可以多卖一些其他的商品让利润最大,这是第二个贪。这道题目由于每个商品卖的时间都是1天,如果时间不固定,就不可以用贪心。和前面的道理是一样的,这里给一个另一个hdu的题目的博客,就可以体会到贪心和动态规划的区别。

http://blog.csdn.net/Dacc123/article/details/50397213

这道题目还有一个用并查集优化的方法,算是对并查集的一个应用。

贪心:

#include <iostream>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h> using namespace std;
#define MAX 100000
int n;
int vis[MAX+5];
struct Node
{
int price;
int time;
}a[MAX+5];
int cmp(Node a,Node b)
{
if(a.price==b.price)
return a.time<b.time;
return a.price>b.price;
}
int main()
{
int ans;
while(scanf("%d",&n)!=EOF)
{
memset(vis,0,sizeof(vis));
ans=0;
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].price,&a[i].time);
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
{
for(int j=a[i].time;j>=1;j--)
{
if(!vis[j])
{
ans+=a[i].price;
vis[j]=1;
break;
}
}
}
printf("%d\n",ans);
}
}

并查集优化:

#include <iostream>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h> using namespace std;
#define MAX 100000
int n;
int vis[MAX+5];
struct Node
{
int price;
int time;
}a[MAX+5];
int cmp(Node a,Node b)
{
return a.price>b.price;
}
int father[MAX+5];
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
void init()
{
for(int i=0;i<=MAX;i++)
father[i]=i;
}
int main()
{
int ans;
while(scanf("%d",&n)!=EOF)
{
init();
ans=0;
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].price,&a[i].time);
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
{
int b;
b=find(a[i].time);
if(b>0)
{
ans+=a[i].price;
father[b]=b-1;
}
}
printf("%d\n",ans);
}
}

POJ-1456 Supermarket(贪心,并查集优化)的更多相关文章

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

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

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

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

  3. Supermarket---poj456(贪心并查集优化)

    题目链接:http://poj.org/problem?id=1456 题意是现有n个物品,每个物品有一个保质期和一个利润,现在每天只能卖一个商品,问最大的利润是多少,商品如果过期了就不能卖了: 暴力 ...

  4. poj1456 Supermarket 贪心+并查集

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

  5. POJ 1456 - Supermarket - [贪心+小顶堆]

    题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...

  6. nyoj 208 + poj 1456 Supermarket (贪心)

    Supermarket 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 A supermarket has a set Prod of products on sal ...

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

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

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

    有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少 这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润 ...

  9. Supermarket(贪心/并查集)

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

随机推荐

  1. 兼容 iOS Retina(视网膜显示) 的程序

    首先我们需要明确一点,iOS设备上图片兼容retina的问题最初是由于iPhone4的分辨率由iPhone3的320X480提升到了640X960所产生. 为了让iPhone4能够兼容iPhone3上 ...

  2. js实现点击评论进行显示回复框

    有人在群里问如何在留言评论那里点击回复按钮,下面就自动显示一个回复框,他想要的效果如图: 于是我随意的写了一段HTML,代码如下: <!DOCTYPE HTML> <html lan ...

  3. mysql 字符串转数据丢失精度,mysql转换丢失精度,mysql CAST 丢失精度

    mysql 字符串转数据丢失精度,mysql转换丢失精度,mysql CAST 丢失精度 =============================== ©Copyright 蕃薯耀 2017年9月1 ...

  4. Linux应急响应(三):挖矿病毒

    0x00 前言 ​ 随着虚拟货币的疯狂炒作,利用挖矿脚本来实现流量变现,使得挖矿病毒成为不法分子利用最为频繁的攻击方式.新的挖矿攻击展现出了类似蠕虫的行为,并结合了高级攻击技术,以增加对目标服务器感染 ...

  5. 绕过D盾的一句话

    一个很简单的一个技巧,作个笔记,可以绕过D盾检测. 新建test1.php <?php eval($_POST[g]); ?> 新建test2.php <?php $a=" ...

  6. 浅谈千万级PV/IP规模高性能高并发网站架构(转自老男孩)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://oldboy.blog.51cto.com/2561410/736710 如果把来 ...

  7. codeforces水题100道 第十四题 Codeforces Round #321 (Div. 2) A. Kefa and First Steps (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/580/A题意:求最长连续非降子序列的长度.C++代码: #include <iostream ...

  8. Android Studio Error:Connection timed out: connect.解决方案

    遇到了这样的错误: Error:Connection timed out: connect. If you are behind an HTTP proxy, please configure the ...

  9. 【H5】使用h5实现复制粘贴功能

    方案一 : 可满足大部分浏览器正常使用 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  10. Esper学习之八:EPL语法(四)

    关于EPL,已经写了三篇了,预估计了一下,除了今天这篇,后面还有5篇左右.大家可别嫌多,官方的文档对EPL的讲解有将近140页,我已经尽量将废话都干掉了,再配合我附上的例子,看我的10篇文章比那140 ...