POJ1456---Supermarket---贪心+小根堆

2023-05-16

POJ1456 Supermarket

Time Limit: 2000MS Memory Limit: 65536K

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

Hint

The sample input contains two product sets. The first set encodes the products from table 1. The second set is for 7 products. The profit of an optimal schedule for these products is 185.

Source

Southeastern Europe 2003

Code

#include<iostream>
#include<queue>
using namespace std;

const int maxn = 1e4 + 3;

inline int read()
{
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
    return x * f;
}

struct node {
    int w, t;
    node(int w = 0, int t = 0) :w(w), t(t){}
}nodes[maxn];

// 先把先过期的排前面,相同时间过期的比较谁的利润更高
bool cmp(node a, node b) {
    if(a.t != b.t) return a.t < b.t;
    return a.w > b.w;
}

int main() {
    int n, w, t;
    while (cin >> n) {

        for (int i = 0; i < n; i++) {
            w = read(), t = read();
            nodes[i] = node(w, t);
        }
        sort(nodes, nodes + n, cmp);

        priority_queue<int, vector<int>, greater<int>> pq;
        for (int i = 0; i < n; i++) {
            // 若pq的空间小于t,说明可以在t天过期前把之前的都卖完再把这个卖了
            if (nodes[i].t > pq.size()) pq.push(nodes[i].w);
            // 若t天过期的商品和空间一样大,说明这件商品不能卖出,这时候要和堆顶进行比较
            else if (pq.size() && nodes[i].t == pq.size()) {
                if (nodes[i].w > pq.top()) {
                    pq.pop();
                    pq.push(nodes[i].w);
                } 
            }
            
        }
        int ans = 0;
        while (pq.size()) {
            ans += pq.top();
            pq.pop();
        }
        cout << ans << endl;
    }
    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

POJ1456---Supermarket---贪心+小根堆 的相关文章

  • Linux:chmod -R 777 *含义

    Linux xff1a chmod R 777 首先 xff0c chmod命令是linux上用于改变权限的命令 xff0c R 是递归遍历子目录 xff0c 因为你要操作的文件使用的 通配符 777 xff0c 第一个7代表文件所属者的权
  • STM32HAL库学习笔记七——I2C通信

    HAL库快速部署I2C 本文主要介绍如何使用STM32CubeMX快速部署I2C通信 xff0c 并与EEPROM进行数据收发 文章目录 HAL库快速部署I2CI2C简介EEPROM简介HAL库部署IIC通信实现多字节写入一 CubeMX配
  • python报错Statements must be separated by newlines or semicolons解决方法

    今天做练习时遇到这样的报错 xff1a Statements must be separated by newlines or semicolons 翻译一下就是 xff1a 语句必须用换行符或分号分隔 首先按报错提示 xff0c 我把cl
  • python自然语言处理之spacy详解

    spaCy简介 spaCy号称工业级Python自然语言处理 xff08 NLP xff09 软件包 xff0c 可以对自然语言文本做词性分析 命名实体识别 依赖关系刻画 xff0c 以及词嵌入向量的计算和可视化等 spaCy模块有4个非常
  • anaconda创建env报错 ResolvePackageNotFound

    具体错误 如图 xff1a 按照其他博主 xff08 方法详情 xff09 提供的方法操作了还是有部分报错 xff1a 解决策略 继续上面解决剩下的部分报错 xff0c 打开 yaml文件 xff0c 记事本打开就行 将报错列出的几个包移到
  • LDA主题建模过程及参数详解

    平台及工具 语言 xff1a python 平台 xff1a anaconda 43 jupyter notebook 语料库 xff1a 近三百篇英文文献的摘要 主要代码 首先 xff0c pandas处理csv数据 span class
  • 已经成功安装了但是jupyter notebook仍然找不到模块

    问题描述 工具 语言 jupyter notebook 43 anaconda python 有时会遇到这样的情况 xff0c 命名已经install了模块 xff0c notebook还是报找不到模块错误 再装已经提示satisfied
  • pyecharts 地图绘制

    环境描述 win11 jupyter notebook 目标效果 世界地图 43 按数据进行分级着色 xff1b 最终效果图如下 xff1a pyecharts 绘制地图时注意点 可以实现目标地图绘制效果的python库很多 xff0c 这
  • 指定wb用户在指定日期范围内的wb内容抓取

    一 操作步骤 只记录过程 xff0c 不讲述原理 1 获取用户ID和cookie 用户ID在进入个人主页时导航栏中就会有显示 xff0c 例如下面这样 xff1a cookie获取 xff08 有的代码无需cookie也能运行 xff09
  • 中介变量、调节变量与协变量

    在平时看论文过程中偶会接触到这几个概念 xff0c 然而都没想过弄明白 xff0c 每次总觉得只要看明白个大概反正自己又不用这种方法 作为科研人 xff0c 还是应该保持谦逊 xff0c 保持学习 一 中介变量 1 概念 中介变量 xff0
  • linux环境搭建

    文章目录 linux环境搭建基础工具环境docker镜像docker 的基本操作git amp amp sshbash脚本 python 环境 linux环境搭建 基础工具环境 docker镜像 Docker CE 镜像源站 docker如
  • 【Linux】状态机

    Linux 状态机 首先感谢阅读 xff0c 作者是在工作中学习与积累 xff0c 每一个笔记都是心得和积累 xff0c 希望可以和大家一起交流学习 学习咱们先定好计划 xff0c 需要了解的都一样 xff0c 有 xff1a xff08
  • MyBatisPlus源码

    MyBatisPlus源码 文章目录 MyBatisPlus源码1 通用Mapper 96 BaseMapper 96 2 顶级Mapper 96 Mapper 96 3 通用Service 96 IService 96 4 通用Servi
  • bomb二进制炸弹拆除实验(MIPS)

    上学期计算机系统基础课程的一个实验 xff0c 在这里再简略整理一下 xff1a 实验要求 xff1a 仅给定一个MIPS二进制可执行文件bomb xff0c 要求运用GDB调试工具 xff0c 通过分析反汇编代码来输入正确的参数以拆除炸弹
  • 图与网络可视化实战(matlab实现)

    本篇以2020年数学建模美赛D题的足球传球网络可视化为例 xff0c 分三大步骤来简单讲解一下matlab在图与网络可视化方面的应用 xff1a Step1 构图 xff1a 首先根据输入数据构造邻接矩阵 xff0c 然后调用matlab中
  • Week8 - 程序设计思维与实践 - HRZ的序列(第二次CSP模拟T1)

    T1 HRZ的序列 该比赛已结束 xff0c 您无法在比赛模式下递交该题目 您可以点击 在题库中打开 以普通模式查看和递交本题 题目描述 相较于咕咕东 xff0c 瑞神是个起早贪黑的好孩子 xff0c 今天早上瑞神起得很早 xff0c 刷B
  • Week8 - 程序设计思维与实践 - HRZ学英语(第二次CSP模拟T2)

    T2 HRZ学英语 该比赛已结束 xff0c 您无法在比赛模式下递交该题目 您可以点击 在题库中打开 以普通模式查看和递交本题 题目描述 瑞神今年大三了 xff0c 他在寒假学会了英文的26个字母 xff0c 所以他很兴奋 xff01 于是
  • Week14 - 程序设计思维与实践 - 矩阵快速幂(+优化DP)

    题目连接 A Q老师与石头剪刀布 xff08 必做 xff09 题意 每一个大人曾经都是一个小孩 xff0c Q老师 也一样 为了回忆童年 xff0c Q老师 和 Monika 玩起了石头剪刀布的游戏 xff0c 游戏一共 n 轮 无所不知
  • Week14 - 程序设计思维与实践 - HDU3700 - Cat(限时模拟)

    Problem Description There is a cat cat likes to sleep If he sleeps he sleeps continuously no less than A hours For some
  • SDU《程序设计思维与实践》课程个人感想

    这门课到此也就宣告结束了呀 xff0c 回想过去居家学习的 16 周真的是既辛酸又充实 由于是非竞赛生 xff0c 之前没怎么接触过算法刷题这一方面 xff0c 唯一学的点算法也是来自上学期的数据结构 xff0c 回想刚开始上这门课的时候

随机推荐