124062024-12-17 11:37:5142Síkság (55)pypy3Time limit exceeded 19/55303ms32000 KiB
# O(N*log^2)

class minSegmentTree:
    def __init__(self):
        self.tree = None

    def buildTree(self,List):
        n = len(List)
        self.tree = [0 for i in range(2*n)]

        for i in range(n):
            self.tree[i+n] = List[i]

        for i in range(n-1,0,-1):
            self.tree[i] = min(self.tree[i<<1] , self.tree[i<<1 | 1])

    def minInRange(self, left, right):
        n = len(self.tree)//2
        result = self.tree[n+left]
        left += n
        right += n

        while left<right:
            if left&1:
                result = min(result,self.tree[left])
                left += 1
            if right&1:
                right -= 1
                result = min(result,self.tree[right])

            left >>= 1
            right >>= 1

        return result

class maxSegmentTree:
    def __init__(self):
        self.tree = None

    def buildTree(self,List):
        n = len(List)
        self.tree = [0 for i in range(2*n)]

        for i in range(n):
            self.tree[i+n] = List[i]

        for i in range(n-1,0,-1):
            self.tree[i] = max(self.tree[i<<1] , self.tree[i<<1 | 1])

    def maxInRange(self, left, right):
        n = len(self.tree)//2
        result = self.tree[n+left]
        left += n
        right += n

        while left<right:
            if left&1:
                result = max(result,self.tree[left])
                left += 1
            if right&1:
                right -= 1
                result = max(result,self.tree[right])

            left >>= 1
            right >>= 1

        return result
       
#def main():
#    segmentTree = SegmentTree()
#
#    #Sample test case
#    sampleList = [1,3,5,7,9,11]
#    segmentTree.buildTree(sampleList)
#
#    print("Array representation of tree :",segmentTree.tree)
#    print("Sum of the interval [1,3] :",segmentTree.sumInRange(1,4))
#
#
#if __name__ == "__main__":
#    main()


N=int(input())
A=list(map(int,input().split()))

minTree=minSegmentTree()
minTree.buildTree(A)
maxTree=maxSegmentTree()
maxTree.buildTree(A)

start=0
length=1

#print(A)
#for i in range(len(A)):
#    for j in range(i+1,len(A)+1):
#         if maxTree.maxInRange(i,j)-minTree.minInRange(i,j)<=1:
#              if j-i>length:
#                  start=i
#                  length=j-i

for i in range(1,len(A)):
    if maxTree.maxInRange(0,i+1)-minTree.minInRange(0,i+1) <= 1:
        if i+1>length:
            start=0
            length=i+1
    else:
        lo=0
        hi=i
        while hi-lo>1:
            mid=(hi+lo)//2
            if maxTree.maxInRange(mid,i+1)-minTree.minInRange(mid,i+1)<=1:
                hi=mid
            else:
                lo=mid
        if i+1-hi>length:
            start=hi
            length=i+1-hi
                  
print(length,start+1)
SubtaskSumTestVerdictTimeMemory
base19/55
1Accepted0/046ms21100 KiB
2Time limit exceeded0/0289ms27096 KiB
3Accepted2/250ms21136 KiB
4Accepted2/250ms21196 KiB
5Accepted2/245ms21212 KiB
6Accepted2/263ms21604 KiB
7Accepted3/364ms21588 KiB
8Accepted2/2156ms23916 KiB
9Accepted3/3165ms25056 KiB
10Time limit exceeded0/3303ms27252 KiB
11Time limit exceeded0/3263ms26092 KiB
12Accepted3/3112ms23276 KiB
13Runtime error0/378ms32000 KiB
14Runtime error0/376ms32000 KiB
15Runtime error0/368ms32000 KiB
16Runtime error0/378ms32000 KiB
17Runtime error0/379ms32000 KiB
18Runtime error0/379ms32000 KiB
19Runtime error0/381ms32000 KiB
20Runtime error0/367ms32000 KiB
21Time limit exceeded0/3293ms29672 KiB
22Runtime error0/371ms32000 KiB