Python 代码部队-548龙与公主
我正在研究一个名为“龙与公主”的问题: 从前,有一位骑士。由于非常勇敢,他决定进行一次充满战斗和冒险的长途旅行。此旅程的地图可以表示为一行,其中有n个单元格,编号从1到n,从左到右。最初,骑士位于最左边的单元格(单元格编号1)。他应该一个接一个地通过所有的牢房,然后在最右边的牢房(n号牢房)结束他的旅程。他不允许向后移动或跳过某些单元格,他将从第一个到最后一个访问所有单元格 除第一个单元外,每个单元都包含一条龙或一位公主。每条龙都有一个装有金币的箱子。牢房里的龙我保存着gi硬币。每次骑士带着一条龙走进牢房,他都有一个选择——杀死这条龙,或者只是穿过牢房。骑士非常强壮和灵巧,所以在路上杀死任何龙对他来说都不是问题。如果一条龙被杀死,骑士将拥有所有的金龙 当骑士带着公主走进牢房时,她想知道他杀死了多少条龙。如果这个数字大于或等于她的美丽指数,公主认为骑士足够勇敢,并立即要求他嫁给她。作为一个真正的绅士,骑士不能拒绝,他的冒险立刻结束 骑士爱住在n号牢房的公主,并想娶她。在旅途中,他还想收集尽可能多的黄金。请帮助他完成这项任务 输入- 输入的第一行包含一个整数n(2≤ N≤ 2·105)-细胞数。接下来的n-1行描述了从2到n的细胞 如果单元格编号i包含一条龙,则输入的第i行包含字母“d”,后跟一个整数gi(1≤ 胃肠道≤ 104)-龙保存的硬币数量。字母和整数用一个空格分隔 如果单元号i包含公主,则输入的第i行包含字母“p”,后跟一个整数bi(1)≤ 毕≤ 2·105)-公主的美丽。字母和整数用一个空格分隔。保证最后一个单元格包含公主 输出- 在输出的第一行打印一个整数-骑士可以收集的金币的最大数量。在第二行打印一个整数k——要杀死的龙的数量。第三行应该包含k个整数——骑士应该杀死一条龙的单元格数。单元格编号应按递增顺序打印 如果存在多个最优解,则输出其中任何一个。如果骑士不能娶他心爱的公主,只需在输出的第一行打印-1 示例: 样本输入: 六, d 10 d 12 P2 d 1 P2 样本输出: 十三, 二, 35 样本输入: 六, d 10 d 12 P2 d 1 第3页 样本输出: -一, 我提交了我的解决方案,但由于某种原因,它失败了,我无法访问失败的测试用例 我希望一些新的眼睛能发现问题 我的解决方案:Python 代码部队-548龙与公主,python,python-3.x,data-structures,Python,Python 3.x,Data Structures,我正在研究一个名为“龙与公主”的问题: 从前,有一位骑士。由于非常勇敢,他决定进行一次充满战斗和冒险的长途旅行。此旅程的地图可以表示为一行,其中有n个单元格,编号从1到n,从左到右。最初,骑士位于最左边的单元格(单元格编号1)。他应该一个接一个地通过所有的牢房,然后在最右边的牢房(n号牢房)结束他的旅程。他不允许向后移动或跳过某些单元格,他将从第一个到最后一个访问所有单元格 除第一个单元外,每个单元都包含一条龙或一位公主。每条龙都有一个装有金币的箱子。牢房里的龙我保存着gi硬币。每次骑士带着一条
class MinHeap:
def __init__(self):
self.lst = []
def insert(self, data):
self.lst.append(data)
self.heapify_up(len(self.lst) - 1)
def pop_root(self):
root = self.lst[0]
last = self.lst.pop()
if len(self.lst) > 0:
self.lst[0] = last
self.heapify_down(0, 0)
return root
def heapify_down(self, parent_idx, child_idx):
if child_idx >= len(self.lst):
return
parent_greater_bool = self.lst[parent_idx] > self.lst[child_idx]
if parent_greater_bool:
self.lst[parent_idx], self.lst[child_idx] = self.lst[child_idx], self.lst[parent_idx]
if parent_greater_bool or parent_idx == 0:
self.heapify_down(child_idx, child_idx * 2 + 1)
self.heapify_down(child_idx, child_idx * 2 + 2)
def heapify_up(self, child_idx):
parent_idx = (child_idx - 1) // 2
if parent_idx < 0:
return
if self.lst[parent_idx] > self.lst[child_idx]:
self.lst[parent_idx], self.lst[child_idx] = self.lst[child_idx], self.lst[parent_idx]
self.heapify_up(parent_idx)
def heap_sum(self):
return sum([d.gold for d in self.lst])
def indexes(self):
return ' '.join(sorted([str(d.index) for d in self.lst]))
def __len__(self):
return len(self.lst)
def __str__(self):
res = ''
for d in self.lst:
res += f"index: {d.index}, gold: {d.gold} | "
return res
class NotValidCharacter(Exception):
pass
class Dragon:
def __init__(self, index, gold):
self.index = index
self.gold = gold
def __str__(self):
return f"index: {self.index}, gold: {self.gold}"
def __lt__(self, other):
return self.gold < other.gold
def dragons_and_princesses():
heap = MinHeap()
try:
length = int(input("insert the length of the map\n"))
except ValueError:
raise ValueError("The map length must be integer")
if length < 2 or length > 200000:
while length < 2 or length > 200000:
try:
length = int(input("insert the length of the map (2 - 200,000)\n"))
except ValueError:
raise ValueError("The map length must be integer")
for i in range(1, length):
try:
character, value = input().split()
if character not in ['d', 'p']:
raise NotValidCharacter('You can only enter dragons and princesses')
value = int(value)
except ValueError:
raise ValueError("Make sure you enter 'd' or 'p' and following integer")
if character == 'd':
heap.insert(Dragon(i + 1, value))
if character == 'p':
if i == length - 1:
if value > len(heap):
print(-1)
else:
print(heap.heap_sum())
print(len(heap))
print(heap.indexes())
elif value <= len(heap) and i != length - 1:
while value <= len(heap):
heap.pop_root()
if __name__ == "__main__":
dragons_and_princesses()
class MinHeap:
定义初始化(自):
self.lst=[]
def插入(自身,数据):
self.lst.append(数据)
自我保健(len(self.lst)-1)
def pop_根目录(自):
root=self.lst[0]
last=self.lst.pop()
如果len(self.lst)>0:
self.lst[0]=最后一个
self.heapify_down(0,0)
返回根
def heapify_down(自身、父项、子项):
如果child_idx>=len(self.lst):
返回
parent\u greater\u bool=self.lst[parent\u idx]>self.lst[child\u idx]
如果父对象大于父对象:
self.lst[parent_idx],self.lst[child_idx]=self.lst[child_idx],self.lst[parent_idx]
如果父项大于或父项idx==0:
self.heapify_down(child_idx,child_idx*2+1)
self.heapify_down(child_idx,child_idx*2+2)
def heapify_up(自我、儿童idx):
父\u idx=(子\u idx-1)//2
如果父项_idx<0:
返回
如果self.lst[parent_idx]>self.lst[child_idx]:
self.lst[parent_idx],self.lst[child_idx]=self.lst[child_idx],self.lst[parent_idx]
self.heapify\u up(父项\u idx)
def堆_和(自身):
返回金额([d.gold代表self.lst中的d])
def索引(自):
返回“”。连接(已排序([str(d.index)表示self.lst中的d]))
定义(自我):
返回len(自身lst)
定义(自我):
res=''
对于self.lst中的d:
res+=f“索引:{d.index},黄金:{d.gold}|”
返回res
类NotValidCharacter(异常):
通过
龙类:
定义初始值(自、索引、黄金):
self.index=索引
self.gold=黄金
定义(自我):
返回f“index:{self.index},gold:{self.gold}”
定义(自身、其他):
返回self.gold200000时:
尝试:
length=int(输入(“插入映射的长度(2-200000)\n”))
除值错误外:
raise VALUERROR(“映射长度必须为整数”)
对于范围内的i(1,长度):
尝试:
字符,值=输入().split()
如果字符不在['d','p']中:
升起NotValidCharacter('您只能进入龙和公主')
瓦尔