Queen Attack -- 微软2017年预科生计划在线编程笔试第二场
#!/usr/bin/env python
# coding:utf-8
# Queen Attack
# https://hihocoder.com/problemset/problem/1497
# Author: kngxscn
# Date: 2017-04-22
"""
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
There are N queens in an infinite chessboard. We say two queens may attack each other if they are in the same vertical line, horizontal line or diagonal line even if there are other queens sitting between them.
Now given the positions of the queens, find out how many pairs may attack each other?
输入
The first line contains an integer N.
Then N lines follow. Each line contains 2 integers Ri and Ci indicating there is a queen in the Ri-th row and Ci-th column.
No two queens share the same position.
For 80% of the data, 1 <= N <= 1000
For 100% of the data, 1 <= N <= 100000, 0 <= Ri, Ci <= 1000000000
输出
One integer, the number of pairs may attack each other.
样例输入
5
1 1
2 2
3 3
1 3
3 1
样例输出
10
"""
# 统计出每行、每列、每条对角线的 Queen 数量,然后分别求组合个数
def C2(n):
return n*(n-1) / 2
if __name__ == '__main__':
n = int(raw_input())
horizontal = {}
vertical = {}
diagonal1 = {}
diagonal2 = {}
for i in range(n):
r, c = [int(x) for x in raw_input().split(' ')]
if r not in horizontal:
horizontal[r] = 0
horizontal[r] += 1
if c not in vertical:
vertical[c] = 0
vertical[c] += 1
if r-c not in diagonal1:
diagonal1[r-c] = 0
diagonal1[r-c] += 1
if r+c not in diagonal2:
diagonal2[r+c] = 0
diagonal2[r+c] += 1
attack_count = 0
for i in horizontal:
attack_count += C2(horizontal[i])
for i in vertical:
attack_count += C2(vertical[i])
for i in diagonal1:
attack_count += C2(diagonal1[i])
for i in diagonal2:
attack_count += C2(diagonal2[i])
print attack_count
Queen Attack -- 微软2017年预科生计划在线编程笔试第二场的更多相关文章
- 【微软2017年预科生计划在线编程笔试第二场 A】Queen Attack
[题目链接]:http://hihocoder.com/problemset/problem/1497 [题意] 给你n个皇后; 然后问你其中能够互相攻击到的皇后的对数; 皇后的攻击可以穿透; [题解 ...
- 【微软2017年预科生计划在线编程笔试第二场 B】Diligent Robots
[题目链接]:http://hihocoder.com/problemset/problem/1498 [题意] 一开始你有1个机器人; 你有n个工作; 每个工作都需要一个机器人花1小时完成; 然后每 ...
- hihocoder1489 Legendary Items (微软2017年预科生计划在线编程笔试)
http://hihocoder.com/problemset/problem/1489 笔试题第一道,虽然说第一道都很水,但是我感觉这题不算特别水把..这道题我就卡住了我记得,tle,最后只有30分 ...
- 微软2017年预科生计划在线编程笔试 A Legendary Items
思路: 获得第i(i = 0, 1, ..., n - 1)件物品的概率仅由公式p / (1 << i)决定,所以获得这i件物品之间是相互独立的.迭代计算获得所有i件物品的期望再求和即可. ...
- 【微软2017年预科生计划在线编程笔试 A】Legendary Items
[题目链接]:https://hihocoder.com/problemset/problem/1489 [题意] 每轮游戏; 你一开始有p的概率获得超神标记; 如果这轮游戏你没获得超神标记; 那么你 ...
- 【微软2017年预科生计划在线编程笔试 B】Tree Restoration
[题目链接]:https://hihocoder.com/problemset/problem/1490 [题意] 给你一棵树的以下信息: 1.节点个数 2.给出树的每一层从左到右的顺序每个节点的编号 ...
- 2015-微软预科生计划-面试题-Swimming Plans
http://hihocoder.com/problemset/problem/1188 题目大意 Steven在时刻T到达了室内游泳池. 游泳池一共有N条泳道,游泳池两侧分别标记为0和1. 已知除了 ...
- hihocoder #1289 : 403 Forbidden (2016 微软编程笔试第二题)
#1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Sometimes ...
- 微软2016校园招聘在线笔试第二场 题目1 : Lucky Substrings
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if and only if the number of different ch ...
随机推荐
- [NOI2018]归程(kruscal重构树)
[NOI2018]归程 题面太长辣,戳这里 模拟赛上写了一个spfa (关于spfa,它已经死了),然后一个st表水完暴力跑路.考后说是Kruscal重构树或者可持久化并查集???这都是些什么东西.不 ...
- JAVA(-Xms,Xmx,Xmn-XX:newSize,-XX:MaxnewSize,-XX:PermSize,-XX:MaxPermSize)区别
1.-Xms:表示java虚拟机堆区内存初始内存分配的大小,通常为操作系统可用内存的1/64大小即可,但仍需按照实际情况进行分配.2.-Xmx:表示java虚拟机堆区内存可被分配的最大上限,通常为操作 ...
- python-抽象类和抽象方法
需要模块 import abc 抽象类不能实例化 import abc class Animal(metaclass=abc.ABCMeta): #抽象类 @abc.abstractmethod # ...
- 【leetcode】1170. Compare Strings by Frequency of the Smallest Character
题目如下: Let's define a function f(s) over a non-empty string s, which calculates the frequency of the ...
- jquery-时间轴滑动
效果预览图: html: <div class="tim"> <div class="timdiv"> <div class=&q ...
- 整合spring cloud云架构 - 根据token获取用户信息
根据用户token获取yoghurt信息的流程: /** * 根据token获取用户信息 * @param accessToken * @return * @throws Exception */ @ ...
- 【CF1252G】Performance Review(线段树)
题意: n,q<=1e5,a[i],b[i][j]<=1e9,保证能力值互不相同,询问之间保留前面的影响 思路:其实把大于a[1]的看成0,小于的看成1,设第i天小于a[1]的有b[i]个 ...
- Beauty Values
Beauty Values 题意:给$n$个数, 定义它的Beauty Values为所有连续子区间的(区间长度*区间内不同数字的数目)求和 求Beauty Values A[i]数组表示数字i最近一 ...
- 版本基线自动化之Linux
上一篇笔者叙述了如何在windows平台上进行自动化打包,这次采用linux平台 1.start.bat : 调用cleanall.bat脚本 ,从svn服务器中检出代码,并采用7-Zip工具进行压缩 ...
- [CSP-S模拟测试]:计数(DP+记忆化搜索)
题目描述 既然是萌萌哒$visit\text{_}world$的比赛,那必然会有一道计数题啦!考虑一个$N$个节点的二叉树,它的节点被标上了$1\sim N$的编号.并且,编号为$i$的节点在二叉树的 ...