#!/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. [NOI2018]归程(kruscal重构树)

    [NOI2018]归程 题面太长辣,戳这里 模拟赛上写了一个spfa (关于spfa,它已经死了),然后一个st表水完暴力跑路.考后说是Kruscal重构树或者可持久化并查集???这都是些什么东西.不 ...

  2. [每日一学]apache camel|BDD方式开发apache camel|Groovy|Spock

    开发apache camel应用,最好的方式就是tdd,因为camel的每个组件都是相互独立并可测试的. 现在有很多好的测试框架,用groovy的Spock框架的BDD(行为测试驱动)是比较优秀和好用 ...

  3. Ubuntu安装SFTP服务,及启动失败处理

    安装openssh-server sudo apt-get install openssh-server 查看是否安装成功 dpkg --get-selections | grep ssh 新建用户组 ...

  4. layui token 过期 重新登陆

    这个方法你要全局设置     //jquery全局配置 $.ajaxSetup({     cache: false,     crossDomain: true,       headers :{' ...

  5. gremlin语言语法--学习笔记

    学习gremlin语言的目的:测试图数据,支持gremlin语句,所以必须系统学习一下!!!! 一.基础查询 g.V() 查询所有的顶点 g.V(3) 查询顶点id为3的点.字符串id的要到引号V(& ...

  6. redis面试题集錦

    1为什么Redis需要把所有数据放到内存中? Redis为了达到最快的读写速度将数据都读到内存中,并通过异步的方式将数据写入磁盘.所以Redis具有快速和数据持久化的特性.如果不将数据放到内存中,磁盘 ...

  7. clang和llvm的安装

    https://blog.csdn.net/qq_31157999/article/details/78906982

  8. Day01_课后练习题

    1.(将摄氏温度转化华氏温度)编写一个从控制台读取摄氏温度并将他转变为华氏温度并予以显示的程序.转换公式如下. Fahrenheit = (9 / 5) *  celsius + 32 这里是这个程序 ...

  9. asp.net大文件上传解决方案

    以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传  ...

  10. 旧题再做【bzoj2287】【[pojchallenge]消失之物】分治背包

    (上不了p站我要死了) 今天听了 doggu神 讲了这道题的另一种做法,真是脑洞大开.眼界大开.虽然复杂度比黄学长的要大一点,但不总结一下简直对不起这神思路. 方法1:黄学长的做法(点这里) Desc ...