书上用的方法是正着按照天数推,如果任务大于小根堆顶就替换,天数多于任务就加。

而我依稀记得以前洛谷上有一题也是这个,用时光倒流来求解,天数倒推,加任务,取大根堆顶即可。

我的代码实现:

 #include <cstdio>
#include <queue>
#include <algorithm>
const int N = ;
/// poj 1456
struct A {
int val, time;
bool operator < (const A &x) const {
return time < x.time;
}
}a[N]; std::priority_queue<int> Q; int main() {
int n;
while(scanf("%d", &n) != EOF) {
while(!Q.empty()) Q.pop();
for(int i = ; i <= n; i++) {
scanf("%d%d", &a[i].val, &a[i].time);
}
std::sort(a + , a + n + );
int ans = ;
for(int t = a[n].time, j = n; t >= ; t--) {
while(a[j].time >= t) {
Q.push(a[j--].val);
}
if(!Q.empty()) {
ans += Q.top();
Q.pop();
}
}
printf("%d\n", ans);
}
return ;
}

AC代码

poj1456 Supermarket的更多相关文章

  1. POJ1456 Supermarket 并查集

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ1456 题意概括  一家超市,要卖出N种物品(每种物品各一个),每种物品都有一个卖出截止日期Di(在该 ...

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

    Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10725 Accepted: 4688 Descript ...

  3. poj1456——Supermarket

    Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14656   Accepted: 6656 Desc ...

  4. POJ1456 supermarket [堆]

    题目传送门 Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15192   Accepted: 685 ...

  5. poj1456 Supermarket[另类的并查集做法]

    1.Supermarket(题目地址) 跟很久以前模拟的打地鼠那题一样,贪心+优先队列.这次换用并查集做法. 还是基于贪心,但这次换一种策略,先选价值最大的, 同时使其尽可能晚的被选上(因为早选会将之 ...

  6. POJ1456 Supermarket —— 贪心 + 路径压缩优化

    题目链接:http://poj.org/problem?id=1456 Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Subm ...

  7. POJ-1456 Supermarket 贪心问题 有时间限制的最小化惩罚问题

    题目链接:https://cn.vjudge.net/problem/POJ-1456 此题与HDU-1789完全是一道题 题意 有N件商品,分别给出商品的价值和销售的最后期限,只要在最后日期之前销售 ...

  8. POJ1456 Supermarket 贪心

    贪心策略:一定先卖价值最大的,然后考虑卖当前的物品,卖的日期越靠后,越优,可以为以后的物品提供机会 #include <stdio.h> #include <string.h> ...

  9. poj1456 Supermarket 贪心+并查集

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

随机推荐

  1. OpenCV__type()返回的数字

    OpenCV中的类型以宏定义的形式给出 type_c.h中片段 #define CV_CN_MAX 512 #define CV_CN_SHIFT 3 #define CV_DEPTH_MAX (1 ...

  2. 使用withCount后再使用select设置查询的字段。就找不到withCount的数据了

    https://laravelacademy.org/index.php/discussion/1021 如:Article::withCount(['comments'])->select(' ...

  3. Android——MaterialDesign之四 FloatingActionButton、Snackbar、CoordinaterLayout

    FloatingActionButton 悬浮按钮,默认colorAccent来作为按钮的颜色 <android.support.design.widget.FloatingActionButt ...

  4. CentOS7装Tomcat

    有两种安装方式:(1)yum 命令  (2)安装包 本次采用第二种方式: 1.windos下载apache-tomcat-7.0.73.tar.gz安装包 2.通过WinSCP传到linux下(本次放 ...

  5. linux 安装python 和pip

    下载文件 python官网:https://www.python.org/downloads/ 百度网盘http://pan.baidu.com/s/1mixGB12     密码   9nzu [r ...

  6. extensions

    extensions.blocklist.enabled

  7. 使用styled-components实现CSS in JS

    前面的话 使用jsx语法可以实现HTML in JS,使用svgr可以实现svg in JS,使用styled-components可以实现CSS in JS.这样,使用react开发,就变成了使用J ...

  8. 【Python练习题】程序5

    #题目:输入三个整数x,y,z,请把这三个数由小到大输出. # a = input('请输入整数: \n') # # b = input('请输入整数: \n') # # c = input('请输入 ...

  9. How to write to an event log by using Visual C#

    using System; using System.Diagnostics; namespace WriteToAnEventLog_csharp { /// Summary description ...

  10. B-树 B+树复习总结

    一.B-树的定义 一棵m阶的B-树或为空树,或为具有以下特性的m叉树 1.树中每个结点至多有m棵子树 (m-1个关键字) 2.根结点至少有两棵子树 (至少有一个关键字) 3.除根节点的分支结点至少有f ...