poj2002 数正方形 hash

POJ2002 正方形的个数(二维点的哈希) 给定N个点,求出这些点一共可以构成多少个正方形。 若正方形为ABCD,A坐标为(x1, y1),B坐标为(x2, y2),则很容易可以推出C和D的坐标。对于特定的A和B坐标,C和D可以在线段AB的左边或者右边,即有两种情况。 因此只需要枚举点A和点B,然后计算出两种对应的C和D的坐标,判断是否存在即可。这样计算完之后得到的答案是正确答案的4倍,因为正方形的4条边都枚举了,所以答案要右移两位。 阅读详情

直接上渣代码了

#include <iostream>
using namespace std;
struct gtype {
       int x,y,next;
}g[3000];
int first[1000100],tot,n,x[3010],y[3010],ans,h,tx,ty;
int hash(int x,int y) {
    return (40000*x+y+800040000)%999991+1;
}
bool find(int x,int y) {
     int h = hash(x,y);
     bool f = false;
     for (int t=first[h];t!=-1;t=g[t].next)
         if (g[t].x==x && g[t].y==y) {     
            f = true;
            break;
         } 
     return f;
}
int main() {
    while (~scanf("%d",&n)) {
          tot = 0;
          memset(first,-1,sizeof(first));
          for (int i=1;i<=n;i++) {
              scanf("%d%d",&x[i],&y[i]);
              h = hash(x[i],y[i]);
              tot++;
              g[tot].x = x[i];
              g[tot].y = y[i];
              g[tot].next = first[h];
              first[h] = tot;
          }
          ans = 0;
          for (int i=1;i<=n;i++)
              for (int j=i+1;j<=n;j++) {
                  if (i==j) continue;
                  tx = x[i]-x[j];
                  ty = y[i]-y[j];
                  if (find(x[i]+ty,y[i]-tx) && find(x[j]+ty,y[j]-tx)) ans++;
                  if (find(x[i]-ty,y[i]+tx) && find(x[j]-ty,y[j]+tx)) ans++;
              }
          printf("%d\n",ans/4);
    }
    return 0;
}


POJ2002Squares【求点集中正方形的个数 计算几何+Hash】 Language: Default Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 17755   Accepted: 6781 Description A square is a 4-sided polygon whose sides have e 阅读详情

相关推荐

poj 2002 Squares (哈希+正方形已知相邻两点求另外两点的坐标)

poj 2002 Squares (哈希+正方形已知相邻两点求另外两点的坐标)

WinForBest 1213

poj 2002

二分查找一个点是否存在,用set无法避免素数陷阱,无法解决负数取模问题,用map也无法解决。 只能hash或者二分 #include <iostream> #include <cstdio> #include <iomanip> #include <string> #include <cstdlib> #include <cstri...

Fawkess的博客 270

【简单实现版】【无手写哈希】Poj 2002正方形的个数 Enjoy.

描述 给定直角坐标系中的若干整点,请寻找可以由这些点组成的正方形,并统计它们的个数。 输入 包括多组数据,每组数据的第一行是整点的个数n(1<=n<=1000),其后n行每行由两个整数组成,表示一个点的x、y坐标。输入保证一组数据中不会出现相同的点,且坐标的绝对值小于等于20000。输入以一组n=0的数据结尾。 输出 对于每组输入数据,输出一个数,表示这组数据中的点可以组成的正方形的数量。 样例输入 4 1 0 0 1 1 1 0 0 9 0 0 1 0 2 0 0 2 1 2 2 2 0 1

Sanfeng_W的博客 1124

POJ 2002 Squares 数学 + 必须hash

http://poj.org/problem?id=2002 只能说hash比二分快很多。随便一个hash函数都可以完爆二分。 判断是否存在正方形思路如下: 1、枚举任意两个点,作为正方形的一条边,那么,整个正方形就确定了,有两个方向。 因为, 设枚举的坐标为(x1, y1) & (x2, y2),所求的坐标是和x1,y1这个点相连,那么有方程如下。 1、垂直,向量积是0 2...

weixin_30466421的博客 136

POJ2002-Squares

北大POJ2002-Squares 解题报告+AC代码

【UE5+Quest3】手部模型替换---3-0

这次手部模型替换遇到了一些小问题,不过好在是bug一个个都处理掉了,我会分开几个章节去整理。详细的视频教程可以参考油管大佬的视频手部素材放到文章末尾,自行下载使用。(仅用作学习研究,不可用于商用)

qbbmnnnnnn的博客 944

POJ 2002 快速查找

这是一道几何+快速查找的题目。 根据扩展方向的不同,主要有两种坐标的形式。我们只需要选择想一个方向的扩展,因为我们需要枚举所有的节点。但是这要做还是会有重复的,根据AB求CD坐标和根据AD求BC坐标,其实这两种确定的是一个正方形,所以最后的结果需要除以2。     另,枚举两个点,求得另外两个点,判断求出的这两个点是否在给定的点集中。我使用的是二分查找策略,首先需要对坐标先按照x排序,

陈氏春秋 470

超详细MySQL 8.0安装与配置

MySQL的安装与配置 还不会安装MySQL的同学们,还不会配置MySQL的同学们,赶紧按照下面步骤操作起来,超详细安装步骤如下: 进入MySQL官网 1、点击download 2、点击MySQL Community Server 3、选好选项点击go to download page 4、点击download 5、点击no thanks,just start my download 6、选Server only,点击Next 7、点击Execute 8、点击

Turniper的博客 8753

poj_2002 哈希

题意:给定一堆点,判断能构成多少个正方形 枚举前两个点,根据前两个点推算出后两个点,看后两个点在不在表中,若在,ans++,记得最后ans除以4,多算了4倍。 code: #include #include using namespace std; const int HASH_BASE = 1007; int n; int map[1000][2]; int ha

在路上的学习者 564

Wi-Fi Sigma测试系统

Wi-Fi Sigma测试系统是Wi-Fi联盟(WFA)提出的一套全自动的WiFi认证测试系统,几乎涵盖的所有Wi-Fi的功能及性能(throughput)的自动化测试。该系统正逐步取代以前的WiFi认证测试系统。

Ke8323的博客 1万+

通过TMS320F28335学习DSP的开发流程——28335的基础知识(1)

1、TMS320F28335内核的主要特点 ①F28335 DSP集成了DSP和微控制器的长处,如DSP的主要特征、单周期乘法运算,F28335能够在一个周期内完成32x32位的乘法累加运算,或者两个16x16位乘法累加运算,而同样32位的普通单片机则需要4个周期以上才能完成 ②快速的中断响应 ③可在任何内存位置进行单周期读、修改、写操作 ④可采用C/C++编程软件,效率非常高 2、处理器的主要资源 ①32位浮点DSP,主频是150MHz,方便电机控制、电力设备控制及工业控制等。 ②片上存储器:FLASH–

Kayboy的博客 2万+

POJ 2002 Squares

题目链接:https://vjudge.net/problem/POJ-2002 题目大意   多组数据,每组给定 n 个不同的点,问从这 n 个点中选 4 个,使之能连成正方形,有多少种不同的选法? 分析   思路是枚举 2 个点,然后计算出可能的另外两个点,看它们存不存在。   最优解应该是用哈希表(O(n2))做,不过POJ万年不更新,都不能用 unordered_map...

weixin_30646315的博客 138

2024.12.29 【RNA-seq】Rawdata数据缺失问题与解决方案

Fastq格式是一种用于存储高通量测序数据的文件格式,包含序列和相应的质量信息。第一行以开头,后接序列标识符;第二行是测序的DNA序列;第三行以开头,可能会跟随序列标识符;第四行是对应的质量值,以ASCII字符表示。Rawdata的数据完整性通常情况下是没有问题的,但我们仍然需要对下机数据进行验证,确保后续分析的正常进行,好的分析习惯可以提高整体的分析效率,避免很多不必要的麻烦。

1111

POJ - 2002 Squares 数正方形【二分】【哈希表】

Squares  POJ - 2002  题意 平面上有N个点,任取4个点,求能组成正方形的不同组合方式有多少种;相同的四个点,不同顺序构成的正方形视为同一正方形 分析 做法1 先枚举两个点,通过计算得到另外两个点,然后检查点集中是否存在这两个点 (tx,ty)绕坐标原点旋转W(弧度制)之后点的坐标为(x,y) double x = tx * cos(w) - ty * ...

紫芝的博客 2784

PD3.0详解 协议的时序timer和counter计数

前面介绍了关于PD协议的交互,还有协议层数据各个字段的含义,但是一直没有系统的说关于协议的时序,还有部分counter,就是协议允许发送的次数,或者一些其他计数。 大师匈今天说一下关于这方面的东西,这些东西都是非常关键的东西,但是相对来说PD标准内对时序的要求还是比较宽泛,非常容易能够实现这样的要求。 Timer 1、CRCReceiveTimer 0.9~1.1ms 表示每次通信收到goodcrc的时间,必须在这个时间内收到goodcrc, 我们要注意不是在0.9~1.1ms内,而是第三方可以在这个范

大师匈的博客 5007

YOLOv8 超参数调优教程! 使用Ray Tune进行高效的超参数调优!

YOLOv8超参数调优教程!😎 使用Ray Tune进行高效的超参数调优!

Coding... 1万+

poj2002 数正方形 (哈希+几何)

题目传送门题目大意:给你一堆点,问你能组成几个正方形。思路:一开始想的是用对角线的长度来当哈希的key,但判断正方形会太复杂,然后就去找了一下正方形的判断方法,发现已知: (x1,y1) (x2,y2) 则: x3=x1+(y1-y2) y3= y1-(x1-x2) x4=x2+(y1-y2) y4= y2-(x1-x2) 或 x3=x1-(y1...

weixin_30455365的博客 395

【python爬虫--爬取电商商品及对其销量、好差评等数据进行分析】Python爬虫爬取电商商品及大数据分析-实战

选择目标电商网站:这里选择爬取两个不同电商网站(比如京东、淘宝)上的商品数据,包括商品名称、价格、销量和评价信息。编写爬虫:通过爬虫获取商品信息并保存数据。数据清洗与对齐:将两个电商网站抓取到的数据进行统一清洗和对齐。数据分析:进行销量分析、好评与差评分析等。

Dreams°的博客 4011

Poj 2002 Squares (正方形个数 Hash)

题意:平面内一堆点,问其中有多少个正方形,相同的四个点,不同顺序构成的正方形视为同一正方形。 思路:设两个点的坐标为(a1,a2),(b1,b2),若构成正方形,另两个点的坐标是:(a1-(a2-b2), a2+(a1-b1))、(b1-(a2-b2), b2+(a1-b1)))或者:(a1-(a2-b2), a2+(a1-b1))、(b1-(a2-b2), b2+(a1-b1)),注意这样处理

whyorwhnt的专栏 1206

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

POJ - 2002(静态哈希链表找正方形)

POJ - 2002(静态哈希链表找正方形) 第一次做OJ,卡了好久, 稍微总结下. 原理 注意如果正方形有两个点是整数, 余下的两个点一定是整数. 计算公式如下: 已知:(x1,y1),(x2,y2)(x1,y1) ,(x2,y2) 则: x3=x1+(y1−y2)y3=y1−(x1−x2)x3=x1+(y1-y2) \quad y3= y1-(x1-x2) x4=x2+(

leonhe27的博客 702

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

【POJ No. 2002】 正方形 Squares

【POJ No. 2002】 正方形 Squares

代码改变世界 756

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

POJ - 2002 - Squares (hash算法)

题目:poj-2002 题意:给一些点的坐标,问最多可以构成多少个正方形。 直接四个点四个点地枚举肯定超时的,不可取。 普遍的做法是:先枚举两个点,通过数学公式得到另外2个点,使得这四个点能够成正方形。然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形。 但这种做法会使同一个正方形按照不同的顺序被枚举了四次,因此最后的结果要除以4. 题解: 已知: (x1,y1) ...

Rotepad的博客 446

poj 2002 HASH判断正方形

题意:给出一些直角坐标系的点,求这些点可以组成多少个正方形。 思路:hash 正方形一条边的两个点的和,然后求另外两个点,如果可以找到构成这个正方形的点的话,个数加一 #include #include #define M 1031 using namespace std; struct point { int x,y; }; point P[1024]; int cmp(const vo

丿聪丶 710

poj 2002 Squares,hash

poj 2002 Squares 给出n个点,问能组成多少个正方形? 题解: 先把每个点hash 然后枚举两点(即枚举正方形的一条边),然后通过三角形全等,可以推出正方形的另外两点,在hash表里查找这两点看是存在,存在则 Cnt +1。 最后 answer = Cnt/4 //因为同一正方形都统计了4次。 #include #include #in

天亮说晚安 1716

poj2002——求点能组成正方形的个数(二分或hash)

题目链接:http://poj.org/problem?id=2002 A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 9...

mumei的博客 2772
上一篇: poj3003&2397 DP 记录路径
下一篇: poj3682 概率
琴空
博客等级 码龄16年 48粉丝 82原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值