#!/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年预科生计划在线编程笔试第二场的更多相关文章

  1. 【微软2017年预科生计划在线编程笔试第二场 A】Queen Attack

    [题目链接]:http://hihocoder.com/problemset/problem/1497 [题意] 给你n个皇后; 然后问你其中能够互相攻击到的皇后的对数; 皇后的攻击可以穿透; [题解 ...

  2. 【微软2017年预科生计划在线编程笔试第二场 B】Diligent Robots

    [题目链接]:http://hihocoder.com/problemset/problem/1498 [题意] 一开始你有1个机器人; 你有n个工作; 每个工作都需要一个机器人花1小时完成; 然后每 ...

  3. hihocoder1489 Legendary Items (微软2017年预科生计划在线编程笔试)

    http://hihocoder.com/problemset/problem/1489 笔试题第一道,虽然说第一道都很水,但是我感觉这题不算特别水把..这道题我就卡住了我记得,tle,最后只有30分 ...

  4. 微软2017年预科生计划在线编程笔试 A Legendary Items

    思路: 获得第i(i = 0, 1, ..., n - 1)件物品的概率仅由公式p / (1 << i)决定,所以获得这i件物品之间是相互独立的.迭代计算获得所有i件物品的期望再求和即可. ...

  5. 【微软2017年预科生计划在线编程笔试 A】Legendary Items

    [题目链接]:https://hihocoder.com/problemset/problem/1489 [题意] 每轮游戏; 你一开始有p的概率获得超神标记; 如果这轮游戏你没获得超神标记; 那么你 ...

  6. 【微软2017年预科生计划在线编程笔试 B】Tree Restoration

    [题目链接]:https://hihocoder.com/problemset/problem/1490 [题意] 给你一棵树的以下信息: 1.节点个数 2.给出树的每一层从左到右的顺序每个节点的编号 ...

  7. 2015-微软预科生计划-面试题-Swimming Plans

    http://hihocoder.com/problemset/problem/1188 题目大意 Steven在时刻T到达了室内游泳池. 游泳池一共有N条泳道,游泳池两侧分别标记为0和1. 已知除了 ...

  8. hihocoder #1289 : 403 Forbidden (2016 微软编程笔试第二题)

    #1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Sometimes ...

  9. 微软2016校园招聘在线笔试第二场 题目1 : Lucky Substrings

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if and only if the number of different ch ...

随机推荐

  1. PAT Basic 1046 划拳 (15 分)

    划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒.两人同赢或两人同输 ...

  2. python基础练习题2

    01:python九九乘法表 for i in range(1,10): for j in range(1,i+1): print('{}*{}={}'.format(j,i,i*j),end='\t ...

  3. 前端之CSS:属性操作1

    css之操作属性 1.文本 1.文本颜色:color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB(255,0, ...

  4. 如何在vue中引入图片?

    当我们在Vue.js项目中引用图片时,关于图片路径有以下几种情形: 使用一. 我们在data里面定义好图片路径 imgUrl:'../assets/logo.png' 然后,在template模板里面 ...

  5. linux-LVM磁盘扩容

    查看磁盘 [ops@stock_kline_database ~]$ sudo fdisk -l 磁盘 /dev/sda: 字节, 个扇区 Units = 扇区 of * = bytes 扇区大小(逻 ...

  6. SQL Server性能调优--优化建议(二)

    序言 优化建议 库表的合理设计对项目后期的响应时间和吞吐量起到至关重要的地位,它直接影响到了业务所需处理的sql语句的复杂程度,为提高数据库的性能,更多的把逻辑主外键.级联删除.减少check约束.给 ...

  7. POJ 3764 The xor-longest Path ( 字典树求异或最值 && 异或自反性质 && 好题好思想)

    题意 : 给出一颗无向边构成的树,每一条边都有一个边权,叫你选出一条路,使得此路所有的边的异或值最大. 分析 : 暴力是不可能暴力的,这辈子不可能暴力,那么来冷静分析一下如何去做.假设现在答案的异或值 ...

  8. 软件工程 in MSRA Code Search-第二次结对编程

    重现基线模型 我们选择了 code2vec 模型进行复现.该模型由 Uri Alon 等作者于 2018 年提出. 模型思路: 从代码与普通语言相比的特殊性入手,首先,对于输入的代码段,作者考虑到尽管 ...

  9. 关联规则挖掘--Apriori算法

  10. warp(图像仿射变换)

    仿射变换是一种二维坐标(x,y)到二维坐标(u,v)的线性变换. 对应的齐次坐标矩阵表示形式为: 仿射变换特点: 直线经仿射变换后依然为直线: ’直线之间的相对位置关系保持不变,平行线经仿射变换后依然 ...