I fumo 星(STL,数学)

ops/2024/9/23 11:54:03/

登录—专业IT笔试面试备考平台_牛客网

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

假设平面上有 nnn 颗 fumo 星,编号分别为 1,2,…,n1,2,\dots,n1,2,…,n。求这样的直线的数量:直线经过这 nnn 颗 fumo 星中至少 222 个序号不同的 fumo 星。

输入描述:

首先输入一行一个整数 n(1≤n≤103)n(1\leq n \leq 10^{3})n(1≤n≤103),表示 fumo 星的数量。接下来输入 n 行,每行 2 个整数 xi,yi(−109≤xi≤109,−109≤yi≤109)代表序号为 i 的 fumo 星的横坐标与纵坐标。

输出描述:

如果有无数条满足的直线,输出 "inf"(不含引号);否则输出一行一个整数,表示直线的数量。

示例1

输入

2
1 1
2 2

输出

1

示例2

输入

4
0 0
1 1
2 2
3 3

输出

1

示例3

输入

2
1 1
1 1

输出

inf

解析: 

看完题目,先别急着做题,先看数据范围,再看时间限制。发现时间限制很大,我们可以使用n^2级别的时间复杂度来处理这个问题。

最简单的方法就是将所有的点都遍历一遍,每次遍历的时候枚举所有可能与它形成直线的点。同时使用gcd将斜率化成最小分数,使用map或其他容器存储每个点形成的所有直线。

根据初中知识,确定一条直线只需要一个点和一个斜率即可。

因此可以这样存一条直线:map<点,map<斜率,是否存在>>mp;

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<sstream>
#include<deque>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<double, double> PDD;
const int INF = 0x3f3f3f3f;
const LL Mod = 1e9;
const int N = 1e3 + 10, M = 4e5 + 10, P = 110;
int n;
struct node {int x, y;
}node[N];
map<int, map<pair<int,int>, int>>mp;
map<PII, int>p;int gcd(int a, int b) {if (b == 0)return a;return gcd(b, a % b);
}int main() {cin >> n;int flg = 0;for (int i = 1; i <= n; i++) {scanf("%d%d", &node[i].x, &node[i].y);if (p.count({ node[i].x,node[i].y })) {flg = 1;}p[{node[i].x,node[i].y}]=1;}int ans = 0;if (flg) {cout << "inf" << endl;}else {for (int i = 1; i <= n; i++) {for (int j = 1; j < i; j++) {int x = node[i].x - node[j].x, y = node[i].y - node[j].y;int g = gcd(x, y);x /= g;y /= g;if (!mp[j].count({x,y})) {ans++;}mp[j][{x,y}] = 1;mp[i][{x, y}] = 1;}}cout << ans << endl;}return 0;
}

再提供一个队友写的代码,效率更高:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<sstream>
#include<deque>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
using namespace std;
const int N = 1e4 + 10;
#define LL long long 
tuple<LL, LL, LL> o;
set<tuple<LL, LL, LL> > p;
LL a[N], b[N];
LL n;
LL gcd(LL x, LL y)
{if (!y) return x;return gcd(y, x % y);
}
int main()
{cin >> n;for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];if (n == 1){cout << 0 << endl;return 0;}int flag = 0;for (int i = 1; i <= n; i++){for (int j = i + 1; j <= n; j++){if (a[i] == a[j] && b[i] == b[j]){flag = 1;break;}LL x = b[j] - b[i], y = a[j] - a[i], z = y * b[i] - x * a[i];LL gc = gcd(gcd(x, y), z);if (!gc) p.insert({ x,y,z });else {x = x / gc;y /= gc;z /= gc;p.insert({ x,y,z });}}if (flag)break;}if (flag){cout << "inf" << endl;return 0;}cout << p.size() << endl;return 0;}


http://www.ppmy.cn/ops/9250.html

相关文章

设计模式—门面模式

定义: 门面模式,也称为外观模式&#xff0c;是一种结构型设计模式。它的主要目的是提供统一的接口来访问子系统中的多个接口&#xff0c;从而简化客户端与复杂子系统之间的交互。 在门面模式中&#xff0c;一个门面类充当中介&#xff0c;为客户端提供一个简化了的访问方式&…

Oracle EBS Interface/API(54)- GL日记账审批

背景: 客户化创建薪酬凭证或者银企付款入账日记账以后,用户希望自动提交审批流程,无需到系统标准功能点击审批,减少用户操作。 快速参考 参考点内容功能导航N: GL->日记账->输入并发请求None基表GL.GL_JE_BATCHESAPI参考下面介绍错误信息表None接口FormNone接口Reque…

【matlab 代码的python复现】 Matlab实现的滤波器设计实现与Python 的库函数相同实现Scipy

实现一个IIR滤波器的设计 背景 Matlab 设计的滤波器通常封装过于完整,虽然在DSP中能够实现更多功能的滤波器设计但是很难实现Python端口的实现。 我们以一段原始的生物电信号EEG信号进行处理。 EEG信号 1.信号获取 EEG信号通常通过头皮电极,经过多通道采样芯片采样,将获…

Vulnhub靶机 DC-6 打靶实战 详细渗透测试过程

Vulnhub靶机 DC-6 详细渗透流程 打靶实战 目录 Vulnhub靶机 DC-6 详细渗透流程 打靶实战一、将靶机导入到虚拟机当中二、渗透测试主机发现端口扫描信息探测web渗透目录爆破爆破后台密码反弹shell搜集有价值信息SSH远程登录提权反弹jens用户权限的shell 提权利用 一、将靶机导入…

Linux学习 - 管道、标准输入输出

Linux学习 - 管道、标准输入输出 Linux下的标准输入、输出、重定向、管道 在Linux系统中&#xff0c;有4个特殊的符号&#xff0c;<, ‘>’, ‘|’, ‘-‘&#xff0c;在我们处理输入和输出时存在重要但具有迷惑性的作用。 默认Linux的命令的结果都是输出到标准输出&a…

外包干了6天,技术明显退步。。。

我是一名大专生&#xff0c;自19年通过校招进入湖南某软件公司以来&#xff0c;便扎根于功能测试岗位&#xff0c;一晃便是近四年的光阴。今年3月&#xff0c;我如梦初醒&#xff0c;意识到长时间待在舒适的环境中&#xff0c;已让我变得不思进取&#xff0c;技术停滞不前。更令…

HarmonyOS ArkUI实战开发-NAPI数据类型

在前两篇文章里笔者简单介绍了 NAPI 工程结构以及生成的 cpp 源码部分&#xff0c;其中 JS 应用层传递过来的数据被封装在了 napi_value 中&#xff0c;使用前先要转换成对应的 C/C 数据类型&#xff0c;C/C 端的数据也要转换成 napi_value 数据类型传递给 JS 应用层&#xff0…

c++中的数据类型

一、整形 数据类型存在的意义&#xff1a;给变量分配合适的内存空间 #include <iostream> using namespace std;int main(){//整型//1.短整型(-32768~32767)short num1 32768;//2.整型(-2147483648~2147483647)int num2 32768;//3.长整型(-9223372036854775808~922337…