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 ...
随机推荐
- 022-OpenStack 中虚拟机hostname问题
第一种: openstack中直接使用 hostnamectl 修改主机名,主机名在内核中的信息会被立即修改,但是当系统重启之后,主机名又重新变成原来的主机名称了.openstack主机名由cloud ...
- PAT Basic 1027 打印沙漏 (20 分)
本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号:各行符号中心对齐:相邻两 ...
- Codeforces Round #567 (Div. 2)B. Split a Number (字符串,贪心)
B. Split a Number time limit per test2 seconds memory limit per test512 megabytes inputstandard inpu ...
- AI人工智能对医疗行业有哪些巨大贡献?
人工智能(AI)有可能显着改变医生的角色并彻底改变医学实践.这篇定性评价文章总结了过去12个月的人工智能健康研究,涉及不同的医学专业,并讨论了与这一新兴技术相关的当前优势和挑战. 医生,特别是担任领导 ...
- Mongodb的几条命令
最近.... #设置用户名密码db.createUser({user: 'root', pwd: '123456', roles: ['root']}) #开启认证nohup mongod --aut ...
- LeetCode--094--二叉树的中序遍历(python)
递归 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # ...
- KCF跟踪算法
参考:https://www.cnblogs.com/YiXiaoZhou/p/5925019.html 参考:https://blog.csdn.net/shenxiaolu1984/article ...
- iOS Core Image-----十行代码实现微信朋友圈模糊效果
昨天下午微信的朋友圈着实火了一把,在这之后好多程序员都通过抓包工具看到了原图,但是我却在想,网上说是在移动前端做到的那是怎么做到的呢,经过一些学习,终于掌握了一些Core Image的知识,做出了相应 ...
- [CF342C]Cupboard and Balloons 题解
前言 博主太弱了 题解 这道题目是一个简单的贪心. 首先毋庸置疑,柜子的下半部分是要放满的. 于是我们很容易想到,分以下三种情况考虑: \[\small\text{请不要盗图,如需使用联系博主}\] ...
- 【bzoj1975】[Sdoi2010]魔法猪学院
*题目描述: iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界的世界本原有了很多的了解:众所周知,世界是由元素构成的:元 ...