一条直线上N个线段所覆盖的总长度
原文:http://blog.csdn.net/bxyill/article/details/8962832
问题描述:
现有一直线,从原点到无穷大。
这条直线上有N个线段。线段可能相交。
问,N个线段总共覆盖了多长?(重复覆盖的地区只计算一次)
================================================
解题思路:
可以将每个线段拆分成“单位1”
遍历所有线段,使用一个数组记录每个线段所走过的“单位1”
最后统计数组中被走过的中“单位1”的个数,即是所有线段覆盖的总长度了。
这里有个问题?数组的大小如何确定?
数组的大小应该是所有线段中最大的端点坐标。
===============================================
顺便想到一个问题。
给出若干个线段。求一共有几个“连通域”。就是将能合并的线段 合并成一个线段。
最后能合并出几个来?
利用上面的思想。非常简单。
只需遍历单位数组的时候做个开始和结尾的记录就行了。
程序实现如下。
===============================================
- //此题要求
- //求出一条直线上所有线段所覆盖的全程长度是多少。
- //重叠的地方只计算一次。
- //================================
- //本算法的思想是,将每个线段进行像素化,
- //添加到一个单位数组c[N]中
- //遍历c数组判断哪些单位被覆盖到了,
- //在count计数一下就知道一共覆盖了多少路程。
- //真是巧妙啊。
- //==============================
- #include <iostream>
- using namespace std;
- const int N = 10000;
- //线段结构体
- struct Segment
- {
- int start;
- int end;
- };
- //
- int coverage(Segment *segments, int n)
- {
- bool c[N]={false};//每个“单位1”是否被覆盖到
- int start=0;
- int end = 0;
- //遍历n个线段
- for(int i = 0; i < n; i++)
- {
- for(int j = segments[i].start; j < segments[i].end; j++)
- {
- c[j] = true;
- }
- //寻找最右端
- if(segments[i].end > end)
- {
- end = segments[i].end;
- }
- //寻找最左端
- if(segments[i].start < start)
- {
- start = segments[i].start;
- }
- }
- //从最左端开始到最右端。遍历单位数组C
- int count = 0;
- for(int i= start; i < end; i++)
- {
- if(c[i])
- {
- int s=i;
- while(c[i])
- {
- count++;
- i++;
- }
- int e=i;
- cout << "["<<s<<","<<e<<"]"<<endl;
- }
- }
- return count;
- };
- int main()
- {
- Segment s1;
- s1.start = 1;
- s1.end = 3;
- Segment s2;
- s2.start = 2;
- s2.end = 6;
- Segment s3;
- s3.start = 11;
- s3.end = 12;
- Segment s4;
- s4.start = 10;
- s4.end = 13;
- Segment ss[] = {s1,s2,s3,s4};
- cout << "归并后"<<endl;
- cout <<"被覆盖总长度:" <<coverage(ss, sizeof(ss)/sizeof(ss[0]))<<endl;
- }
输出结果如下:
归并后
[1,6]
[10,13]
被覆盖总长度
8
请按任意键继续. . .
一条直线上N个线段所覆盖的总长度的更多相关文章
- lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上
题目 最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...
- LeetCode:149_Max Points on a line | 寻找一条直线上最多点的数量 | Hard
题目:Max Points on a line Given n points on a 2D plane, find the maximum number of points that lie on ...
- lintcode-186-最多有多少个点在一条直线上
186-最多有多少个点在一条直线上 给出二维平面上的n个点,求最多有多少点在同一条直线上. 样例 给出4个点:(1, 2), (3, 6), (0, 0), (1, 3). 一条直线上的点最多有3个. ...
- POJ3304:Segments (几何:求一条直线与已知线段都有交点)
Given n segments in the two dimensional space, write a program, which determines if there exists a l ...
- Lining Up(在一条直线上的最大点数目,暴力)
Lining Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- [LintCode] 最多有多少个点在一条直线上
/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(i ...
- objectarx之判断三点是否在一条直线上
bool CCommonFuntion::IsOnLine(AcGePoint2d& pt1, AcGePoint2d& pt2, AcGePoint2d& pt3){ AcG ...
- 两条直线(蓝桥杯)二分枚举+RMQ
算法提高 两条直线 时间限制:1.0s 内存限制:256.0MB 问题描述 给定平面上n个点. 求两条直线,这两条直线互相垂直,而且它们与x轴的夹角为45度,并且n个点中离这两条 ...
随机推荐
- Github上如何取消fork别人的repository
在Github上如果看到有很不错的项目和作品,一般我们可以进行三种操作:那就是watch, star和fork. Watch也就是关注该repo的动态,star则类似于Facebook和Twitter ...
- 【跟我一起学Python吧】Python 多线程
其实自我感觉Python的多线程很类似于Java的多线程机制,但是比JAVA的多线程更灵活.在早期的Python多线程实现中,采用了thread模块.例如: from time import ctim ...
- leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...
- vector 之 find 重载
众所周知,map有find,但vector的find只能调用algorithm中的find通用方法. 参考<How to find an item in a std::vector?> 对 ...
- HDU 5833 Zhu and 772002 (高斯消元)
Zhu and 772002 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5833 Description Zhu and 772002 are b ...
- CF#190DIV.1
/* CF#190DIV.1-C 题意:给你n个结点的树,给这些结点标记字母AB..Z,对于标记相同的结点路径上 的结点的标记必须有一个是大于该标记的:问是否可以标记(A是最大标记) 分析:整天思路就 ...
- Spring EL Lists, Maps example
In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way ...
- Laravel Configuration
Introduction All of the configuration files for the Laravel framework are stored in the app/config d ...
- [iOS 多线程 & 网络 - 4.0] - AFN框架简单使用
A.AFN基本知识 1.概念 AFNetworking 是对NSURLConnection的封装 运行效率没有ASI高(因为ASI基于CFNetwork),但是使用简单 AFN支持ARC B. ...
- 数据库防sql注入