2017.4.8微软笔试题

来源:互联网 发布:英制螺丝孔算法 编辑:程序博客网 时间:2024/06/02 05:37

微软笔试题

第一题

时间限制: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

为每个方向建一个dict,每加入一个皇后,则总对数加上四个方向已有的皇后数:

def weiruan1():    n = int(raw_input())    sum1=0    dic1={}    dic2={}    dic3={}    dic4={}    for _ in xrange(n):        a=list(map(int,raw_input().split()))    '''判断水平方向上有多少皇后,sum1加上皇后数,然后这个方向上的dict加1'''        if a[0] in dic1:            sum1 += dic1[a[0]]            dic1[a[0]]+=1        else:            dic1[a[0]]=1    '''同上,判断垂直方向上有多少皇后'''        if a[1] in dic2:            sum1 += dic2[a[1]]            dic2[a[1]] += 1        else:            dic2[a[1]] = 1    '''同上,判断135度方向上有多少皇后'''        f=a[0]+a[1]        if f in dic3:            sum1 += dic3[f]            dic3[f] += 1        else:            dic3[f] = 1    '''同上,判断45度方向上有多少皇后'''        g=a[1]-a[0]        if g in dic4:            sum1 += dic4[g]            dic4[g] += 1        else:            dic4[g] = 1    '''输出总对数'''    print sum1

第四题

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
In a video game, Little Hi is going to assassinate the leader of an evil group, EL SUENO.

There are N high-value targets in the group, numbered from 1 to N. Each target has another target as his direct superior except for EL SUENO who has no superior. So the superior-subordinate hierarchy forms a tree-like structure. EL SUENO is at the root.

The cost of assassinating the i-th target is Ci. Before assassinating a target Little Hi has to obtain enough information about him. For the i-th target, Little Hi needs INi units of information. By assassinating a target Little Hi will obtain some information about the target’s direct superior. More specifically, the i-th target has IPi units of information about his superior. So in order to assassinate EL SUENO, Little Hi needs to assassinate some of his direct subordinates so that the sum of information obtained is enough; assassinating the subordinates needs to assassinate their direct subordinates … until it reaches some targets require zero information in advance. (Luckily if a target has no subordinate he always requires zero information.)

How Little Hi wants to know what is the minimum cost for successful assassinating EL SUENO.

输入
The first line contains an integer N.

Then follow N lines. Each line describes a target with 4 integers, Fi, INi, IPi, Ci.

Fi is i-th target’s superior. For EL SUENO his Fi is zero.

INi is the amount of information needed to assassinate the i-th target. For a target has no subordinates his INi is always zero.

IPi is the amount of information the i-th target has about his superior Fi.

Ci is the cost of assassinating the i-th target.

For 30% of the data, 1 <= N <= 10, 0 <= INi, IPi <= 100

For 60% of the data, 1 <= N <= 100, 0 <= INi, IPi <= 1000

For 100% of the data, 1 <= N <= 2000, 0 <= Fi <= N, 0 <= INi, IPi <= 20000, 1 <= Ci <= 1000.

It is guaranteed that the N targets form a tree structure.

输出
The minimum cost. If Little Hi is not able to assassinate EL SUENO output a number -1.

样例输入
6
2 1 2 2
0 4 0 2
2 0 2 5
2 0 2 6
1 0 1 2
1 0 1 3
样例输出
11

1500 EL SUENO AC Python2 185ms 6MB
建树,递归计算杀死目标i所需的成本,递归后序遍历树计算每个节点所需的成本,最终得到根节点所需成本:

def weiruan2():#树类,a代表对上级的信息量,b代表成本,c是下级集合,d是所需信息量    class El(object):        def __init__(self,a,b,d):            self.a = a            self.b = b            self.c = []            self.d = d    n = int(raw_input()) #建树,el指向根节点    mubiao=[El(0,0,0) for _ in xrange(n)]    el=mubiao[0]    for i in xrange(n):        a = list(map(int, raw_input().split()))        mubiao[i].a=a[2]        mubiao[i].b=a[3]        mubiao[i].d=a[1]        if a[0]==0:            el=mubiao[i]        else:            mubiao[a[0] - 1].c.append(mubiao[i])   #makeab计算某个节点所需成本    def makeab(ell):    #后序遍历,先把子树的所需成本计算出来,再计算父节点的成本        for i in ell.c:            makeab(i)    #递归计算某个节点所需最小成本        min0=[10000000]        def sel(a, k, g, d):            for i in xrange(len(a)):                s = a[i + 1:] + []                if a[i].a + k >= d:                    if g + a[i].b < min0[0]:                        min0[0] = g + a[i].b                else:                    k1 = k + a[i].a                    g1 = g + a[i].b                    sel(s, k1, g1, d)        if ell.c!=[]:            sel(ell.c,0,0,ell.d)            ell.b += min0[0]    makeab(el)    if el.b>=10000000:        print -1    else:        print el.b
0 0