Codeforces 651C Watchmen【模拟】
题意:
求欧几里得距离与曼哈顿距离相等的组数。
分析:
化简后得到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【模拟】的更多相关文章
- CodeForces 651C Watchmen map
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...
- codeforces 651C Watchmen
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...
- CodeForces - 651C Watchmen (去重)
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn t ...
- 【CodeForces - 651C 】Watchmen(map)
Watchmen 直接上中文 Descriptions: 钟表匠们的好基友马医生和蛋蛋现在要执行拯救表匠们的任务.在平面内一共有n个表匠,第i个表匠的位置为(xi, yi). 他们需要安排一个任务计划 ...
- Codeforces 389B(十字模拟)
Fox and Cross Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submi ...
- codeforces 591B Rebranding (模拟)
Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...
- Codeforces 626B Cards(模拟+规律)
B. Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...
- Codeforces 631C. Report 模拟
C. Report time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Codeforces 679B. Barnicle 模拟
B. Barnicle time limit per test: 1 second memory limit per test :256 megabytes input: standard input ...
随机推荐
- Oracle本地动态 SQL
本地动态 SQL 首先我们应该了解什么是动态 SQL,在 Oracle数据库开发 PL/SQL块中我们使用的 SQL 分为:静态 SQL语句和动态 SQL语句.所谓静态 SQL指在 PL/SQL块中使 ...
- 解决::processDebugResourcesERROR: In<declare-styleable> FontFamilyFont编译报错
cordova编译时报错 错误信息 :processDebugResourcesERROR: In <declare-styleable> FontFamilyFont, unable t ...
- Node.js——环境变量
- 模块 (Module)
#1.模块概念的官网描述 -- Module If you quit from the Python interpreter and enter it again, the definitions y ...
- 迅速搞懂JavaScript正则表达式之方法
咱们来看看JavaScript中都有哪些操作正则的方法. RegExp RegExp 是正则表达式的构造函数. 使用构造函数创建正则表达式有多种写法: new RegExp('abc');// /ab ...
- 常见的HTTP相应状态码
200:请求被正常处理204:请求被受理但没有资源可以返回206:客户端只是请求资源的一部分,服务器只对请求的部分资源执行GET方法,相应报文中通过Content-Range指定范围的资源.301:永 ...
- SpringMVC+ajax返回JSON串
一.引言 本文使用springMVC和ajax做的一个小小的demo,实现将JSON对象返回到页面,没有什么技术含量,纯粹是因为最近项目中引入了springMVC框架. 二.入门例子 ①. 建立工程, ...
- extjs传递参数
以前我是这么传的: 先获取表Form,再通过表找组件,然后用gradeCode=0&groupCode=1传 现在只需要先获得表,然后通过form.getValues()取得所有的值,再使用E ...
- 零基础入门学习Python(16)--序列!序列!
前言 你可能发现了,小甲鱼把这个列表,元组,字符串放在一起讲是有道理的,它们有许多共同点: 都可以通过索引得到每一个元素 默认索引值总是从0开始 可以通过分片的方法得到一个范围内的元素的集合 有很多共 ...
- Linux的发展史和centos7的安装
目 录 第1章 Linux系统介绍与环境搭建准备UNIX操作系统 1 1.1 Unix的发展 1 1.2 MINIX的发展 1 1.3 GUN 1 1.4 Linux的 ...