124082024-12-17 11:41:0742Síkság (55)pypy3Időlimit túllépés 19/55303ms32000 KiB
# O(N*log^2)
from sys import stdin
input=stdin.readline

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
       
N=int(input())
A=list(map(int,input().split()))

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

start=0
length=1

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)
RészfeladatÖsszpontTesztVerdiktIdőMemória
base19/55
1Elfogadva0/045ms21244 KiB
2Időlimit túllépés0/0287ms27100 KiB
3Elfogadva2/248ms21192 KiB
4Elfogadva2/245ms21228 KiB
5Elfogadva2/250ms21124 KiB
6Elfogadva2/254ms21468 KiB
7Elfogadva3/357ms21708 KiB
8Elfogadva2/2166ms24040 KiB
9Elfogadva3/3155ms25288 KiB
10Időlimit túllépés0/3303ms27880 KiB
11Időlimit túllépés0/3291ms26088 KiB
12Elfogadva3/3123ms23348 KiB
13Futási hiba0/365ms32000 KiB
14Futási hiba0/364ms32000 KiB
15Futási hiba0/368ms32000 KiB
16Futási hiba0/379ms32000 KiB
17Futási hiba0/368ms32000 KiB
18Futási hiba0/378ms32000 KiB
19Futási hiba0/379ms32000 KiB
20Futási hiba0/379ms32000 KiB
21Időlimit túllépés0/3303ms29564 KiB
22Futási hiba0/382ms32000 KiB