# 5,5 # 2,10 # 3,20 # 4,30 # 5,40 # 5,90 # 输出: 50 # # 某购物城有m个商铺,现决定举办一场活动选出人气最高的店铺。活动共有n位市民参与,每位市民只能投一票, # 但1号店铺如果给该市民发放q元的购物补贴,该市民会改为投1号店铺 # 请计算1号店铺需要最少发放多少元购物补贴才能成为人气最高的店铺(即获得的票数要大于其他店铺) # 如果1号店铺本身就是票数最高的店铺,返回0 # # 输入描述: # 第一行为小写逗号分割的两个整数n,m,其中第一个整数n表示参与的市民总数 # 第二个整数m代表店铺的总数,1<=n,m<=3000 # 第2到n+1行,每行为小写逗号分割的两个整数p,q,表示市民的意向投票情况, # 其中每行的第一个整数p表示该市民意向投票给p号店铺,第二个整数q表示其改投1号店铺所需给予的购物补贴,1<=p<=m,1<=q<=10^9 #输出: 一号店最少发放的补贴金额 from collections import defaultdict popularityMap = defaultdict(int) minMoney = float("inf") money = 0 def processVotes(peopleList, changeList, index): global minMoney, money print("changeList==",changeList) print("minMoney:",minMoney) if isMax(changeList) and minMoney > money: minMoney = money else: for i in range(index, len(peopleList)): changeList.append(peopleList[i]) processVotes(peopleList, changeList, i + 1) changeList.pop() def isMax(changeList): global popularityMap, money tempMap = popularityMap.copy() # defaultdict(<class 'int'>, {1: 0, 2: 1, 3: 1, 4: 1, 5: 2}) money = 0 for shop, popularity in changeList: money += popularity tempMap[shop] -= 1 tempMap[1] += 1 entryList = sorted(tempMap.items(), key=lambda x: x[1], reverse=True) firstShop = entryList[0][0] if firstShop == 1 and (len(entryList) == 1 or entryList[0][1] > entryList[1][1]): return True return False if __name__ == "__main__": input1 = input().split(",") shopCount = int(input1[0]) voteCount = int(input1[1]) peopleList = [] popularityMap[1] = 0 for i in range(shopCount): input2 = input().split(",") shop = int(input2[0]) popularity = int(input2[1]) if shop != 1: peopleList.append([shop, popularity]) popularityMap[shop] += 1 print("popularityMap=", popularityMap) print("peopleList=", peopleList) processVotes(peopleList, [], 0) print(minMoney)