↓ Skip to main content
  1. Posts/

poj 1971 Parallelogram Counting

·446 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:给出n(n<=1E3)个不同的点,问最多组成多少个平行四边形。

思路:这道题的关键是,对于平行四边形的判断条件,要利用平行四边形对角线的交点平分两条对角线的性质。

也就是说,如果两条线段的对角线重合,那么一定可以组成一个平行四边形。

因此统计中点的位置即可,复杂度nnlg(n*n)

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年11月22日 星期二 22时43分26秒
 4File Name :code/poj/1971.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33struct point
34{
35    int x,y;
36    bool operator < (point b)const
37    {
38	if (x==b.x) return y<b.y;
39	return x<b.x;
40    }
41}p[1005],mid[1001*1001];
42int n;
43int main()
44{
45	#ifndef  ONLINE_JUDGE
46	freopen("code/in.txt","r",stdin);
47  #endif
48	int T;
49	cin>>T;
50	while (T--)
51	{
52	    ms(mp,0);
53	    scanf("%d",&n);
54	    for ( int i = 1; i <= n ; i++) scanf("%d%d",&p[i].x,&p[i].y);
55	    int cnt = 0 ;
56	    for ( int i = 1; i <= n ; i++)
57		for ( int j = i +1 ; j <= n ;j++)
58		{
59		    mid[++cnt].x = p[i].x + p[j].x;
60		    mid[cnt].y = p[i].y + p[j].y;
61		}
62
63	    sort(mid+1,mid+cnt+1);
64	    LL ans = 0 ;
65	    int num = 0 ;
66	    for ( int i = 1; i <= cnt-1 ; i++)
67	    {
68		if (mid[i].x==mid[i+1].x&&mid[i].y==mid[i+1].y)
69		{
70		    num++;
71		    ans = ans + num;
72		}
73		else
74		{
75		    num = 0 ;
76		}
77	    }
78	    printf("%lld\n",ans);
79	}
80
81
82  #ifndef ONLINE_JUDGE
83  fclose(stdin);
84  #endif
85    return 0;
86}

Related

bzoj 2456: mode (O(1)找到出现次数大于n/2的数)

·398 words·1 min
2456: mode # Time Limit: 1 Sec Memory Limit: 1 MB Submit: 3887 Solved: 1636 [Submit][Status][Discuss] Description # 给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数。 Input # 第1行一个正整数n。 第2行n个正整数用空格隔开。

【叉姐的魔法训练第一课_初级魔法练习】poj 3244 Difference between Triplets (数学)

·565 words·2 mins
题目链接 题意: # For every pair of triplets, Ta = (Ia, Ja, Ka) and T__b = (Ib, Jb, Kb), we define the difference value between Ta and_T__b_ as follows: D(Ta,_ Tb_) = max {Ia − Ib, Ja − Jb, Ka − Kb} − min {Ia − Ib, Ja − Jb, Ka − Kb} Now you are given N triplets, could you write a program to calculate the sum of the difference values between every unordered pair of triplets?

light oj 1045 Digits of Factorial (k进制数的位数)

·276 words·1 min
题目链接 题意:求n!在k进制表示下有多少位。 思路:答案为[ log(1)+log(2)+…+log(N) ]+1 其中log的底数都是K 由于有多组数据,预处理一个log的前缀和。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :Tue 13 Sep 2016 05:13:15 PM CST 4File Name :code/loj/1045.cpp 5************************************************ */ 6#include <cstdio> 7#include <cstring> 8#include <iostream> 9#include <algorithm> 10#include <vector> 11#include <queue> 12#include <set>-digits-of-factorial-k进制数的位 13#include <map> 14#include <string> 15#include <cmath> 16#include <cstdlib> 17#include <ctime> 18#define fst first 19#define sec second 20#define lson l,m,rt<<1 21#define rson m+1,r,rt<<1|1 22#define ms(a,x) memset(a,x,sizeof(a)) 23typedef long long LL; 24#define pi pair < int ,int > 25#define MP make_pair 26using namespace std; 27const double eps = 1E-8; 28const int dx4[4]={1,0,0,-1}; 29const int dy4[4]={0,-1,1,0}; 30const int inf = 0x3f3f3f3f; 31const int N=1E6+7; 32int n; 33int base; 34double sum[N]; 35int main() 36{ 37 #ifndef ONLINE_JUDGE 38 freopen("code/in.txt","r",stdin); 39 #endif 40 sum[0] = 0 ; 41 for ( int i = 1 ; i < N ; i++) sum[i] = sum[i-1] + log(i); 42 int T; 43 cin>>T; 44 int cas = 0 ; 45 while (T--) 46 { 47 scanf("%d%d",&n,&base); 48 double ans = sum[n]/log(base)+1; 49 printf("Case %d: %d\n",++cas,int(ans)); 50 } 51 #ifndef ONLINE_JUDGE 52 fclose(stdin); 53 #endif 54 return 0; 55}

codeforces #368 div 2 C. Pythagorean Triples (构造,数学)

·953 words·2 mins
题目链接 题意:给出一个数,问包含这个数三个数组成的勾股数,输出另外两个数。 思路: 所谓勾股数,就是当组成一个直角三角形的三边长都为正整数时,我们就称这一组数为勾股数. 那么,组成一组勾股数的三个正整数之间,是否具有一定的规律可寻呢?下面我们一起来观察几组勾股数: 规律一:在勾股数(3,4,5)、(5,12,13)、(7,24,25)(9,40,41)中,我们发现 由(3,4,5)有:32=9=4+5 由(5,12,13)有:52=25=12+13 由(7,24,25)有:72=49=24+25 由(9,40,41)有:92=81=40+41. 即在一组勾股数中,当最小边为奇数时,它的平方刚好等于另外两个连续的正整数之和.因此,我们把它推广到一般,从而可得出以下公式: ∵(2n+1)²=4n²+4n+1=(2n²+2n)+(2n²+2n+1) ∴(2n+1)²+(2n²+2n)²=(2n²+2n+1)²(n为正整数) 证明(略) 勾股数公式一:(2n+1,2n²+2n,2n²+2n+1)(n为正整数) 规律二:在勾股数(6,8,10)、(8,15,17)、(10,24,26)中,我们发现 由(6,8,10)有:62=36+2×(8+10) 由(8,15,17)有:82=64=2×(15+17) 由(10,24,26)有:102=100=2×(24+26) 即在一组勾股数中,当最小边为偶数时,它的平方刚好等于两个连续整数之和的二倍,推广到一般,从而可得出另一公式: ∵(2n)2=4n2=2[(n2-1)+(n2+1)] ∴(2n)2+(n2-1)2=(n2+1)2(n≥2且n为正整数) 证明(略) 勾股数公式二:(2n,n²-1,n²+1)(n≥2且n为正整数) 利用以上两个公式,我们可以快速写出各组勾股数.