贝壳笔试9.7,第三题单纯读取输入就超时

数据挖掘机器学习方向笔试第三题,用Python读取输入直接超时,啥玩意,垃圾题目浪费时间,反馈还反馈半小时

最后附上自己的答案
import sys
def solution(price, money):
    price.sort(key=lambda x: [x[1], -x[0]])
    money.sort(key=lambda x: x[1])
    res = []
    n, m = len(price), len(money)
    i, j = 0, 0
    while i < n and j < m:
        if i == n&nbs***bsp;money[j][1] < price[i][1]:
            if i > 0:
                res.append([price[i - 1][0], price[i - 1][1], money[j][0]])
            else:
                res.append([-1, -1, money[j][0]])
            j += 1
        else:
            if i < n:
                i += 1
    res.sort(key=lambda x: x[2])
    return res
T = int(input())
for _ in range(T):
    n = int(input())
    temp = list(map(int, sys.stdin.readline().strip().split()))
    price, money = [], []
    for i in range(n):
        price.append([i + 1, temp[i]])
    q = int(input())
    for i in range(q):
        x, y, z = list(map(int, sys.stdin.readline().strip().split()))
        temp = x * y * z
        money.append([i + 1, temp])
    res = solution(price, money)
    for i in range(len(res)):
        if res[i][0] == -1:
            print(-1)
        else:
            print(res[i][0], res[i][1])
补一个单纯读取输入就超时的图片


#笔试题目##贝壳找房#
全部评论
我做的开发笔试题。用java的BufferedReader和BufferedWriter处理输入输出都错误,蒙逼了。。
点赞 回复 分享
发布于 2020-09-08 09:42
老哥你这有点名不符实啊~
点赞 回复 分享
发布于 2020-09-07 22:26
这个题卡了IO,C++的话把cin、cout换成scanf和printf就可以过了
点赞 回复 分享
发布于 2020-09-07 18:01
老哥 可以让我看看你卖糖果的代码吗?
点赞 回复 分享
发布于 2020-09-07 17:36
我当时懵逼了 调了半天 调了个寂寞
点赞 回复 分享
发布于 2020-09-07 17:35
先是暴力,然后二分,然后总共1000个可能的钱数打表,然后调了个寂寞
点赞 回复 分享
发布于 2020-09-07 17:32
。。。调了半天不知道错在哪
点赞 回复 分享
发布于 2020-09-07 17:29
import sys def get_buyable(P, money):     curmax = 0     curid = -1     for i in range(len(P)):         if P[i] <= money:             if P[i] > curmax:                 curmax = P[i]                 curid = i     if curid == -1:         return -1, -1     else:         return curid, curmax n = int(sys.stdin.readline().strip()) values = [] for i in range(n):     k = int(sys.stdin.readline().strip())     line = sys.stdin.readline().strip()     P = list(map(int, line.split()))     values.append(P)     q = int(sys.stdin.readline().strip())     Q = []     for j in range(q):         line = sys.stdin.readline().strip()         x, y, z = list(map(int, line.split()))         money = x * y * z         curid, curmax = get_buyable(P, money)         if curid == -1:             print(-1)         else:             print(curid + 1, curmax) 我怀疑过价格是不是从小到大,可以二分查找,但我试了2分查找,也是超时好吧
点赞 回复 分享
发布于 2020-09-07 17:26

相关推荐

1.自我介绍2.介绍一下mcp,&nbsp;skills3.了解react哪些状态管理库4.对话是sse还是什么?是用fetch还是EventSource?5.ts中的any&nbsp;和&nbsp;unknown讲一讲6.是直接用组件库的组件还是自己封装了一些别的7.代码输出题1function&nbsp;main()&nbsp;{{var&nbsp;a&nbsp;=&nbsp;1let&nbsp;b&nbsp;=&nbsp;2}console.log(a);console.log(b);}main()console.log(a);8.什么是块级作用域&nbsp;全局作用域&nbsp;函数作用域9.代码输出题2for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(i);},&nbsp;100);}10.代码输出题3for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&amp;lt;&nbsp;5;&nbsp;i++){function&nbsp;printText(temp)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(temp);},&nbsp;100);}printText(i)}11.代码输出题4for(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++){function&nbsp;printText(temp)&nbsp;{var&nbsp;temp&nbsp;=&nbsp;isetTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(temp);},&nbsp;100);}printText(i)}12.代码输出题5for(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++){function&nbsp;printText(temp)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{var&nbsp;temp&nbsp;=&nbsp;iconsole.log(temp);},&nbsp;100);}printText(i)}13.点击控制台输出题export&nbsp;default&nbsp;function&nbsp;App()&nbsp;{const&nbsp;[count,&nbsp;setCount]&nbsp;=&nbsp;useState(0)console.log('render',count)return&nbsp;(&lt;div&gt;&lt;h1&gt;{count}&lt;/h1&gt;{setCount(count&nbsp;+&nbsp;1)setTimeout(()&nbsp;=&amp;gt;&nbsp;console.log('setTimeout',&nbsp;count),&nbsp;1000)}}&amp;gt;+1&lt;/div&gt;)}//这个组件点击按钮后,控制台的输出顺序和值如下://&nbsp;1.&nbsp;render&nbsp;1&nbsp;(组件重新渲染,&nbsp;count&nbsp;更新为&nbsp;1)//&nbsp;2.&nbsp;setTimeout&nbsp;0&nbsp;(1秒后输出,注意这里是&nbsp;0&nbsp;而不是&nbsp;1)14.算法:给有序数组arr&nbsp;=&nbsp;[-4,&nbsp;-1,&nbsp;0,&nbsp;3,&nbsp;5],返回平方后的排序//&nbsp;有序数组平方后排序const&nbsp;arr&nbsp;=&nbsp;[-4,&nbsp;-1,&nbsp;0,&nbsp;3,&nbsp;5]function&nbsp;solution(arr)&nbsp;{const&nbsp;len&nbsp;=&nbsp;arr.lengthconst&nbsp;result&nbsp;=&nbsp;new&nbsp;Array(len)let&nbsp;left&nbsp;=&nbsp;0let&nbsp;right&nbsp;=&nbsp;len&nbsp;-&nbsp;1let&nbsp;index&nbsp;=&nbsp;len&nbsp;-&nbsp;1while&nbsp;(left&nbsp;&amp;lt;=&nbsp;right)&nbsp;{if&nbsp;(arr[left]&nbsp;*&nbsp;arr[left]&nbsp;&amp;gt;&nbsp;arr[right]&nbsp;*&nbsp;arr[right])&nbsp;{result[index]&nbsp;=&nbsp;arr[left]&nbsp;*&nbsp;arr[left]left++}&nbsp;else&nbsp;{result[index]&nbsp;=&nbsp;arr[right]&nbsp;*&nbsp;arr[right]right--}index--}return&nbsp;result}console.log(solution(arr));15.反问
查看14道真题和解析
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务