第三次模拟考-大数据方向-编程题

#1 数组变换
#思路:即数组中的每个数都除2,直至不能整除,判断剩下的数是否为同一个
# -*-- coding:utf-8 -*-
import sys

if __name__ == '__main__':
    while 1:
        t = sys.stdin.readline().strip()
        if not t:
            break
        t = int(t)
        nums = [int(i) for i in sys.stdin.readline().strip().split(' ')]
        for i in xrange(t):
            while nums[i]%2==0:
                nums[i] /=2

        numSet = set(nums)
        if len(numSet) ==1:
            print "YES"
        else:
            print "NO"
#2 排序字序列
#思路:题目意思,判断数组中 严格递增字序列,非严格递增字序列、严格递减字序列、非严格递减字序列
#  取了一个巧,把数组中相邻的数字重复的进行去重,然后寻找严格递增字序列和严格递减字序列就可以了  # -*-- coding:utf-8 -*-
import sys


def getUp(numsO, ind,lens):

    while ind+1<lens:
        if numsO[ind+1]>numsO[ind]:
            ind+=1
        else:
            return ind+1
    return -1


def getDown(numsO, ind, lens):

    while ind + 1 < lens:
        if numsO[ind + 1] < numsO[ind]:
            ind += 1
        else:
            return ind + 1
    return -1


if __name__ == '__main__':
    while 1:
        t = sys.stdin.readline().strip()
        if not t:
            break
        t = int(t)
        nums = [int(i) for i in sys.stdin.readline().strip().split(' ')]
        if t <= 2:
            print 1
            continue
        numsO=[nums[0]]
        for i in xrange(1,t):
            if nums[i]!=nums[i-1]:
                numsO.append(nums[i])
        res=0
        lens = len(numsO)
        if lens==1:
            print 1
            continue
        ind = 0
        while ind+1 < lens:
            if numsO[ind+1]>numsO[ind]:
                ind = getUp(numsO,ind,lens)
                res+=1
                if ind==-1:
                    break
            else:
                ind = getDown(numsO,ind,lens)
                res += 1
                if ind == -1:
                    break
            if ind==lens-1:
                res+=1
        print res

#3 :牛牛的序列
#思路:跟上一题类似,把数组划分成不同的递增字序列,若存在字序列a,b , 即:
###   1) 判断能否改变a的最大的能不能和b进行合并
###   2) 判断能否改变b的最小的能不能和a进行合并
# -*-- coding:utf-8 -*-
import sys

if __name__ == '__main__':
    while 1:
        t = sys.stdin.readline().strip()
        if not t:
            break
        t = long(t)
        nums = [long(i) for i in sys.stdin.readline().strip().split(' ')]
        res= []
        mins=0
        maxs=0
        for i in xrange(1,t):
            if nums[i]>nums[i-1]:
                maxs = i
            else:
                res.append((mins,maxs))
                mins=i
                maxs=i
        res.append((mins,maxs))
        lens = len(res)-1
        ans =-1
        for ind in xrange(lens):
            minInd = res[ind][1]
            maxInd = res[ind+1][0]
            if minInd - 1 <0 and nums[maxInd]>=2:
                ans = max(ans,1+res[ind+1][1])
                continue
            if res[ind][0] == res[ind][1] or res[ind+1][0]==res[ind+1][1]:
                ans = max(ans,res[ind][1]-res[ind][0]+1+res[ind+1][1]-res[ind+1][0]+1)
                continue
            if nums[maxInd]-nums[minInd-1]>=2 or nums[maxInd+1]-nums[minInd]>=2:
                ans = max(ans,res[ind][1]-res[ind][0]+1+res[ind+1][1]-res[ind+1][0]+1)

        print ans

全部评论
大牛啊,受益匪浅!!get~
点赞 回复 分享
发布于 2017-05-19 21:19

相关推荐

真tmd的恶心,1.面试开始先说我讲简历讲得不好,要怎样讲怎样讲,先讲背景,再讲技术,然后再讲提升多少多少,一顿说教。2.接着讲项目,我先把背景讲完,开始讲重点,面试官立即打断说讲一下重点,无语。3.接着聊到了项目的对比学习的正样本采样,说我正样本采样是错的,我解释了十几分钟,还是说我错的,我在上一家实习用这个方法能work,并经过市场的检验,并且是顶会论文的复现,再怎么不对也不可能是错的。4.面试官,说都没说面试结束就退出会议,把面试者晾在会议里面,丝毫不尊重面试者难受的点:1.一开始是讲得不好是欣然接受的,毕竟是学习。2.我按照面试官的要求,先讲背景,再讲技术。当我讲完背景再讲技术的时候(甚至已经开始蹦出了几个技术名词),凭什么打断我说讲重点,是不能听出人家重点开始了?这也能理解,每个人都有犯错,我也没放心上。3.我自己做过的项目,我了解得肯定比他多,他这样贬低我做过的项目,说我的工作是错误的,作为一个技术人员,我是完全不能接受的,因此我就和他解释,但无论怎么解释都说我错。凭什么,作为面试官自己不了解相关技术,别人用这个方式work,凭什么还认为这个方法是错的,不接受面试者的解释。4.这个无可厚非,作为面试官,不打招呼就退出会议,把面试者晾着,本身就是有问题。综上所述,我现在不觉得第一第二点也是我的问题,面试官有很大的问题,就是专门恶心人的,总结面试官说教,不尊重面试者,打击面试者,不接受好的面试者,技术一般的守旧固执分子。有这种人部门有这种人怎么发展啊。最后去查了一下,岗位关闭了。也有可能是招到人了来恶心人的,但是也很cs
牛客20646354...:招黑奴啊,算法工程师一天200?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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