Codeforces 442A
A. Borya and Hanabitime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.
Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has).
The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints.
A color hint goes like that: a player names some color and points at all the cards of this color.
Similarly goes the value hint. A player names some value and points at all the cards that contain the value.
Determine what minimum number of hints the other players should make for Borya to be certain about each card's color and value.
InputThe first line contains integer n (1 ≤ n ≤ 100) — the number of Borya's cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in.
OutputPrint a single integer — the minimum number of hints that the other players should make.
Sample test(s)input2
G3 G3output0input4
G4 R4 R3 B3output2input5
B1 Y1 W1 G1 R1output4NoteIn the first sample Borya already knows for each card that it is a green three.
In the second sample we can show all fours and all red cards.
In the third sample you need to make hints about any four colors.
Accepted Code:
from collections import defaultdict def main():
input()
cards = list(set(raw_input().split())); if len(cards) == 1:
print 0
return
answer = 9;
chars = 'RGBYW12345'
for mask in xrange(1, 1024):
cover = ''
for i in xrange(10):
if (mask & (1<<i)):
cover += chars[i] d = defaultdict(int) for card in cards:
if card[0] in cover and card[1] in cover:
continue;
if card[0] in cover:
d[card[0]] += 1;
elif card[1] in cover:
d[card[1]] += 1;
else:
d['*'] += 1; if all(x <= 1 for x in d.values()):
answer = min(answer, len(cover)); print answer; if __name__ == '__main__':
main()
第一次使用collections.defaultdict,如果定义一个dict(字典),当访问一个不存在的键值的时候会发生异常,
但是defaultdict这种情况下就会提供一个默认值,他使用一个函数作为初始化的参数,可以是内建的类型,如int, list等,
还可以是用户自定义的不含参数的函数,以该函数的返回值作为其默认值。
关于all这个函数,他以一个可迭代的类型的对象作为参数,当所有的值都为True则返回True
Codeforces 442A的更多相关文章
- Codeforces 442A Borya and Hanabi
有五种花色 外加 五种点数 共25张牌,每次有n张牌,主人知道这n张牌中有哪些牌,并且哪种牌有几张,但是不知道具体是哪张牌,他可以问某种花色,然后知道了哪几张是该花色,也可以问点数,然后就知道了哪几张 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 基于宜搭的《T恤尺码收集》应用搭建
简介: 在阿里,T恤是程序员必不可少的元素.每逢公司或者BU(部门)的重大节庆日,比如双11 .年会.新BU成立仪式.大型活动等,都会给员工定制发放统一的T恤或者POLO衫服装.而我们每次发放T恤之前 ...
- Laravel报错:1071 Specified key was too long; max key length is 1000 bytes
Laravel安装,初始化数据库,运行下列命令时候出错 php artisan migrate 解决办法1,设置数据库引擎格式 //临时更改 SET GLOBAL default_storage_en ...
- OSGi.NET使用笔记
一手资料来源于“开放工厂”,以下程序将会引用到一个核心文件UIShell.OSGi.dll 目前我对于OSGi这个框架的理解就是,主程序搜索并加载插件,以插件方式开放,便于扩展. 现在开始正式的旅程. ...
- 写一个网页进度loading
作者:jack_lo www.jianshu.com/p/4c93f5bd9861 如有好文章投稿,请点击 → 这里了解详情 loading随处可见,比如一个app经常会有下拉刷新,上拉加载的功能,在 ...
- VS2005连接MySQL C API
1.在属性添加:附加目录,附加依赖库,附加库目录: 2.在stdafx.h中加入: #include <winsock.h> #include "mysql.h" 3. ...
- 2018-11-19-windows-应用程序在关机时的退出代号
title author date CreateTime categories windows 应用程序在关机时的退出代号 lindexi 2018-11-19 14:31:38 +0800 2018 ...
- 各种语言web性能简单对比测试
忽然想比较一下 python nodejs go 的web 响应,就简单的写了个性能对比测试. 测试目标:1 . i5 4核 32G 同一机器 linux 2. 用python(flas ...
- hibernate hql语句 group by having 的坑
我期望获得这个列表 然而,使用hql只能获得第一条数据,后来我琢磨了一下,和group by有关系 应该改成 成功查询到
- Java的split()方法
在java中,split方法很常用,但是有些时候我们发现并不管用,不管用的情况:split("."),split("^"), 管用的情况:split(" ...
- 【Django入坑之路】Models操作
1:字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 pr ...