Codeforces Round #345 (Div. 2)(C)排列组合,思维
来源:互联网 发布:手机淘宝网客户端 编辑:程序博客网 时间:2024/05/17 08:06
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.
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.
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.
31 17 51 5
2
60 00 10 2-1 10 11 1
11
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.
题意:给你n个点让你找出满足下式的等式:
|xi - xj| + |yi - yj|==
题解:开始根本不知道怎么入手,暴力肯定是不可以的,1e10肯定会超时的,那么我们可以先去化简等式去找一下规律,
化简后的式子如下:2|xi - xj| * |yi - yj|=0
那么观察一下就可以清晰的看出,要找出满足(xi==xj||yi==yj)这样的点,这样就大大
简化了问题,我们统计x,y相同的点使用排列公式C(N,2)求出全排列,但是这里有
一个问题,我们的点会重复,这样会重复多算一些组合,在x中算过一次,又在y
中算了一次,那么我们只要减去每个相同坐标的内部全排列C(M,2),就会去掉重
复计算的部分,我们使用STL的map维护相同点的个数,使用set维护相同(x/y)
上点的(y/x)的个数,这一题就解决了
#include <set> #include <map> #include <list> #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <sstream> #include <algorithm>#include <utility>#define LL long long using namespace std;#define N int(1e3+5) #define inf 0x3f3f3f3fmap<int,multiset<int>>xx, yy;map<pair<int, int>,int>pp;int main(){#ifdef CDZSC freopen("i.txt", "r", stdin);#endifint n;int x, y;while (~scanf("%d", &n)){xx.clear();pp.clear();yy.clear();for (int i = 0;i<n; i++){scanf("%d%d", &x, &y);xx[x].insert(y);yy[y].insert(x);if (!pp.count(make_pair(x, y)))pp[make_pair(x, y)] = 1;elsepp[make_pair(x, y)]++;}LL ans = 0; map<int, multiset<int> >::iterator it;for (it = xx.begin();it!=xx.end();it++){LL x = it->second.size();ans += ((x - 1)*x) / 2;}for (it = yy.begin(); it !=yy.end(); it++){LL x = it->second.size();ans += ((x-1)*x)/2;}for (map<pair<int, int>, int>::iterator it2 = pp.begin(); it2 != pp.end(); it2++){LL x = it2->second;ans -= (x*(x - 1)) / 2;}printf("%lld\n", ans);}return 0;}
- Codeforces Round #345 (Div. 2)(C)排列组合,思维
- Codeforces Round #353 (Div. 2) C 思维
- Codeforces Round #261 (Div. 2)C题(思维题)
- Codeforces Round #246 (Div. 2)(C)数学,思维
- Codeforces Round #273 (Div. 2)(C)贪心,思维
- Codeforces Round #202 (Div. 2)C. Mafia(思维)
- Codeforces Round #381 (Div. 2)C(构造,思维)
- Codeforces Round #386 (Div. 2)C. Tram(模拟+思维)
- Codeforces Round #309 (Div. 2) C 排列组合+费马小
- Codeforces Round #273 (Div. 2)(B)排列组合
- Codeforces Round #400 (Div. 1 + Div. 2, combined) C:Molly's Chemicals(前缀+思维)
- Codeforces Beta Round #32 (Div. 2) C (math+思维)
- Codeforces Round #386(Div. 2)C. Tram【思维】
- Codeforces Round #392(Div. 2) C Unfair Poll【思维+暴力】
- Codeforces Round #398(Div. 2)C. Garland【Dfs+思维】
- Codeforces Round #401(Div. 2)C. Alyona and Spreadsheet【思维】
- Codeforces Round #411(Div. 2)C. Find Amir【思维】
- Codeforces Round #418 (Div. 2) C 预处理/思维题
- QFrame详解
- Android 热修复Nuwa的原理及Gradle插件源码解析
- Python 字符串 String 内建函数大全(2)
- x264代码剖析(四):vs2010编译x264错误集锦
- cf#345-C - Watchmen-map离散化
- Codeforces Round #345 (Div. 2)(C)排列组合,思维
- iOS个人整理26-沙盒机制和文件管理NSFileManger,文件对接NSFileHander
- Unity中关于图形学的基础知识—矩阵(二)
- Uva725——Division
- ubuntu技巧--ubuntu caffe安装
- J2EE开发之servlet
- Leetcode:27. Remove Element(JAVA)
- 单例设计模式(一)
- Arm linux启动流程