博客
关于我
【NOIP2017提高A组模拟8.10】JZOJ7月27日提高组T3 计算几何
阅读量:300 次
发布时间:2019-03-03

本文共 1510 字,大约阅读时间需要 5 分钟。

【NOIP2017提高A组模拟8.10】JZOJ7月27日提高组T3 计算几何

题目描述:在x轴和y轴的正半轴上各有n个点,这2n个点要练成n条线段,要求任意两条线段不相交。有m个询问,每次询问给出点P(x, y),问线段OP与多少条线段相交(O为原点(0,0))。

分析:由于线段两两不相交,可以将x轴和y轴上的点从小到大排序。对于每条线段,可以求出其斜率k和截距b。对于每个询问的点P,可以求出OP的直线方程,然后找到与其他线段的交点,并判断交点是否在线段上。

代码实现:使用二分查找法快速确定交点位置,确保高效处理大量询问。代码中函数pd用于判断中间点是否满足交点条件,通过对x和y进行排序,确保线段不会相交。

题解:通过排序和二分查找,快速确定每条线段的交点位置,判断交点是否在线段上,从而计算OP与多少条线段相交。代码实现了高效的查询方法,确保在大规模数据下也能快速响应。

【题目已解答,以下为代码示例】

#include 
#include
using namespace std;bool pd(int x) { double x1, y1, kk; kk = 1.0 * y[x] / xx; x1 = b[x] / (kk - k[x]); y1 = x1 * kk; if (x1 <= x && y1 <= y) return true; else return false;}int main() { freopen("T3.in", "r", stdin); freopen("T3.out", "w", stdout); scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &x[i]); } for (i = 1; i <= n; i++) { scanf("%d", &y[i]); } sort(x + 1, x + n + 1); sort(y + 1, y + n + 1); for (i = 1; i <= n; i++) { b[i] = y[i]; k[i] = -1.0 * y[i] / x[i]; } scanf("%d", &m); for (i = 1; i <= m; i++) { scanf("%d%d", &xx, &yy); l = 1; r = n; ans = 0; while (l <= r) { mid = (l + r) >> 1; if (pd(mid)) { ans = mid; l = mid + 1; } else { r = mid - 1; } } printf("%d\n", ans); } fclose(stdin); fclose(stdout); return 0;}

代码解释:首先读取输入,排序x和y轴上的点,然后计算每条线段的斜率和截距。对于每个询问,使用二分查找法确定OP直线与其他线段的交点,判断交点是否在线段上,返回相交的线段数量。

转载地址:http://gqql.baihongyu.com/

你可能感兴趣的文章
npm学习(十一)之package-lock.json
查看>>
npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
查看>>
npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
查看>>
npm安装教程
查看>>
npm报错Cannot find module ‘webpack‘ Require stack
查看>>
npm报错Failed at the node-sass@4.14.1 postinstall script
查看>>
npm报错fatal: Could not read from remote repository
查看>>
npm报错File to import not found or unreadable: @/assets/styles/global.scss.
查看>>
npm报错TypeError: this.getOptions is not a function
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
查看>>
npm版本过高问题
查看>>
npm的“--force“和“--legacy-peer-deps“参数
查看>>
npm的安装和更新---npm工作笔记002
查看>>
npm的常用操作---npm工作笔记003
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>
npm设置淘宝镜像、升级等
查看>>
npm设置源地址,npm官方地址
查看>>