依图笔试, 求大佬指点

第一题, 交换数组递增 10%
'''
@Author: rayenwang
@LastEditTime: 2019-09-06 20:40:27
@Description: 
'''
N = int(input())
nums = [int(n) for n in input().split()]

# N = 5
# nums = [1, 5, 2, 4, 3]
result = 0
for i in range(N):
    if nums[i] == i + 1:
        continue
    index = nums[i] - 1
    nums[i], nums[index] = nums[index], nums[i]
    result += 1
print(result)
第二题, 粉碎我自己 AC
'''
@Author: rayenwang
@LastEditTime: 2019-09-06 20:01:39
@Description: 
'''


def fill(height, space, index, N):
    pre_sum = 0
    for i in range(index, N):
        if pre_sum + height[i] <= space:
            pre_sum += height[i]
        else:
            return False, i, space - pre_sum
    return True, i, space - pre_sum


import math
N, H, K = [int(n) for n in input().split()]
height = []
for _ in range(N):
    height.append(int(input()))
# N, H, K = [5, 5, 3]
# height = [5, 4, 3, 2, 1]

time = 0
finish, index, space = fill(height, H, 0, N)
if finish:
    print(math.ceil((H - space) / K))
else:
    while index < N:
        wait = math.ceil((height[index] - space) / K)
        time += wait
        space = min(H, space + wait * K)
        finish, index, space = fill(height, space, index, N)
        if finish:
            break
    print(time + math.ceil((H - space) / K))
第三题, 最大的K个节点之和 0%
'''
@Author: rayenwang
@LastEditTime: 2019-09-06 20:33:16
@Description: 
'''

N, K, S = [int(n) for n in input().split()]
path = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    x, y = [int(n) for n in input().split()]
    path[x].append(y)
value = [0]
for _ in range(N):
    value.append(int(input()))
result = [0 for _ in range(N + 1)]


def recur(root):
    if not root:
        return []
    temp = [root]
    for child in path[root]:
        temp = sorted(temp + recur(child), reverse=True)[:K]
    result[root] = sum(temp)
    return temp


recur(S)
for i in range(1, N + 1):
    print(result[i])
    



#笔试题目##依图科技#
全部评论
这是第三套卷子,楼主第一题题目应该没说数组是连续的吧,直接index = num[i]-1是不对吧
点赞 回复 分享
发布于 2019-09-06 21:33
第一题这个思路对吗?AC了?
点赞 回复 分享
发布于 2019-09-07 14:38
# coding=utf-8 import sys if __name__ == "__main__":     nhk = sys.stdin.readline().strip().split()     n = int(nhk[0])     h = int(nhk[1])  # kerongna     k = int(nhk[2])  # meimiao     a = []     for _ in range(n):         a.append(int(sys.stdin.readline().strip()))     temp = 0     i = 0     res = 0     while i < n or temp > 0:         if i < n and temp + a[i] <= h:             temp += a[i]             i += 1         temp = max(temp-k, 0)         res += 1     print(res) 求问第二题,哪里没考虑全😭,按照粉碎的过程
点赞 回复 分享
发布于 2019-09-07 00:42
第一题没有说是1~N吧,数组是随意给的
点赞 回复 分享
发布于 2019-09-07 00:34
"""依图笔试第三套第一题,数组交换""" import sys n = int(input()) nums = list(map(int,input().split())) dicts = {} sort_nums = sorted(nums) for i in range(len(sort_nums)):     dicts[sort_nums[i]] = i cnt = 0 flag = [False]*len(nums) for i in range(len(nums)):     if not flag[i]:         j = i         while not flag[j]:             flag[j] = True             j = dicts[nums[j]]         cnt += 1 print(len(nums) - cnt)
点赞 回复 分享
发布于 2019-09-06 23:21
交换数组  #!/usr/bin/env python # encoding: utf-8 import sys n = int(sys.stdin.readline().strip()) nums = map(int, sys.stdin.readline().strip().split(' ')) nums = [0] + nums Flag = True count = 0 while Flag:     Flag = False     for i in range(1, n+1):         if nums[i] != i:             # print nums[i]             tmp = nums[nums[i]]             nums[nums[i]] = nums[i]             nums[i] = tmp             count += 1             Flag = True print count
点赞 回复 分享
发布于 2019-09-06 21:54
第一道ac 第二道80% 后面两道怎么搞都是0 很绝望
点赞 回复 分享
发布于 2019-09-06 21:53
第二题粉碎50% 超时。。。。感觉复杂度不高啊 #!/usr/bin/env python # encoding: utf-8 import sys import math n, h, k = map(int, sys.stdin.readline().strip().split(' ')) a = [] for i in range(n):     tmp = int(sys.stdin.readline().strip())     a.append(tmp) total_time = 0 tmp = a.pop(0) rem = h - tmp is_empty = False Flag = True while a:     Flag = False     # print a     tmp = a.pop(0)     while rem >= tmp:         rem = rem - tmp         if a:             tmp = a.pop(0)         else:             is_empty = True             break     if is_empty:         total_time += int(math.ceil((h-rem)*1./k))     else:         tmp_time = int(math.ceil((tmp - rem)*1. / k))         # print rem, tmp         # print tmp_time         # break         rem = min(tmp_time * k + rem, h)         total_time += tmp_time         a = [tmp] + a if Flag:     total_time = int(math.ceil(tmp*1./k)) print total_time
点赞 回复 分享
发布于 2019-09-06 21:52
#!/usr/bin/env python # encoding: utf-8 import sys def sub_k(s):     max_k = 0     for i in range(10, -1, -1):         Flag = True         for j in range(2**i-1, -1, -1):             tmp = bin(j)[2:]             for k in range(i-len(tmp)):                 tmp = '0'+tmp             if tmp not in s:                 Flag = False                 break         if Flag:             max_k = i             break     return max_k n, m = map(int, sys.stdin.readline().strip().split(' ')) string = ['-1'] for i in range(n):     tmp_str = sys.stdin.readline().strip()     string.append(tmp_str) op = [[i] for i in range(n+1)] for i in range(m):     left, right = map(int, sys.stdin.readline().strip().split(' '))     op.append(op[left]+op[right]) concat = [] for l in op[n+1:]:     # tmp_str = string[left] + string[right]     tmp_str = ''.join([string[i] for i in l])     string.append(tmp_str)     # print tmp_str     print sub_k(tmp_str) 第四题 80%
点赞 回复 分享
发布于 2019-09-06 21:50
第四题呢。。
点赞 回复 分享
发布于 2019-09-06 21:45
第三题并不是前面的就是父节点 你需要自己判断; 第一题: 借鉴LeetCode的41题的思想https://leetcode.com/problems/first-missing-positive/ def change(arr):     res=0     for i in range(len(arr)):         while (arr[i]!=arr[arr[i]]):             res+=1             arr[arr[i]],arr[i]=arr[i],arr[arr[i]]     return res def p1():     n=int(input())     arr=[int(i) for i in input().split()]     ind=sorted(range(n),key=lambda x:arr[x]) # 返回index     print(change(ind))
点赞 回复 分享
发布于 2019-09-06 21:32
这是哪套题?
点赞 回复 分享
发布于 2019-09-06 21:25
第二题需要考虑什么边界么,只做了80%
点赞 回复 分享
发布于 2019-09-06 21:18
同求大佬
点赞 回复 分享
发布于 2019-09-06 21:15

相关推荐

2025-12-02 02:15
门头沟学院
最近菊厂陆续开了,极力劝退那些拿13级的985硕士,就13级那么点儿薪资,一线城市每个月到手1.8/7/6w,租房2k还是破烂,吃饭2k还是预制菜,买个1k衣服都是聚酯纤维破塑料,稍微出去浪一浪,能留1w就是万岁,要是再有个啥都想买的对象,一线工作一年难存10w。隔壁工地混泥土,钳工,焊工一天800+,还包吃包住。读书18年到985硕士出来就为了进厂螺丝工?还不如从8岁童工开始干活,别人读书完了你工龄18+,混不上领导也是个小头头了。当然专科进来正式工,od都行,一般本科进来13级也OK,毕竟22岁年纪摆在那个地方还不需要太花钱。读硕博的基本26岁,工作两年就要结婚的,兜里没几个崽,连彩礼都要信用贷。菊厂离职的不少,毕竟正常没人受得了9116(梗:再来一次911刷6)。为啥这时候劝?因为刚下班,因为国考刚完,省考下周,就是可惜选调只有当年应届能报。现在回想能拍断大腿。应届生真实好身份,错过这一次,选调,考公,考编,当老师,进医院,研究所,高校,央国企,基本都无缘了,就连报名资格都被剥夺了,可谓是被党和国家遗弃的废材,统称“社会上的”,扔到社会去流浪,被用坏了就扔医院,长期超负载使用,零件修不好基本可以扔火里回炉重造了。体制内奉行找体制内的,都是党和国家选的人才,智力不差,样貌不丑,身材端正,收入稳定,安居房政策福利待遇也OK。因公出行都是报销,周末顺带“游山玩水“,这种体制内单身资源但凡想找对象,去社会上随便吆喝一声都排队。观察一下,基本没什么公务员在相亲,因为早就被邻里邻居抢光了。
哈哈哈,你是老六:就这不去的人大把人干呢,现在不缺人干活,你不干大把干呢,还有那个说农民工赚钱的,那个800+我估计肯定也就那一段时间,哪有这么赚钱,还是一句话,要想存下钱必须花销极低,能省的就不花钱,工资要高点
点赞 评论 收藏
分享
评论
点赞
5
分享

创作者周榜

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