1、計分板
時間限制:C/C++語言1000MS;其他語言3000MS
內存限制:C/C++語言131072KB;其他語言655360KB
題目描述:假設你正在參加一場比賽,這場比賽有A,B,C...M—共13題。現在,你已經通過了n題,此時你注意到,大屏幕上有一個計分板,裏面記錄了每一道題目前有多少人通過。你想知道,在剩下的還沒有做出來題目當中,哪一道題纔是通過人數最多的題目?如果有多個這樣的題目,取題號最靠前的。
輸入:
第一行一個數n,表示你現在通過的題目。接下來n個字母,描述你通過的題號。接下來13個數,表示每一道題有多少人通過,參加的人數不大於150人。
輸出:
輸出一個字母,代表在剩下的還沒有做出來題目當中通過人數最多的題目。
樣例輸入:
6
ADEGHM
58 42 98 84 84 40 75 97 98 78 40 54
樣例輸出:
C
import sys
n = int(sys.stdin.readline().strip()) # 已經通過n道題
t = list(sys.stdin.readline().strip()) # 通過的題號
l = [ord(i)-ord('A') for i in t] # 記錄通過題目在所有題目中對應的下標 A-0 B-1
p = map(int, sys.stdin.readline().strip().split()) # 每道題通過的人數
ans = 0
for i in range(13):
if i not in l and p[i] > p[ans]:
ans = i
print chr(ord('A')+ans)
需要掌握的知識點:
ord()函數主要用來返回字符的ascii碼。
chr()主要用來表示ascii碼對應的字符。輸入爲數字,可以用十進制和十六進制。
print chr(97) #a
print chr(0x61) #a
2、對講機
時間限制:C/C++語言1000MS ;其他語言 3000MS
內存限制:C/C++語言 131072KB ; 其他語言 655360KB
題目描述:某學術會議上,一共有n個人參加,現在已知每個人會的語言(一個人可能不會任何語言)。現在有一種學習機,每一個學習機可以在會議期間使一個人暫時掌握一種自己不會的語言,問要使得任意兩人都要能直接或者間接的交流至少準備多少個學習機?間接交流的意思是:可以通過其他參加會議的人翻譯(可能出現很多人一起幫忙翻譯的情況)進行交流。如:第一個人和第二個人會第一種語言,第二個人和第三個人會第二種語言,那麼第一個人可以和第三個人進行交流(通過第二個人的翻譯)
輸入:
第一行3個數n,m,k代表人數,語言數,己知的信息數。接下來k行,每行兩個數u,v,代表第u個人會第v種語言。
輸出:
輸出需耍準備的學習機的個數
樣例輸入:
3 3 2
2 3
3 1
樣例輸出:
2
思路一:
構圖,輸出聯通分量的個數-1
思路二:
用兩個list分別存每個語言會的人數,以及每個人會的語言
用兩個set分別保存可以互相交流的人以及語言。
遍歷所有的語言,如果會該語言的人與可以互相交流的人有交集,那麼這些人都可以通過這個人互相交流,並將該語言加入可相互交流的列表
如果沒有交集,則需要一個對講機 res -1,再加入人和語言
import ,sys
n, m, k = map(int, sys.stdin.readline().strip().split())
skill_p = [[] for i in range(m 1)] #每個技能會的人
people_s = [[] for i in range(n 1)] #每個人會的技能
for i in range(k):
a, b = map(int, sys.stdin.readline().strip().split())
skill_p[b].append(a)
people_s[a].append(b)
res = 0 #答案
for i in range(1, n 1): #不會講任何一種語言的人需要對講機
if not people_s[i]:
res = 1
#用兩個set分別保存可以互相交流的人以及語言 people_set = set()
skill_set = set()
for i in range(1, m 1):
temp_set = set(skill_p[i]) & people_set
if skill_p[i] and not temp_set:
res = 1
people_set = people_set | set(skill_p[i])
skill_set.add(i)
print res - 1
思路三:並查集
import sys
from itertools import combinations
def find_root(node):
while(node!=parent[node]):
node = parent[node]
return node
def union_two(node1, node2, res):
root1 = find_root(node1)
root2 = find_root(node2)
if root1 == root2:
return res
else:
parent[root2] = root1
res -= 1
return res
n, m, k = map(int, sys.stdin.readline().strip().split())
skills = [[] for i in range(m+1)]
parent = [i for i in range(n+1)]
res = n
if k == 0:
print res
else:
for i in range(k):
u, v = map(int, sys.stdin.readline().strip().split())
skills[v].append(u)
for i in range(m+1):
if len(skills[i]) > 1:
for j in combinations(skills[i], 2):
res = union_two(j[0], j[1], res)
#print res
print res-1
需要掌握的知識:
python list的排列組合問題:
排列:from itertools import permutations
p = permutations(list, n) #返回list中n個元素的全排列,類型爲迭代器,可通過list(c)轉化爲列表 [(), (), ...],時間複雜度 n!
組合:from itertools import combinations
c = combinations(list, n) # 從list中選擇所有長度爲n的組合,返回迭代器,時間複雜度2^n
笛卡爾積:from itertools import product
product(A,B)函數,返回A和B中的元素組成的笛卡爾積的元組
product(A,repeat=3):repeat指定iterable重複幾次,product(A,repeat=3)等價於product(A,A,A)
元素可重複出現時的排列:product(l, repeat=n) #元素可重複的排列,時間複雜度是n^n
筆試時間:8.29
崗位:視覺算法工程師
語言:python 2.7