[Codeforces] 650A - Watchmen

A. Watchmen
time limit per test 
3 seconds
memory limit per test 256 megabytes
input 
standard input
 
output 
standard output

Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).

They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan considers the distance between watchmen i and j to be |xi - xj| + |yi - yj|. Daniel, as an ordinary person, calculates the distance using the formula .

The success of the operation relies on the number of pairs (i, j) (1 ≤ i < j ≤ n), such that the distance between watchman i and watchmen j calculated by Doctor Manhattan is equal to the distance between them calculated by Daniel. You were asked to compute the number of such pairs.

Input

The first line of the input contains the single integer n (1 ≤ n ≤ 200 000) — the number of watchmen.

Each of the following n lines contains two integers xi and yi (|xi|, |yi| ≤ 109).

Some positions may coincide.

Output

Print the number of pairs of watchmen such that the distance between them calculated by Doctor Manhattan is equal to the distance calculated by Daniel.

Examples
input
3
1 1
7 5
1 5
output
2
input
6
0 0
0 1
0 2
-1 1
0 1
1 1
output
11
Note

In the first sample, the distance between watchman 1 and watchman 2 is equal to |1 - 7| + |1 - 5| = 10 for Doctor Manhattan and  for Daniel. For pairs (1, 1), (1, 5) and (7, 5), (1, 5) Doctor Manhattan and Daniel will calculate the same distances.

 

题意

给出一个点集,定义一个二元关系成立条件如下:两点距离原点的曼哈顿距离和欧几里得距离相等,求点集中有多少对这种符合关系的点集

 

分析

其实只要两个点符合Xi == Xj或者Yi == Yj就行了(具体为什么画个Rt三角形就行),重点是如何处理同坐标的点。

首先根据各点X排序一下,然后计算同X点的关系数。在这里先把同一个坐标的那些点忽略不计(注意)

之后根据各点Y排序,这次将同坐标的点和同Y点一起加起来。

顺便,CF要求不能用long long要用__int64和%I64d ...

 

代码

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #define maxn 200000
 7 using namespace std;
 8 
 9 struct Poi{
10     __int64 x,y;
11 }list[maxn*2];
12 
13 bool com_x(Poi A,Poi B){
14     if(A.x < B.x) return true;
15     if(A.x == B.x) return A.y < B.y;
16     if(A.x > B.x) return false;
17 }
18 
19 bool com_y(Poi A,Poi B){
20     if(A.y < B.y) return true;
21     if(A.y == B.y) return A.x < B.x;
22     if(A.y > B.y) return false;
23 }
24 
25 int main(){
26     __int64 n,cnt1 = 1,cnt2 = 1,ans = 0;
27     scanf("%I64d",&n);
28     for(int i = 0;i < n;i++){
29         scanf("%I64d%I64d",&list[i].x,&list[i].y);
30     }
31     
32     sort(list,list+n,com_x);
33     
34     for(int i = 1;i < n;i++){
35         if(list[i].x == list[i-1].x){
36             cnt1++;
37             if(list[i].y == list[i-1].y) cnt2++;
38             else{
39                 ans -= cnt2*(cnt2-1)/2;
40                 cnt2 = 1;
41             }
42         }else{
43             ans += cnt1*(cnt1-1)/2;
44             cnt1 = 1;
45             ans -= cnt2*(cnt2-1)/2;
46             cnt2 = 1;
47         }
48     }
49     
50     ans += cnt1*(cnt1-1)/2-cnt2*(cnt2-1)/2;
51     
52     cnt1 = 1;
53     sort(list,list+n,com_y);
54     
55     for(int i = 1;i < n;i++){
56         if(list[i].y == list[i-1].y) cnt1++;
57         else{
58             ans += cnt1*(cnt1-1)/2;
59             cnt1 = 1;
60         }
61     }
62     
63     ans += cnt1*(cnt1-1)/2;
64     
65     printf("%I64d",ans);
66     
67     return 0;
68 }
Sure to read??

 

理论基础

算法设计

转载于:https://www.cnblogs.com/Chorolop/p/7148107.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值