我是靠谱客的博主 冷酷煎蛋,这篇文章主要介绍Codeforces 650A 数学简单题,现在分享给大家,希望可以做个参考。

题目:http://codeforces.com/problemset/problem/650/A

题意:
给出n个点,问符合和|xi - xj| + |yi - yj|. 这两个公式算出来的两点“距离”相等的点对有几个?
分析:
显然如果上面两个公式相等,那么必须横坐标相等或者纵坐标相等,每次从相等的个数中取出两个的,就是这一相等坐标的排列数,分别按横纵坐标求一遍,但是因为有相等的,这样会重复计算一次,最后把 相等的位置排列数减去就好。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h> using namespace std; const int N=2e5+5; typedef long long ll; typedef pair<int,int>pii; pii a[N]; bool cmp(const pii &x,const pii &y){return x.second<y.second;} int num[N]; int main() { int n;scanf("%d",&n); map<pii,int>mp; int cnt=0;int x,y; for(int i=0;i<n;i++){ scanf("%d%d",&x,&y); pii t=make_pair(x,y); a[i]=t; if(mp.count(t)){ num[mp[t]]++; } else{ mp[t]=cnt++; } } sort(a,a+n); ll ans=0; int i=0; while(i<n){ int t=a[i].first; i++;int sum=1; while(i<n&&a[i].first==t)i++,sum++; if(sum>1){ ans+=(ll)sum*(sum-1)/2; } } sort(a,a+n,cmp); i=0; while(i<n){ int t=a[i].second; i++;int sum=1; while(i<n&&a[i].second==t)i++,sum++; if(sum>1){ ans+=(ll)sum*(sum-1)/2; } } for(map<pii,int>::iterator it=mp.begin();it!=mp.end();it++){ int t=it->second; if(num[t]>=1){ ans-=(ll)num[t]*(num[t]+1)/2; } } cout<<ans<<endl; }

最后

以上就是冷酷煎蛋最近收集整理的关于Codeforces 650A 数学简单题的全部内容,更多相关Codeforces内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部