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 ...
随机推荐
- js类、原型——学习笔记
js 内置有很多类,我们用的,都是从这些类实例化出来的. function Object () {} function Array () {} function String () {} functi ...
- iOS 中集成海康威视 摄像视频
本文原文地址 http://www.cnblogs.com/qianLL/p/6652104.html 一.要导入相关的库,注意 这里比较坑的是 要用和他一样的 如果开始的工程中用了AFN或者MJE ...
- 初识react native遇到的问题
Andriod 使用react native时遇到的问题 打开现有项目报错: 从第一行Error可以知道是一个zip的压缩文件打不开,往下看应该是下载的Gradle文件有问题,提示也是让从新下 ...
- scala打印error,debug,info
1.以wordcount为例 package org.apache.spark.examples import org.apache.spark.examples.SparkPi.logger imp ...
- MSSQL 重新生成索引,重新组织索引
> 5% 且 < = 30% ALTER INDEX REORGANIZE > 30% ALTER INDEX REBUILD WITH (ONLINE = ON)* * 重新生成索 ...
- 富文本KindEditor使用
1.官网down KindEditor,添加到自己的项目中:添加时可把不需要的文件夹干掉,asp/php等等.我的项目用的是纯html和js,直接调用后台api: 2.页面引入相关js.eclipse ...
- servlet 生命周期 与 初始化
一. 生命周期 Servlet 通过调用 init () 方法进行初始化. Servlet 调用 service() 方法来处理客户端的请求. Servlet 通过调用 destroy() 方法终止( ...
- hexo_config.yml配置内容
# Hexo Configuration ## Docs: https://hexo.io/docs/configuration.html ## Source: https://github.com/ ...
- laravel jwt 做API 退出登录(注销) 该怎么弄? 如何让token失效
laravel jwt 做API 退出登录(注销) 该怎么弄? 如何让token失效 php框架 laravel 2.1k 次浏览 问题对人有帮助,内容完整,我也想知道答案0问题没有实际价值,缺少关键 ...
- autorun - 自动装载/卸载CDROMs并在装载后执行/path/to/cdrom/autorun
总览 autorun [-lmqv?V] [-a EXEC] [-c CDPLAYER] [-e STRING] [-i MILLISEC] [-n STRING] [-t STRING] [--au ...