【并查集的另一个思考方向】POJ1456
这个题一看好像就是用贪心做啊,一个结构体,拍一下序,vis数组一遍遍扫荡,最后输出值,没错,贪心的确能做出来,而这类题目也能应用并查集,实现得思想也是贪心
#include <iostream>
#include <string.h>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1e4 + 1e2;
int pre[maxn];
int n;
struct node
{
int p,d;
}obj[maxn];
void init()
{
for(int i = 0;i < maxn;i++)
{
pre[i] = i;
}
memset(obj,0,sizeof(obj));
}
bool cmp(node a,node b)
{
return a.p > b.p;
}
一开始都差不多哈,并查集pre【i】得含义是第i天前第一个空得天数是第几天!!(差不多了吧和你反向暴力一遍vis数组貌似差不多)
但是并查集能路径压缩欸
int Find(int x)
{
if(x != pre[x])
{
int tem = pre[x];
pre[x] = Find(tem);
}
return pre[x];
}
int main()
{
int n,a,d;
while(~scanf("%d",&n))
{
init();
for(int i = 0;i < n;i++)
{
scanf("%d%d",&obj[i].p,&obj[i].d);
}
sort(obj,obj+n,cmp);
int ans = 0;
for(int i = 0;i < n;i++)
{
int r = Find(obj[i].d);
if(r > 0)
{
ans += obj[i].p;
pre[r] = r - 1;
}
}
printf("%d\n",ans);
}
return 0;
}
这个题并不算难,主要是提供了另一个思考的方向吧,现在不是出初中高中得应试阶段,每一个类型都有固定得做法,而ACM这种题,就应该去创新的想一想,去应用更多好玩得数据结构,去做更多好玩得事情
【并查集的另一个思考方向】POJ1456的更多相关文章
- poj 1182 食物链 并查集的又一个用法
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41584 Accepted: 12090 Descripti ...
- 线段树 or 并查集 (多一个时间截点)
There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...
- D. Restructuring Company 并查集 + 维护一个区间技巧
http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...
- POJ1182--食物链(经典并查集)并查集看不出来系列2
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65906 Accepted: 19437 Description ...
- 【BZOJ2959】长跑 (LCT+并查集)
Time Limit: 1000 ms Memory Limit: 256 MB Description 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室 ...
- Marked Ancestor [AOJ2170] [并查集]
题意: 有一个树,有些节点染色,每次有两种操作,第一,统计该节点到离它最近的染色父亲结点的的号码(Q),第二,为某一个节点染色(M),求第一种操作和. 输入: 输入由多个数据集组成.每个数据集都有以下 ...
- POJ1182:食物链(并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 94930 Accepted: 28666 Description ...
- J. The Volcano Eruption(圆相交+并查集)
题目链接:https://codeforces.com/gym/101915/problem/J 思路:将所有相交的圆用并查集维护看做一个整体,然后枚举每个整体的左边界和右边界,判断能不能同时覆盖整个 ...
- 并查集补集作法 codevs 1069 关押罪犯
1069 关押罪犯 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description ...
随机推荐
- Find Amir CodeForces 805C
http://codeforces.com/contest/805/problem/C 题意:有n个学校,学校的编号是从1到n,从学校i到学校j的花费是(i+j)%(n+1),让你求遍历完所有学校的最 ...
- XiaoKL学Python(C)__future__
__future__ in Python 1. from __future__ import xxxx 这是为了在低版本的python中使用可能在某个高版本python中成为语言标准的特性,从而 在将 ...
- 探索未知种族之osg类生物---器官初始化一
我们把ViewerBase::frame()比作osg这类生物的肺,首先我们先来大概的看一下‘肺’长什么样子,有哪几部分组成.在这之前得对一些固定的零件进行说明,例如_done代表osg的viewer ...
- fedora25的免密码rsync服务配置
目标:实现免密同步数据: 1.安装rsync包: 2.手工添加配置文件: cat /etc/rsyncd.conf # See rsyncd.conf man page for more opt ...
- 09. pt-fingerprint
vim pt-fingerprint.txt select name, password from user where id=5;select name, password from user wh ...
- PC 上的 LVM 灾难修复
LVM 介绍 LVM 简介 LVM 是逻辑盘卷管理(Logical Volume Manager)的简称,最早是 IBM 为 AIX 研发的存储管理机制.LVM 通过在硬盘和分区之间建立一个逻辑层,可 ...
- Spring MVC 中的 forward redirect Flash属性
forward:转发 redirect:重定向 -- 转发比重定向快,因为重定向经过客户端,而转发并没有. -- 重定向能够重定向到一个外部网站,但转发不行. -- 重定向能够避免在用户重新加载页面时 ...
- Common tasks that you can perform with the Groovy Script test step
https://support.smartbear.com/readyapi/docs/soapui/steps/groovy.html Get test case object To obtain ...
- Spring IOC(六)依赖查找
Spring IOC(六)依赖查找 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring BeanFactory ...
- Koko Eating Bananas LT875
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The g ...