*[codility]Number-of-disc-intersections
http://codility.com/demo/take-sample-test/beta2010/
这题以前做的时候是先排序再二分,现在觉得没有必要。首先圆可以看成线段,把线段的进入作为一个事件,出去作为一个事件。注意根据题意,同样的点,进入要在出去之前。那么O(n)扫一遍就可以得到结果。注意的是A[i]+i可能会超出int。
// you can also use includes, for example:
#include <algorithm>
int solution(const vector<int> &A) {
// write your code in C++98
vector<pair<long long, int> > intervals;
for (int i = 0; i < A.size(); i++) {
intervals.push_back(make_pair(((long long)i)-A[i], -1));
intervals.push_back(make_pair(((long long)i)+A[i], 1));
}
sort(intervals.begin(), intervals.end());
int ans = 0;
int height = 0;
for (int i = 0; i < intervals.size(); i++) {
pair<long long, int> pp = intervals[i];
if (pp.second == -1) { // start point
ans += height;
height++;
if (ans > 10000000) return -1;
}
else { // end point
height--;
}
}
return ans;
}
*[codility]Number-of-disc-intersections的更多相关文章
- HDU1086You can Solve a Geometry Problem too(判断线段相交)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- *HDU 1086 计算几何
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- hdu 1086 You can Solve a Geometry Problem too
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- hdu 1086(计算几何入门题——计算线段交点个数)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2 ...
- [转]100个经典C语言程序(益智类问题)
目录: 1.绘制余弦曲线 2.绘制余弦曲线和直线 3.绘制圆 4.歌星大奖赛 5.求最大数 6.高次方数的尾数 8.借书方案知多少 9.杨辉三角形 10.数制转换 11.打鱼还是晒网 12.抓交通肇事 ...
- UVA 10790 How Many Points of Intersection?
How Many Points of Intersection? We have two rows. There are a dots on the top row and b dots on ...
- 【计算几何初步-线段相交】【HDU1089】线段交点
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- you can Solve a Geometry Problem too(hdoj1086)
Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare ...
- UVa 10806 Dijkstra,Dijkstra(最小费用最大流)
裸的费用流.往返就相当于从起点走两条路到终点. 按题意建图,将距离设为费用,流量设为1.然后增加2个点,一个连向节点1,流量=2,费用=0;结点n连一条同样的弧,然后求解最小费用最大流.当且仅当最大流 ...
- 【集训笔记】计算几何【HDOJ2036【HDOJ1086【HDOJ1115【HDOJ1147【HDOJ1392 【ZOJ2976
改革春风吹满地 Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted ...
随机推荐
- JAXB - Annotations, The Annotation XmlElement
The basic annotation for a field that's intended to be an element is XmlElement. It permits you to d ...
- 未能加载文件或程序集“App_global.asax”或它的某一个依赖项
未能加载文件或程序集"App_global.asax"或它的某一个依赖项.生成此程序集的运行时比当前加载的运行时新,无法加载此程序集. 出现这一问题的原因是空间支持framewor ...
- makefile文件制作入门
一.首先,看一下最简单的C文件 //hello.c文件 #include <stdio.h> void main() { printf("hello world\n") ...
- java中实现多态的机制是什么?
多态性是面向对象程序设计代码重用的一个重要机制,我们曾不只一次的提到Java多态性.在Java运行时多态性:继承和接口的实现一文中,我们曾详细介绍了Java实现运行时多态性的动态方法调度:今天我们再次 ...
- 18_高级映射:一对一查询(使用resultMap)
[简述] 数据库模型和数据等信息与上一篇博文相同. 需求也同上一篇博文. [工程截图] [User.java]POJO package cn.higgin.mybatis.po; import jav ...
- STL容器与配接器
STL容器包括顺序容器.关联容器.无序关联容器 STL配接器包括容器配接器.函数配接器 顺序容器: vector 行为类似于数组,但可以根据要求 ...
- 九度OJ 1348 数组中的逆序对 -- 归并排序
题目地址:http://ac.jobdu.com/problem.php?pid=1348 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求 ...
- PHP扩展开发(1):入门
有关PHP扩展开发的文章.博客已经很多了,比较经典的有: TIPI项目(http://www.php-internals.com/,强烈推荐) <Extending and Embedding ...
- SSH+Ajax实现用户名重复检查(一)
1.struts.xml设置 <package name="default" namespace="/" extends="json-defau ...
- Ajax请求过程中显示“进度”的简单实现
Ajax在Web应用中使用得越来越频繁.在进行Ajax调用过程中一般都具有这样的做法:显示一个GIF图片动画表明后台正在工作,同时阻止用户操作本页面(比如Ajax请求通过某个按钮触发,用户不能频繁点击 ...