传送门

1.贪心 + 优先队列

按照时间排序从前往后

很简单不多说

——代码

 #include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>
#define N 10001 int n, t, ans;
std::priority_queue <int, std::vector <int>, std::greater <int> > q; struct node
{
int a, b;
}p[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline bool cmp(node x, node y)
{
return x.b < y.b;
} int main()
{
int i, j;
while(~scanf("%d", &n))
{
t = ;
ans = ;
while(!q.empty()) q.pop();
for(i = ; i <= n; i++) p[i].a = read(), p[i].b = read();
std::sort(p + , p + n + , cmp);
for(i = ; i <= n; i++)
{
if(t <= p[i].b)
{
t++;
ans += p[i].a;
q.push(p[i].a);
}
else if(t == p[i].b + && q.top() < p[i].a)
{
ans += p[i].a - q.top();
q.pop();
q.push(p[i].a);
}
}
printf("%d\n", ans);
}
return ;
}

2.并查集

很难想

按照价格从大到小排序

如果当前的那一天没有被占用,那么就用当前那一天,如果当前那一天被占用了,就用上一天,如果还被占用,再往前

其实这就是暴力过程

可以用并查集优化,当前天被占用时用并查集连接上一天,如果根指向0,就表明前面的天都被占用了,也就不用加

具体看代码

——代码

 #include <cstdio>
#include <iostream>
#include <algorithm>
#define N 10001 int n, ans;
int f[N]; struct node
{
int a, b;
}p[N]; inline bool cmp(node x, node y)
{
return x.a > y.a;
} inline int find(int x)
{
return x == f[x] ? x : f[x] = find(f[x]);
} inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} int main()
{
int i, x;
while(~scanf("%d", &n))
{
ans = ;
for(i = ; i < N; i++) f[i] = i;
for(i = ; i <= n; i++) p[i].a = read(), p[i].b = read();
std::sort(p + , p + n + , cmp);
for(i = ; i <= n; i++)
{
x = find(p[i].b);
if(x)
{
f[x] = x - ;
ans += p[i].a;
}
}
printf("%d\n", ans);
}
return ;
}

[POJ1456]Supermarket(贪心 + 优先队列 || 并查集)的更多相关文章

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

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

  2. A - A Supermarket (贪心, 并查集)

    超市里有n个产品要卖,每个产品都有一个截至时间dx(从开始卖时算起),只有在这个截至时间之前才能卖出并且获得率润dy. 有多个产品,所有可以有不同的卖出顺序,每卖一个产品要占用1个单位的时间,问最多能 ...

  3. Luogu 1525 【NOIP2010】关押罪犯 (贪心,并查集)

    Luogu 1525 [NOIP2010]关押罪犯 (贪心,并查集) Description S城现有两座监狱,一共关押着N名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨 ...

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

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

  5. poj1456 Supermarket 贪心+并查集

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

  6. POJ 1456 Supermarket 区间问题并查集||贪心

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

  7. 1202. [HNOI2005]狡猾的商人【贪心 或 并查集】

    Description 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 ...

  8. NYOJ 208 Supermarket (模拟+并查集)

    题目链接 描述 A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Pr ...

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

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

随机推荐

  1. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  2. java动态代理实例

    import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflec ...

  3. Java基础学习经验分享

    很多人学习Java,尤其是自学的人,在学习的过程中会遇到各种各样的问题以及难点,有时候卡在一个点上可能需要很长时间,因为你在自学的过程中不知道如何去掌握和灵活运用以及该注意的点.下面我整理了新手学习可 ...

  4. phpStorm更新后配置svn无法使用

    phpStorm迎来新的更新,结果之前配置的svn竟然无法使用啦,快捷键一类也没有作用.各种查找解决方案,最后找到解决方案.点击File找到Settings,找到Plugins这个部分,这个部分是管理 ...

  5. array_column()函数兼容低版本

    array_column 用于获取二维数组中的元素(PHP 5.5新增函数),但我们有时候需要在低版本的 function i_array_column($input, $columnKey, $in ...

  6. Android内存管理(12)*「实例」用Monitor 生成.hprof文件 并分析内存泄漏

    参考 http://blog.csdn.net/xiaanming/article/details/42396507 基本步骤: 1,准备一个有内存泄漏的代码 2,如何发现内存泄漏 3,生成.hpro ...

  7. Spring IOC set注入

    Hobby.java package com.wh.bean; public class Hobby { private Integer id; private String name; public ...

  8. UNIX环境高级编程--6

    系统数据文件和信息    数据文件都是ASCII文本文件,并且使用标准I/O库读这些文件,例如口令文件/etc/passwd和组文件/etc/group就是经常被多个程序频繁使用的两个文件.    口 ...

  9. SQL Server之纵表与横表互转

    1,纵表转横表 纵表结构 Table_A: 转换后的结构: 纵表转横表的SQL示例: SELECT  Name ,        SUM(CASE WHEN Course = N'语文' THEN G ...

  10. 新认知之WinForm窗体程序

    Windows应用程序和控制台应用程序有很大的区别 >Form1.cs  :窗体文件,程序员对窗体编写的代码一般都存放在这个文件中. >Form1.Designer.cs :窗体设计文件, ...