acmicpc.sdnu.edu.cn 1012区间合并
来源:互联网 发布:店铺宝贝优化 编辑:程序博客网 时间:2024/05/17 14:17
Description:
给定n个开区间,合并他们中的重合者,输出合并后的区间数量。
Input:
第一行:n(1 <= n <= 1000)
第2至第n+1行:每行两个整数,第i行的两个整数表示第i-1个区间的左边界和右边界。
Output:
合并后所剩余的区间数量。
一道经典的并查集问题。
并查集问题讨论关于不相交集合的问题,对于任意两个集合A,B,若有元素x∈A则x∉B,即可以通过集合中任一元素代表该集合。新问题是如何选择代表该集合的元素?代表元素最好能惰性变化。
于是在集合中添加数据结构(即数据间相互关系),由于树有且仅有且必须有一个根节点,于是选择树形结构作为集合内数据逻辑关系模型。
而问题将分为两个子问题:①两线段是否重合,②集合合并问题
①两线段是否重合,纯几何问题,仅提供True或False以标记是否合并
②集合合并,当发现集合A与集合B出现重叠,将二者重合。
#include <stdio.h>struct NODE{int left,right;int father;}node[1010];bool Judge(int a, int b){if(node[b].left >= node[a].right || node[b].right <= node[a].left){return false;}else return true;}void Union(int a, int b){int af = a;int bf = b;while(node[af].father != af) af = node[af].father;while(node[bf].father != bf) bf = node[bf].father;if(af < bf){node[bf].father = af;}else if(bf < af){node[af].father = bf;}}int main(){int n;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d%d",&node[i].left, &node[i].right);node[i].father = i;}for(int i=0;i<n-1;i++){for(int k=i+1;k<n;k++){if(Judge(i,k)){Union(i,k);}}}int sum = 0;for(int i=0;i<n;i++){if(node[i].father == i) sum++;}printf("%d\n",sum);return 0;}
0 0
- acmicpc.sdnu.edu.cn 1012区间合并
- acmicpc.sdnu.edu.cn 1011盒子与球
- http://acm.hdu.edu.cn/showproblem.php?pid=3308&&线段树之区间合并
- SDNU 1310.Cover Interval 区间合并之后求覆盖的区间长度
- course.cug.edu.cn
- mashang6.edu.cn
- http://acm.hdu.edu.cn/showproblem.php?pid=1698 成段更新,总区间求和
- http://acm.hdu.edu.cn/showproblem.php?pid=1754 更新节点,区间最值
- http://acm.hdu.edu.cn/showproblem.php?pid=1166 更新节点,区间求和
- http://acm.hdu.edu.cn/showproblem.php?pid=2871 更新最左区间
- http://acm.hdu.edu.cn/showproblem.php?pid=1541 结点更新求最左区间的个数
- http://acm.hdu.edu.cn/showproblem.php?pid=1754&&线段树区间求最值
- http://acm.hdu.edu.cn/showproblem.php?pid=1166&&线段树区间求和
- http://acm.pku.edu.cn/JudgeOnline/problem?id=3667 区间更新+求满足长短的最左区间
- http://pat.zju.edu.cn/contests/pat-practise/1012
- http://pat.zju.edu.cn/contests/pat-practise/1012
- http://chenhaifeng.blog.edu.cn/
- http://zjc.wuse.edu.cn/
- LogCat
- undo表空间的作用、收缩及切换
- Codeforces Round 281 div2 C Vasya and Basketball
- Mongodb与Mysql的查询脚本操作对比
- 分布式异步消息框架构建笔记5——如何避开并行编程中的数据共享陷阱
- acmicpc.sdnu.edu.cn 1012区间合并
- Android ViewHolder通用写法(简洁、减少代码量)
- 关于前端开发谈谈单元测试
- One Edit Distance
- 在服务器端判断request来自Ajax请求(异步)还是传统请求(同步)
- javascript考试系统倒计时
- 主页面调用iframe里面匿名Javascript函数的问题
- 心里装着什么,就会得到什么!
- android控件开发之TimePicker和DatePicker