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

#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

相关推荐

看新闻上说,印度媒体都在密集发申请攻略,咨询量直接涨了30%印度、韩国、新加坡的申请意愿特别突出,感觉要成科技人才的新选择了~我的offer还没有呢!
ysb:哥们就不明白了,自己的人才都留不住,然后找外国,咋滴给外国人才高福利朝九晚五不加班是吗,然后我们大学生996,加班,无offer,摆地摊,送外卖是吗,有点意思,很英明
我的秋招日记
点赞 评论 收藏
分享
09-28 22:01
已编辑
广西科技大学 IT技术支持
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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