尝试用二进制搜索解决python问题

尝试用二进制搜索解决python问题,python,python-3.x,Python,Python 3.x,嘿,我正在尝试解决这个python练习问题,但我的解决方案不在给定的时间限制内,请有人解释一下。这将对你非常有帮助,因为我在哪里都找不到这个问题的python解决方案,他们用Java给出的解决方案与我的几乎相同 tasks\u count=int(输入()) 任务=[] def二进制搜索(x): arr=任务 如果arr==[]或x>arr[0]:返回0 elif x

嘿,我正在尝试解决这个python练习问题,但我的解决方案不在给定的时间限制内,请有人解释一下。这将对你非常有帮助,因为我在哪里都找不到这个问题的python解决方案,他们用Java给出的解决方案与我的几乎相同

tasks\u count=int(输入())
任务=[]
def二进制搜索(x):
arr=任务
如果arr==[]或x>arr[0]:返回0
elif x
解决方案2:

tasks_count = int(input())

tasks = []

for i in range(0, tasks_count):
    n = input().split(' ')
    if n[0] == '1':
        tasks.append(int(n[1]))
    elif len(tasks) < 3:
        print('Not enough enemies')
    else:
        tasks.sort(reverse=True)
        print(tasks[int(len(tasks)/3)-1])
tasks\u count=int(输入())
任务=[]
对于范围内的i(0,任务计数):
n=输入()。拆分(“”)
如果n[0]=“1”:
tasks.append(int(n[1]))
elif len(任务)<3:
打印('没有足够的敌人')
其他:
tasks.sort(reverse=True)
打印(任务[int(len(任务)/3)-1])
解决方案#2基本上达到了预期目标,但存在一些问题,不必要地降低了速度。首先,
input
函数比直接从
sys.stdin
读取要慢得多;根据我的简短实验,在
范围
上循环并调用
输入
要比在
sys.stdin的
上循环花费大约10倍的时间

它还执行一些繁琐的计算,将索引转换为
float
并返回到
int
,这是可以避免的:

int(len(tasks)/3)-1
可直接计算为:

len(tasks) // 3 - 1
使用
/
楼层分割运算符直接计算
int

一个小的改进是使用
str.partition
分割行,这样就不必处理手动索引(这可能会非常慢,更不用说不太容易阅读)

最后一个微优化是将您的工作捆绑在一个函数调用中;CPython变量在全局范围内存储和加载需要
dict
操作,而在局部范围内,它们是简单的C数组查找,运行速度更快

考虑到所有这些因素,最终的代码更像:

import sys

from itertools import islice

def main():

    tasks_count = int(next(sys.stdin))

    tasks = []

    for line in islice(sys.stdin, tasks_count):
        task_type, sep, task_value = line.partition(' ')
        if task_type == '1':
            tasks.append(int(task_value))
        elif len(tasks) < 3:
            print('Not enough enemies')
        else:
            tasks.sort(reverse=True)
            print(tasks[len(tasks) // 3 - 1])

if __name__ == '__main__':  # Standard import guard
    main()  # Call main

两件事。1.如果你有一个循环或一些没有终止的东西,即使回答错误,你也会被超过时间限制。2.为什么要进行二进制搜索?首先应该使用流排序,如堆排序或插入排序!输入流未排序!在这个问题上,它要求进行二进制搜索,第二个解决方案对所有答案都是正确的exceeded@Srini你能取消否决票吗?我已经试了两个小时,然后在这里发布了我做错了什么提示:使用以下输入行本地运行解决方案1代码:
4
11
13
12
2
@rahushrivastava不是我
import sys

from itertools import islice

def main():

    tasks_count = int(next(sys.stdin))

    tasks = []

    for line in islice(sys.stdin, tasks_count):
        task_type, sep, task_value = line.partition(' ')
        if task_type == '1':
            tasks.append(int(task_value))
        elif len(tasks) < 3:
            print('Not enough enemies')
        else:
            tasks.sort(reverse=True)
            print(tasks[len(tasks) // 3 - 1])

if __name__ == '__main__':  # Standard import guard
    main()  # Call main
import sys
from bisect import insort
from itertools import islice

def main():

    tasks_count = int(next(sys.stdin))

    tasks = []

    for line in islice(sys.stdin, tasks_count):
        task_type, sep, task_value = line.partition(' ')
        if task_type == '1':
            insort(tasks, int(task_value))  # Task 1 more expensive, O(n) instead of O(1)
        elif len(tasks) < 3:
            print('Not enough enemies')
        else:
            print(tasks[-(len(tasks) // 3)])  # Task 2 cheaper; O(1) instead of O(n log n)