前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >POJ-1456 Supermarket(贪心,并查集优化)

POJ-1456 Supermarket(贪心,并查集优化)

作者头像
ShenduCC
发布于 2018-04-25 09:25:39
发布于 2018-04-25 09:25:39
77400
代码可运行
举报
文章被收录于专栏:算法修养算法修养
运行总次数:0
代码可运行

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 这道题目还有一个用并查集优化的方法,算是对并查集的一个应用。 贪心:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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);
    }
}

并查集优化:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-12-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Supermarket超市(贪心算法 优先队列)- POJ 1456
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.
ACM算法日常
2018/08/07
7670
Supermarket超市(贪心算法 优先队列)- POJ 1456
【LeetCode】关关刷题日记24-Leetcode 121. Best Time to Buy and Sell Stock
关小刷刷题 24——Leetcode 121. Best Time to Buy and Sell Stock 题目 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),
WZEARW
2018/04/09
5780
leetcode: explore-array-22 买卖股票的最佳时机 II
leetcode explore 初级算法第二题:买卖股票的最佳时机 II。这个系列目前一共有5道题目:
用户7685359
2020/08/24
3740
leetcode: explore-array-22 买卖股票的最佳时机 II
LeetCode <dp>53&121&122. Maximum Subarray & Best Time to Buy and Sell Stock
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
大学里的混子
2018/11/13
6290
前端也能学算法:由浅入深讲解贪心算法
贪心算法是一种很常见的算法思想,而且很好理解,因为它符合人们一般的思维习惯。下面我们由浅入深的来讲讲贪心算法。
蒋鹏飞
2020/10/15
5060
前端也能学算法:由浅入深讲解贪心算法
【POJ1456】Supermarket(贪心)
每个商品有过期日期和价格,每天可以卖一个商品,必须在过期前出售才能收益,求最大收益。
饶文津
2020/06/02
3100
POJ-2181 Jumping Cows(贪心)
Jumping Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7329 Accepted: 4404 Description Farmer John’s cows would like to jump over the moon, just like the cows in their favorite nursery rhyme. Unfortunately, cows can not jump
ShenduCC
2018/04/25
8580
Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)
A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. Howeve
Angel_Kitty
2018/04/09
7460
Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)
POJ 2063(完全背包)
John never knew he had a grand-uncle, until he received the notary’s letter. He learned that his late grand-uncle had gathered a lot of money, somewhere in South-America, and that John was the only inheritor.
dejavu1zz
2020/10/23
3890
【leetcode系列】121. 买卖股票的最佳时机
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
lucifer210
2019/08/16
5980
经典算法之贪心算法
  贪心算法,又称贪婪算法(Greedy Algorithm),是指在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优解出发来考虑,它所做出的仅是在某种意义上的局部最优解。  
用户3467126
2019/11/08
1.7K0
【算法题解】 Day5 贪心
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
sidiot
2023/08/31
1500
【算法题解】 Day5 贪心
121. Best Time to Buy and Sell Stock
Say you have an array for which the i th element is the price of a given stock on day i.
yesr
2019/03/14
3120
LWC 55:714. Best Time to Buy and Sell Stock with Transaction Fee
该文讲述了如何使用动态规划求解最大利润问题,给定股票价格数组和交易费用,在一系列买入、卖出操作中选取最优策略,以最大利润为目标。具体介绍了状态转移方程和算法实现。"
用户1147447
2018/01/02
5470
【leetcode刷题】2019/3/7 T13-买卖股票的最佳时机
今天分享leetcode第13篇文章,也是leetcode第121题—买卖股票的最佳时机(Best Time to Buy and Sell Stock),地址是:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
木又AI帮
2019/07/18
3550
Code Forces 650 C Table Compression(并查集)
C. Table Compression time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard output Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others.
ShenduCC
2018/04/26
6540
买卖股票,贪心比动规还好用
力扣题目链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii
代码随想录
2021/11/16
5540
Array - 121. Best Time to Buy and Sell Stock
Say you have an array for which the _i_th element is the price of a given stock on day i.
ppxai
2020/09/23
2740
贪心——122. 买卖股票的最佳时机 II
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
向着百万年薪努力的小赵
2022/12/02
4440
LeetCode121. Best Time to Buy and Sell Stock解题
乍一看这题很熟悉,原来之前做过它的第二道,参见 LeetCode122.Best Time to Buy and Sell Stock解题 @GhostLWB
vincentbbli
2021/08/18
1740
推荐阅读
相关推荐
Supermarket超市(贪心算法 优先队列)- POJ 1456
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文