Codeforces Round #345 (Div. 1) A. Watchmen 模拟加点
题意:有n (1 ≤ n ≤ 200 000) 个点,问有多少个点的开平方距离与横纵坐标的绝对值之差的和相等;
即
= |xi - xj| + |yi - yj|.(|xi|, |yi| ≤ 109)
思路:开始想的是容斥原理,即按x,y分别排序,先计算同x的点,然后在计算同y的点,这时由于相同的点之间的连边已经算过了,这样就不能再算。并且同一个y的点中可以每个点有多个点,算是不好编码的(反正我敲了很久..WA了)
反思:上面的容斥原理是从总体的思路来考虑的,这道题的难点也就是不重不漏。那么我们就模拟题目输入来往平面加点即可;每次加入一个点时,看与其同x,y有多少个点,这时由于当前点加了两次所以减去即可;坐标值较大,用map表示即可;
时间复杂度O(n)
#include<bits/stdc++.h>
using namespace std;
typedef __int64 ll;
map<int,int> x,y;
map<pair<int,int>,int> mp; int main()
{
int n,x,y;
ll ans=;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&x,&y);
ans += x[a] + y[b] - mp[MK(a,b)];
x[a]++;
y[b]++;
mp[MK(a,b)]++;
}
printf("%I64d\n",ans);
}
Codeforces Round #345 (Div. 1) A. Watchmen 模拟加点的更多相关文章
- Codeforces Round #345 (Div. 1) A - Watchmen 容斥
C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...
- Codeforces Round #345 (Div. 1) A. Watchmen
A. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #345 (Div. 1) A. Watchmen (数学,map)
题意:给你\(n\)个点,求这\(n\)个点中,曼哈顿距离和欧几里得距离相等的点对数. 题解: 不难发现,当两个点的曼哈顿距离等于欧几里得距离的时候它们的横坐标或者纵坐标至少有一个相同,可以在纸上画一 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】
A. Joysticks time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...
- Codeforces Round #345 (Div. 2)
DFS A - Joysticks 嫌麻烦直接DFS暴搜吧,有坑点是当前电量<=1就不能再掉电,直接结束. #include <bits/stdc++.h> typedef long ...
- Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集
题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...
- Codeforces Round #345 (Div. 2) C (multiset+pair )
C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...
随机推荐
- 多台Linux服务器SSH相互访问无需密码--转
一.环境配置 1.系统:CentOS release 5.6 IP:192.168.4.200 主机名:JW01 2.系统:CentOS release 5.9 IP:192.168.4. ...
- linux剪切拷贝
1.剪切 mv rename.sh ../rename.sh 把这个文件移到上一级目录下 mv rename.sh ./pic/rename.sh 把这个文件移到 当前目录下的pic目录下,并改名 ...
- volley框架详解
Volley可是说是把AsyncHttpClient和Universal-Image-Loader的优点集于了一身,既可以像AsyncHttpClient一样非常简单地进行HTTP通信,也可以像Uni ...
- About xvfb
http://blog.csdn.net/span76/article/details/11473315 有时候我们不关注程序是否有界面(比如自动化测试),只要程序在运行就可以了 很感谢 xvfb 这 ...
- jvm内存模型及分配参数
jvm内存模型 程序计数器:是一块很小的内存空间.当线程数量超过cpu数量时,线程之间根据时间片轮询抢夺cpu资源.每一个线程都必须用一个独立的程序计数器,用于记录下一条要运行的指令. java虚拟机 ...
- readint writeint
inline int readint() { char c = getchar(); while(!isdigit(c)) c = getchar(); ; while(isdigit(c)) { x ...
- try{return} finally
今天去逛论坛 时发现了一个很有趣的问题: 谁能给我我解释一下这段程序的结果为什么是:2.而不是:3? 代码如下: class Test { public int aaa() { int x = 1; ...
- PHP基础1
PHP:相当于客户端和MySQL之间的一堵墙 Apache(阿帕奇):是web服务器软件 localhost:相当于一个域名 一.wampserver http.conf:用来配置Apache p ...
- MySQL 에서 root 암호 변경하기
먼저 터미널이나 SSH를 이용해 접속합니다. 그리고 리눅스 쉘상에서 다음과 같이 입력합니다. [mysqladmin을 이용한 암호변경] 형식) root계정]$mysqladmin -u ...
- Linux - 查看系统的版本信息
在 Linux 中,有多种方法可以查看系统的版本信息. uname 命令 huey@huey-K42JE:~$ uname -a Linux huey-K42JE 3.5.0-43-generic # ...