传送门

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. Nginx的alias与root的用法区别和location匹配规则

    1.alias与root的用法区别 最基本的区别:alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录. location /abc/ { ...

  2. [Apple开发者帐户帮助]七、注册设备(2)注册多个设备

    如果您有许多测试设备,则可以创建包含设备名称和设备ID的文件,并将整个文件上载到开发人员帐户.您的开发人员帐户支持以下两种文件格式:具有.deviceids文件扩展名和纯文本文件的属性列表文件.您选择 ...

  3. [Swift通天遁地]七、数据与安全-(12)使用Instruments Leaks工具检测内存泄露

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. eclipse下整合springboot和mybatis

    1.新建maven项目 先新建一个maven项目,勾选上creat a simple project,填写groupid,artifactid 2.建立项目结构 3.添加依赖 <parent&g ...

  5. hdu2029

    http://acm.hdu.edu.cn/showproblem.php?pid=2029 #include<stdio.h> #include<string.h> #inc ...

  6. ACM_小G的循环

    小G的循环 Time Limit: 2000/1000ms (Java/Others) Problem Description: 一回生,二回熟,三回就腻,小G用for,while循环用得太多了,累觉 ...

  7. 2CSS层叠规则(即引入CSS的三种不同方式的优先级)

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  8. [转]linux grep命令

    转自:http://www.cnblogs.com/end/archive/2012/02/21/2360965.html 1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表 ...

  9. spring boot打包文件后,报错\No such file or directory

    现象: 一段代码: ClassLoader loader = XXXUtil.class.getClassLoader(); String jsFileName = loader.getResourc ...

  10. MVC系列学习(五)-传递数据 与 接收数据

    1.控制器向视图传递数据 a.使用ViewData b.使用ViewBag c.使用Model 方式二: d.使用TempData 2.为什么在控制器中设置了一些属性,在视图中可以接受 3.Actio ...