题目相关

题目链接

AtCoder Beginner Contest 187 B 题,https://atcoder.jp/contests/abc187/tasks/abc187_b。

Problem Statement

On the xyxyxy-plane, We have NNN points numbered 111 to NNN. Point iii is at (xi,yi)(x_i,y_i)(xi​,yi​), and the xxx-coordinates of the NNN points are pairwise different.
Find the number of pairs of integers (i,j)(i<j)(i,j) (i<j)(i,j)(i<j) that satisfy the following condition:
The line passing through Point iii and Point jjj has a slope between −1−1−1 and 111 (inclusive).

Input

Input is given from Standard Input in the following format:

N
xi yi
.
.
.
xn yn

Output

Print the answer.

Sample 1

Sample Input 1

3
0 0
1 2
2 1

Sample Output 1

2

Explaination

The slopes of the lines passing through (0,0)(0,0)(0,0) and (1,2)(1,2)(1,2), passing through (0,0)(0,0)(0,0) and (2,1)(2,1)(2,1), and passing through (1,2)(1,2)(1,2) and (2,1)(2,1)(2,1) are 222, 12\frac{1}{2}21​, and −1−1−1, respectively.

Sample 2

Sample Input 2

1
-691 273

Sample Output 2

0

Sample 3

Sample Input 3

10
-31 -35
8 -36
22 64
5 73
-14 8
18 -58
-41 -85
1 -88
-21 -85
-11 82

Sample Output 3

11

Constraints

  • All values in input are integers.
  • 1 ≤ N ≤ 10^3
  • |xi|,|yi| ≤ 10^3
  • xi≠xj for i≠j.

题解报告

题目翻译

在 xyxyxy 坐标系中,有 NNN 个点,编号为 1−N1 - N1−N,第 iii 个点的坐标为 (xi,yi)(x_i, y_i)(xi​,yi​),这 NNN 个点的坐标各不相同,并且坐标为整数。
请找出有多少个点对 (i,j)(i<j)(i, j) (i < j)(i,j)(i<j) 满足以下条件:
1、连接第 iii 和 jjj 点的直线斜率在 −1-1−1 和 111 之间。

题目分析

本题是一个数学题,核心就是如何求两个点组成的直线斜率。

直线斜率

假设我们有两个点 NiN_iNi​ 和 NjN_jNj​,满足 i<ji < ji<j,对应的坐标为 (xi,yi)(x_i, y_i)(xi​,yi​) 和 (xj,yj)(x_j, y_j)(xj​,yj​),如下图所示。
如图所示,通过这两个点的直线的斜率为:yj−yixj−xi\frac{y_j - y_i}{x_j - x_i}xj​−xi​yj​−yi​​。
因此,本题就是遍历所有可能的两个点组成的直线,计算其斜率,并判断是否符合题目要求。

改进

由于计算斜率我们不可避免会得到一个浮点数,为了优化,我们可以进一步讨论一下数学问题。
满足要求的斜率为:
−1≤yj−yixj−xi≤1⇔∣yj−yixj−xi∣≤1⇔∣yj−yi∣≤∣xj−xi∣-1 \leq \frac{y_j - y_i}{x_j - x_i} \leq 1 \quad \Leftrightarrow \quad \lvert \frac{y_j - y_i}{x_j - x_i} \rvert \leq 1 \quad \Leftrightarrow \quad \lvert y_j - y_i \rvert \leq \lvert x_j - x_i \rvert −1≤xj​−xi​yj​−yi​​≤1⇔∣xj​−xi​yj​−yi​​∣≤1⇔∣yj​−yi​∣≤∣xj​−xi​∣

数据分析

样例数据 1

根据样例数据,我们知道有三个点:(0,0),(1,2),(2,1)(0, 0), (1, 2), (2, 1)(0,0),(1,2),(2,1)。因此可以构成三条直线。
第一条直线,通过点 (0,0)(0, 0)(0,0) 和 点 (1,2)(1, 2)(1,2)。对应的斜率为 2−01−0=2\frac{2-0}{1-0}=21−02−0​=2。
第二条直线,通过点 (0,0)(0, 0)(0,0) 和 点 (2,1)(2, 1)(2,1)。对应的斜率为 1−02−0=12\frac{1-0}{2-0}=\frac{1}{2}2−01−0​=21​。
第三条直线,通过点 (1,2)(1, 2)(1,2) 和 点 (2,1)(2, 1)(2,1)。对应的斜率为 1−22−1=−1\frac{1-2}{2-1}=-12−11−2​=−1。
因此,符合要求的直线有两条,分别是第二条和第三条。

数据范围分析

NNN 的最大值为 10310^3103,因此使用暴力遍历 O(N2)O(N^2)O(N2) 可以满足需求。

AC 代码

//https://atcoder.jp/contests/abc187/tasks/abc187_b
//B - Gentle Pairs
#include <bits/stdc++.h>using namespace std;//如果提交到OJ,不要定义 __LOCAL
//#define __LOCALconst int MAXN=1e3+4;
int x[MAXN];
int y[MAXN];int main() {#ifndef __LOCAL//这部分代码需要提交到OJ,本地调试不使用ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#endifint n;cin>>n;for (int i=1; i<=n; i++) {cin>>x[i]>>y[i];}//int ans=0;for (int i=1; i<n; i++) {for (int j=i+1; j<=n; j++) {if (abs(y[j]-y[i])<=abs(x[j]-x[i])) {ans++;}}}cout<<ans<<"\n";#ifdef __LOCAL//这部分代码不需要提交到OJ,本地调试使用system("pause");
#endifreturn 0;
}

时间复杂度

O(N2)O(N^2)O(N2)。

空间复杂度

O(N)O(N)O(N)。

和使用浮点数比较代码对比

相同的代码,差距在比较使用浮点数。我们仅仅比对时间。

//https://atcoder.jp/contests/abc187/tasks/abc187_b
//B - Gentle Pairs
#include <bits/stdc++.h>using namespace std;//如果提交到OJ,不要定义 __LOCAL
//#define __LOCALconst int MAXN=1e3+4;
int x[MAXN];
int y[MAXN];int main() {#ifndef __LOCAL//这部分代码需要提交到OJ,本地调试不使用ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#endifint n;cin>>n;for (int i=1; i<=n; i++) {cin>>x[i]>>y[i];}//int ans=0;int yy;int xx;double slope;for (int i=1; i<n; i++) {for (int j=i+1; j<=n; j++) {yy=y[j]-y[i];xx=x[j]-x[i];slope=1.0*yy/xx;if (slope>=-1 && slope<=1) {ans++;}}}cout<<ans<<"\n";#ifdef __LOCAL//这部分代码不需要提交到OJ,本地调试使用system("pause");
#endifreturn 0;
}

可以明显的看出,相同的代码,使用浮点数比较需要 15ms,而使用定点数比较,只需要 8ms,速度快了一倍。因此尽量少用浮点数比较。

AtCoder题解 —— AtCoder Beginner Contest 187 —— B - Gentle Pairs —— 暴力相关推荐

  1. AtCoder Beginner Contest 187 F.Close Group Editorial

    AtCoder Beginner Contest 187 F.Close Group Editorial 题目链接 状压DP~ 如果对边暴力的话复杂度约为 21502^{150}2150,显然不可取, ...

  2. AtCoder题解 —— AtCoder Beginner Contest 182 —— D - Wandering

    题目相关 题目链接 AtCoder Beginner Contest 182 D 题,https://atcoder.jp/contests/abc182/tasks/abc182_d. Proble ...

  3. AtCoder题解—— AtCoder Beginner Contest 181 —— B - Trapezoid Sum

    题目相关 题目链接 AtCoder Beginner Contest 181 B 题,https://atcoder.jp/contests/abc181/tasks/abc181_b. Proble ...

  4. AtCoder Beginner Contest 187 F - Close Group

    https://atcoder.jp/contests/abc187/tasks/abc187_f 有点像小米决赛的G题啊,所以就秒了 dp[i]表示i这个状压状态,最少可以是多少连通块组成 先预处理 ...

  5. AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S

    题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...

  6. AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划

    题目相关 题目链接 AtCoder Grand Contest 050 B 题,https://atcoder.jp/contests/agc050/tasks/agc050_b. Problem S ...

  7. AtCoder题解——AtCoder Regular Contest 107——B - Quadruple

    题目相关 题目链接 AtCoder Regular Contest 107 B 题,https://atcoder.jp/contests/arc107/tasks/arc107_b. Problem ...

  8. Atcoder题解与视频集

    开启Atcoder之路 开启Atcoder之路_sortmin的博客-CSDN博客_atcoder怎么用 atcoder心得 atcoder心得_404REN的博客-CSDN博客_atcoder怎么用 ...

  9. AtCoder题解——Beginner Contest 170——F - Pond Skater

    题目相关 题目链接 AtCoder Beginner Contest 170 F题,https://atcoder.jp/contests/abc170/tasks/abc170_f. Problem ...

最新文章

  1. 从起源到具体算法,这篇深度学习综述论文送给你
  2. B2c商城图片尺寸设定研究 尺寸应该多大合适
  3. MySQL LIMIT:限制查询结果的记录条数
  4. 我们为什么推荐在Json中使用string表示Number属性值
  5. 如何为Kafka挑选合适的分区数
  6. 贾老板大秀未来机器人之舞,场面有点不可描述(动图+视频)
  7. 查看Mysql数据库版本
  8. 第一章、Zigbee模块的简介及特点
  9. php获取openid提示错误40163,微信网页授权 40163 code已被使用过
  10. 将Android Studio的设置恢复到初始化(清除所有的设置)
  11. php时区问题,php时区问题
  12. Birt报表开发工具及Birt runtime部署
  13. Vue 2.0 开发聊天程序(二)真正的开始
  14. python如何计算平均分_python脚本如何输入成绩求平均分?
  15. java获取手机通讯录权限_android获取手机通讯录
  16. Dev c++与vs
  17. 〖OKaimi点金胜手_2019年4月〗走地大球分析系统|万胜
  18. Ubuntu 10.10与局域网中的Windows文件共享详细操作步骤
  19. 远程桌面为啥会连接不上?
  20. 伽利略变换的极限式证明及推论

热门文章

  1. echarts地图自定义区域(并添加标记)
  2. 好玩的API调用之---天气预报的API调用与爬虫
  3. 沈阳地区十大市场调查研究咨询公司排名
  4. TU-R BT.601介绍,
  5. GCMS样品前处理-分析-色谱质谱
  6. 叮咚买菜——运力监测
  7. [Stellaris][群星]Mod制作指南-编写中-预计6月完成。。大概
  8. Unity 欢乐球球
  9. 【信息系统项目管理师】2016上半年系统集成项目管理工程师案例分析
  10. python之协程与IO操作