Review of Codeforces 6B and 6C

本文介绍使用Python解决两个问题的方法:一是利用集合(set)属性找出总统办公室的颜色方案;二是通过贪心算法分配巧克力以确保Alice和Bob之间的公平竞争。文章提供了源代码及详细解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值