POJ_1456 Supermarket 【并查集/贪心】
一、题面
二、分析
1.贪心策略:先保证从利润最大的开始判断,然后开一个标记时间是否能访问的数组,时间尽量从最大的时间开始选择,这样能够保证后面时间小的还能够卖。
2.并查集:并查集直接加快了判断该时间能否卖的速度,贪心原理相同。
三、AC代码
//贪心
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <fstream> using namespace std; const int MAXN = 1e4+;
struct Node
{
int px, dx;
bool operator<(const Node t)const
{
return px > t.px;
}
}Data[MAXN];
bool flag[MAXN]; int main()
{
//freopen("input.txt", "r", stdin);
int N, Ans;
while(scanf("%d", &N)!=EOF)
{
Ans = ;
for(int i = ; i < N; i++)
scanf("%d %d", &Data[i].px, &Data[i].dx);
sort(Data, Data+N);
memset(flag, , sizeof(flag));
for(int i = ; i < N; i++)
{
for(int j = Data[i].dx; j >= ; j--)
{
if(!flag[j])
{
Ans += Data[i].px;
flag[j] = ;
break;
}
}
}
printf("%d\n", Ans);
}
return ;
}
贪心
//并查集加速
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <fstream> using namespace std; const int MAXN = 1e4+;
struct Node
{
int px, dx;
bool operator<(const Node t)const
{
return px > t.px;
}
}Data[MAXN];
bool flag[MAXN];
int par[MAXN]; int Find(int x)
{
if(par[x] == -)
return x;
return par[x] = Find(par[x]);
} int main()
{
//freopen("input.txt", "r", stdin);
int N, Ans;
while(scanf("%d", &N)!=EOF)
{
Ans = ;
for(int i = ; i < N; i++)
scanf("%d %d", &Data[i].px, &Data[i].dx);
sort(Data, Data+N);
memset(flag, , sizeof(flag));
memset(par, -, sizeof(par));
for(int i = ; i < N; i++)
{
int f = Find(Data[i].dx); //判断该时间以下是否有时间可以卖
if(f > )
{
Ans += Data[i].px;
par[f] = f-;
}
}
printf("%d\n", Ans);
}
return ;
}
并查集
POJ_1456 Supermarket 【并查集/贪心】的更多相关文章
- poj 1456 Supermarket - 并查集 - 贪心
题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- [POJ2054]Color a Tree (并查集+贪心)
POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ1456:Supermarket(并查集+贪心)
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17634 Accepted: 7920 题目链接 ...
- 利用并查集+贪心解决 Hdu1232
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 2480 Steal the Treasure (并查集+贪心)
题意:给你n个点,m条边,包括有向边与无向边,每条边都有一个权值.在每个点上都有一个人,他可以走与这个点直接相连的所有边中任意一条边一次,并且得到这个权值,就不能走了,注意这条路也只能被一个人走.问最 ...
- POJ1456:Supermarket(并查集版)
浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:http://poj.org/problem?id=1456 堆作法:https:/ ...
- UVA 1664 Conquer a New Region (并查集+贪心)
并查集的一道比较考想法的题 题意:给你n个点,接着给你n-1条边形成一颗生成树,每条边都有一个权值.求的是以一个点作为特殊点,并求出从此点出发到其他每个点的条件边权的总和最大,条件边权就是:起点到终点 ...
随机推荐
- IDEA maven打包时跳过测试
配置这个install -Dmaven.test.skip=true, 可以跳过business项目本地启动自动跑测试用例
- jqentitymanage
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflect ...
- IIS 身份验证
IIS 支持以下身份验证模式: 匿名.如果不需要对客户端进行身份验证(或者使用自定义身份验证机制,如窗体身份验证),则可将 IIS 配置为允许匿名访问.在该事件中,IIS 创建一个 Windows 令 ...
- Head First Python之2函数模块
模块就是一个包含Python代码的文本文件,以.py结尾. 第三方模块都在PyPI(python package index)上,可使用PyPI发布你的模块,供他人使用. 注释代码 # coding= ...
- create-react-app设置proxy反向代理不起作用
在CRA2.X升级以后对proxy的设置做了修改,引用官方升级文档: Object proxy configuration is superseded by src/setupProxy.js To ...
- [转]android中最好的瀑布流控件PinterestLikeAdapterView
PinterestLikeAdapterView 项目地址:https://github.com/GDG-Korea/PinterestLikeAdapterView 使用方法类似于ListView下 ...
- DataType--数值类型
SQL Server数值类型可以分为精确数字类型和近似数字类型精确数字BIT/SMALLINT/TINYINT/INT/BIGINTNUMERIC/DECIMAL/SMALLMONEY/MONEY 近 ...
- 干掉MessageBox,自定义弹出框JMessbox (WindowsPhone)
先上效果图 QQ退出效果 ...
- $(this)在ajax里面不生效的探究
第一个箭头时, 如果没有将$(this) 赋值给 _this ,那么$(this)就无法在ajax方法里面使用. 应该是应为他们属于不同的域. 赋值给_this的话, 就类似于全局变量
- .Net Core .Net Core V1.0 创建MVC项目
.Net Core V1.0 创建MVC项目 创建MVC项目有两种方式: 一.创建Web项目:(有太多没用的东西要去删太麻烦) 2.项目目录结构: 此种方法要注意的是,会创建好多个json文件,下面就 ...