题意:

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

分析:

化简后得到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. Hadoop YARN学习之监控集群监控Nagios(4)

    doop YARN学习之监控集群监控Nagios(4) 1. Nagios是一个流行的开源监控工具,可以用来监控Hadoop集群. 2. 监控基本的Hadoop服务 调试好脚本后命名为chek_res ...

  2. vim插件minibuf配置

    1.去下载网站下载minibufexpl.vim文件放入到~/vim/plugins中,有的系统路径是~/.vim/plugins; 下载网址如下 https://www.vim.org/script ...

  3. avd manager或sdk manager无法打开

    最近开始搞安卓,使用AS启动项目时老是报各种错误,而网上这方面的资料很多都解决不了.只能边实验边做. 定位到avd manager或sdk manager无法打开,网上找了很多资料,都不能解决,知道看 ...

  4. [翻译] API测试最佳实践 - 身份验证(Authentication)

    API测试最佳实践 - 身份验证 适用等级:高级 1. 概况 身份验证通常被定义为是对某个资源的身份的确认的活动,这里面资源的身份指代的是API的消费者(或者说是调用者).一旦一个用户的身份验证通过了 ...

  5. 搜索可用docker镜像

    简介:这一步的目标是学会使用docker search命令来检索可用镜像. 搜索可用的docker镜像 目标: 提示: 正确的命令: 搜索可用的docker镜像 使用docker最简单的方式莫过于从现 ...

  6. RDO、SAD、SATD、λ相关概念【转】

    率失真优化概述: 率失真优化(Rate D isto r t i on Op t i m ized)策略是在率失真理论[3 ]的基础上提出的一种代价函数方案, RDO 的主要思想是, 在计算代价函数时 ...

  7. leetcode_655. Print Binary Tree

    https://leetcode.com/problems/print-binary-tree/ 打印整棵二叉树 class Solution { public: int getTreeHeight( ...

  8. idea 中pom.xml依赖版本号报错(报红,如下图所示)

    1.maven工程中出现的错误 2.解决办法:file->setting->Maven 如果还没好的话请尝试以下方法:

  9. CAD参数绘制填充(com接口)

    填充是CAD图纸中不可或缺的对象,在机械设计行业,常常需要将零部件剖开,以表现其内部的细节,而这些被剖开的截面会用填充来表示:在工程设计行业,一些特殊的材料或地形,也会用填充来表示. C#中实现代码说 ...

  10. 用数据集跑一个模型遇到bug如何解决

    自己在用fast rcnn和ssd跑自己数据集过程中都遇到了bug,fast rcnn中是loss下降但值较高,并且测试出来结果一直不对,ssd是loss从一开始到后面loss都一直为0. 遇到这种情 ...