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

#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

相关推荐

头像
04-29 10:53
已编辑
东北大学 自动化类
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务