首页 > 其他分享 >hdoj Lining Up 1432 (数学)直线过最多点问题

hdoj Lining Up 1432 (数学)直线过最多点问题

时间:2023-04-19 15:36:35浏览次数:40  
标签:case 直线 1432 Lining Up points line include define

Lining Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1251    Accepted Submission(s): 356


Problem Description ``How am I ever going to solve this problem?" said the pilot. 


Indeed, the pilot was not facing an easy task. She had to drop packages at specific points scattered in a dangerous area. Furthermore, the pilot could only fly over the area once in a straight line, and she had to fly over as many points as possible. All points were given by means of integer coordinates in a two-dimensional space. The pilot wanted to know the largest number of points from the given set that all lie on one line. Can you write a program that calculates this number? 

Your program has to be efficient! 
 
Input The input consists of multiple test cases, and each case begins with a single positive integer on a line by itself indicating the number of points, followed by N pairs of integers, where 1 < N < 700. Each pair of integers is separated by one blank and ended by a new-line character. No pair will occur twice in one test case. 

 
Output For each test case, the output consists of one integer representing the largest number of points that all lie on one line, one line per case.  
Sample Input 5 1 1 2 2 3 3 9 10 10 11  
Sample Output 3 //题意: 给你n个点的坐标,现在问一条直线能穿过最多的点的个数。 //思路:直线方程:y=k*x+b; 因为两点确定一条直线,所以用两层for可以将所有的直线找出来,用一个结构体存放直线的k,b,因为知道k,b就可以唯一确定一条直线。 所以找k,b相同的直线的最多条数就行了,当然得把重复的去掉。运用去重公式即可(具体在代码中)。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 710
#define M 100000000
using namespace std;
struct zz
{
	int x;
	int y;
}p[N];
int cmp(zz a,zz b)
{
	if(a.x==b.x)
		return a.y<b.y;
	return a.x<b.x;
}
struct ss
{
	double k;
	double b;
}pp[N*N];
bool cmpp(ss a,ss b)
{
	if(a.k==b.k)
		return a.b<b.b;
	return a.k<b.k;
}
int main()
{
	int n,i,j,k;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;i++)
			scanf("%d%d",&p[i].x,&p[i].y);
		sort(p,p+n,cmp);
		int kk=0;
		int km=0;
		for(i=0;i<n-1;i++)
		{
			for(j=i+1;j<n;j++)
			{
				int x=p[j].x-p[i].x;
				int y=p[j].y-p[i].y;
				pp[kk].k=y*1.0/x;
				pp[kk].b=p[i].y-pp[kk].k*p[i].x;
				kk++;
			}
		}
		sort(pp,pp+kk,cmpp);
		int m=1,mm=1;
		ss f=pp[0];
		for(i=1;i<kk;i++)
		{
			if(fabs(pp[i].k-f.k)<1e-6&&fabs(f.b-pp[i].b)<1e-6)
			{
				m++;
				mm=max(mm,m);
			}
			else
			{
				f=pp[i];
				m=1;
			}
		}
		if(n==1)
			printf("1\n");
		else if(n==2)
			printf("2\n");
		else
		{
			if(mm==3)
				printf("%d\n",mm);
			else
			{
				mm*=2;
				for(int i=1;i<=n;i++) //去重公式 
				{
					if(i*(i+1)==mm)
					{
						printf("%d\n",i+1);
						break;
					}
				}
			}
		}
	}
	return 0;
}


标签:case,直线,1432,Lining,Up,points,line,include,define
From: https://blog.51cto.com/u_16079508/6206437

相关文章

  • super关键字和方法重写
    1.super关键字介绍super代表父类的引用,用于访问父类的属性、方法、构造器2.基本语法297代码在com.stulzl.super_.包中父类Apackagecom.stulzl.super_;publicclassA{//4个属性publicintn1=100;protectedintn2=200;intn3=300;private......
  • mysql中对于 GROUP_CONCAT 函数的长度限制处理
    今天才知,原来GROUP_CONCAT函数返回的长度默认是有限制的:mysql>SHOWVARIABLESLIKE"group_concat_max_len";可见,默认是最长不超过1024。 修改mysql的配置参数增加限制:vi/etc/my.cnf[mysqld]group_concat_max_len=1024000 注意,有些文章里说设置成-1也可以,意......
  • GROUP BY+join获取全部数据
     参考链接:groupby聚合分组后如何获取分组数据_group分组后返回全部数据_自己收藏学习的博客-CSDN博客SELECTr.device_id,GROUP_CONCAT(r.user_idSEPARATOR';')user_idfromrelatedasrJOINdevicesasdond.id=r.device_id#whered.group_id=1GROUPBYr.device......
  • VMware vSphere 8.0 Update 1 正式版发布 - 企业级工作负载平台
    ESXi8.0U1&vCenterServer8.0U1请访问原文链接:https://sysin.org/blog/vmware-vsphere-8-u1/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org2023-04-18,VMwarevSphere8.0Update1正式版发布。企业级工作负载平台vSphere将云计算的优势引入本地部署工作负......
  • IDEA启动报错Internal error. Please refer to https://jb.gg/ide/critical-startup-e
    CMD右键以管理员身份运行netshwinsockreset完成后重启netshwinsockreset命令,作用是重置Winsock目录。如果一台机器上的Winsock协议配置有问题的话将会导致网络连接等问题,就需要用netshwinsockreset命令来重置Winsock目录借以恢复网络。......
  • CS144 计算机网络 Lab0:Networking Warmup
    前言本科期间修读了《计算机网络》课程,但是课上布置的作业比较简单,只是分析了一下Wireshark抓包的结构,没有动手实现过协议。所以最近在哔哩大学在线学习了斯坦福大学的CS144计算机网课程,这门课搭配了几个Lab,要求动手实现一个TCP协议,而不是简单地调用系统为我们提供好的So......
  • cronolog工具切割nohup运行日志
    1.日志分割:随着JAVA服务线上运行,默认单个日志文件占用磁盘空间会越来越大,查看文件信息不方便,故需要对日志文件进行分割,这里借用第三方工具cronolog切割,因为网上有很多种切分方式,要么不行要么不好用。 2.安装cronologA.yum在线安装:yuminstall-ycronolog;B.rpm离......
  • Unable to create an object of type 'NetcoremvcDbcontext'. For the different patt
    问题描述:我整个项目重新生成没有报错,但是用efcore迁移数据库命令:Add-Migrationinit就生成不了文件夹Migrations,并且报错:Unabletocreateanobjectoftype'NetcoremvcDbcontext'.Forthedifferentpatternssupportedatdesigntime,seehttps://go.microsoft.com/fwlink/......
  • gitea backup sh
    #!/bin/bash#Thisscriptcreatesa.zipbackupofgitearunninginsidedockerandcopiesthebackupfiletothebackupdirectoryecho"Deleteolderbackup..."find/home/mason/gitea/gitea_backup/-typef-mtime+9-name"*.zip"-de......
  • pg 大版本升级方法 及 pg_upgrade就地升级测试
    一、主要升级方法PostgreSQL自身有三种大版本升级的方法:三种方法升级建议架构如下: 另外根据pg大会介绍,还有一种升级工具叫做PgQ其特点如下 二、 pg_upgrade就地升级测试 1.测试环境测试postgresql9.6升级至postgresql10.4源库:9.6环境prefix目录:/data/PRD/postgres/base/......