Review of Codeforces 6B and 6C

来源:互联网 发布:软件项目计划书 编辑:程序博客网 时间:2024/04/30 21:23


6B. President's Office

This question is about using 'set' in pyhon. set has a property that it only add elements that not included in the current set. which is an ideal tool in this question.
First add '.' and 'C(the president's color)' in the set and then search the matrix and when it find 'C', it search around the element and add the alphabeta that not included in the set.

The source code is:

n, m, c = raw_input().split()n, m = map(int, [n, m])a = [raw_input() for i in xrange(n)]s = set(['.', c])for i in xrange(n):for j in xrange(m):if a[i][j] == c:for x, y in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:if x >= 0 and x < n and y >= 0 and y < m: s.add(a[x][y])print len(s) - 2

I also have an other idea, but is trival and has wrong answer. I dont't know why.

6C. Alice, Bob and Chocolate

This question is about greedy algorithm. The only thing you need to remind is there are corner cases in this task when n== 1 and n == 2. every time compare the time consumed by alice and bob, and add one chocolate to the fewer one. The source code as follows:

n = input()a = list(map(int, raw_input().split()))i = 0j = n-1m = a[i]k = a[j]if n == 1:print 1, 0elif n == 2:print 1, 1else:while j-i != 1:if (m <= k):i += 1m += a[i]elif (m > k):j -= 1k += a[j]print i+1, n - j

also, you need to be care the output is i+1 and n-j

0 0
原创粉丝点击