您的位置:首页 > 编程语言 > Python开发

Review of Codeforces 6B and 6C

2014-11-16 10:57 344 查看
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 = 0
j = n-1
m = a[i]
k = a[j]

if n == 1:
print 1, 0
elif n == 2:
print 1, 1
else:
while j-i != 1:
if (m <= k):
i += 1
m += a[i]
elif (m > k):
j -= 1
k += a[j]
print i+1, n - j



also, you need to be care the output is i+1 and n-j
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息