题意:

求欧几里得距离与曼哈顿距离相等的组数。

分析:

化简后得到xi=xj||yi=yj,即为求x相等 + y相等 - x与y均相等。

代码:

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1000000 + 5;
typedef pair<long long , long long>pii;
pii p[maxn];
bool cmp(pii a, pii b)
{
return a.first < b.first||(a.first == b.first && a.second < b.second);
}
bool cmp1(pii a, pii b)
{
return a.second < b.second||(a.second == b.second &&a.first < b.first);
}
int main (void)
{
int n;cin>>n;
for(int i = 0; i < n; i++){
cin>>p[i].first>>p[i].second;
}
long long resx = 0, resy = 0;
long long cnt = 0;
sort(p, p + n, cmp);
for(int i = 1; i < n; i++){
if(p[i].first == p[i - 1].first)
cnt++;
if(i == n-1||p[i].first != p[i - 1].first){
resx += cnt * (cnt + 1)/2;
cnt = 0;
}
}
sort(p, p + n, cmp1);
cnt = 0;
long long aa = 0, both = 0;
for(int i = 1; i < n; i++){
if(p[i].second == p[i - 1].second){
cnt++;
if(p[i].first == p[i-1].first)
aa++;
if(p[i].first != p[i-1].first){
both += aa * (aa + 1)/2;
aa = 0;
}
}
if(i == n-1||p[i].second != p[i - 1].second){
resy += cnt * (cnt + 1)/2;
cnt = 0;
both += aa * (aa + 1)/2;
aa = 0;
}
}
cout<<resx + resy - both<<endl;
}

也可以用map做,这样可以直接记录x和y均相等的个数。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 200005;
typedef pair<int, int> p;
typedef long long ll;
map<int, int>ma, mb;
map<p, int> mp;
int main (void)
{
int n;cin>>n;
int x, y;
for(int i = 0; i < n; i++){
cin>>x>>y;
ma[x] ++;
mb[y]++;
mp[p(x, y)]++;
}
long long resx = 0, resy = 0, both = 0;
map<int, int>::iterator i;
map<p, int>::iterator j;
for(i = ma.begin(); i != ma.end(); i++){
resx +=(ll) i->second * ( i->second- 1)/2;
}
for(i = mb.begin(); i!=mb.end();i++){
resy += (ll)i->second * ( i->second- 1)/2;
}
for(j = mp.begin(); j != mp.end(); j++){
both += (ll) j->second * ( j->second- 1)/2;
}
cout<<resx + resy - both<<endl;
return 0;
}

对于C++11,for(i = ma.begin(); i != ma.end(); i++)可以直接写成

for(auto x: ma) 

不明白第一种方法如果不分别保存resx,resy,both而是直接对res进行不停的加减,最后会runtime error……..

Codeforces 651C Watchmen【模拟】的更多相关文章

  1. CodeForces 651C Watchmen map

    Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...

  2. codeforces 651C Watchmen

    Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...

  3. CodeForces - 651C Watchmen (去重)

    Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...

  4. 【CodeForces - 651C 】Watchmen(map)

    Watchmen 直接上中文 Descriptions: 钟表匠们的好基友马医生和蛋蛋现在要执行拯救表匠们的任务.在平面内一共有n个表匠,第i个表匠的位置为(xi, yi). 他们需要安排一个任务计划 ...

  5. Codeforces 389B(十字模拟)

    Fox and Cross Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submi ...

  6. codeforces 591B Rebranding (模拟)

    Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...

  7. Codeforces 626B Cards(模拟+规律)

    B. Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...

  8. Codeforces 631C. Report 模拟

    C. Report time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  9. Codeforces 679B. Barnicle 模拟

    B. Barnicle time limit per test: 1 second memory limit per test :256 megabytes input: standard input ...

随机推荐

  1. AJPFX:如何保证对象唯一性呢?

    思想: 1,不让其他程序创建该类对象. 2,在本类中创建一个本类对象. 3,对外提供方法,让其他程序获取这个对象. 步骤: 1,因为创建对象都需要构造函数初始化,只要将本类中的构造函数私有化,其他程序 ...

  2. hihocoder offer收割编程练习赛12 D 寻找最大值

    思路: 可能数据太水了,随便乱搞就过了. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...

  3. classpath 路径和classpath*的区别

    classpath和classpath*区别:  classpath:只会到你的class路径中查找找文件. classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找 ...

  4. laravel学习:模块化caffeinated

    # Modules Extract and modularize your code for maintainability. Essentially creates "mini-larav ...

  5. 卸载钩子 UnhookWindowsHookEx

    The UnhookWindowsHookEx function removes a hook procedure installed in a hook chain by the SetWindow ...

  6. bat运行当前路径下程序

    批处理中获取当前路径的方法可能有好几种,具体有几种我没有研究过,本文只是对其中的两种之间的差别进行简单说明 本文涉及的两个当前路径标示为:%cd%.%~dp0 注:我的系统是win7旗舰版,其它系统没 ...

  7. Less简介及安装

    CSS的短板 作为前端学习者的我们 或多或少都要学些 CSS ,它作为前端开发的三大基石之一,时刻引领着 Web 的发展潮向. 而 CSS 作为一门标记性语言,可能 给初学者第一印象 就是简单易懂,毫 ...

  8. (独孤九剑)--PHP简介与现况

    (1)为什么学习PHP? 1.好就业: 2.入门简单,学习周期短,两个月即可: 3.学习编程思路,使编程习惯更加规范: 4.大公司直招: 5.处理大并发数据: 6.开源,所以更加安全 (2)PHP是什 ...

  9. 【计算机网络】2.5 DNS:因特网的目录服务

    第二章第五节 因特网的目录服务 DNS(域名系统)提供了一种能运行主机名到IP地址转换的因特网目录服务:一方面,他让人能够记住如taobao.com这样的主机别名:另一方面,他提供给路由器可理解的IP ...

  10. ios打电话发短信接口

    电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...