6E - Exposition - CodeForces Solution


binary search data structures dsu trees two pointers *1900

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def binary_search(c1, c2):
    m = (c1 + c2 + 1) // 2
    while abs(c1 - c2) > 1:
        m = (c1 + c2 + 1) // 2
        if ok(m):
            c1 = m
        else:
            c2 = m
    m = min(m + 1, n)
    while not ok(m):
        m -= 1
    return m

def ok(m):
    if m > n:
        return False
    elif m <= 0:
        return True
    x, y = slide_min(n, m, h), slide_max(n, m, h)
    for i, j in zip(y, x):
        if i - j <= k:
            return True
    return False

def slide_min(n, k, a):
    ans, s = [], []
    j = 0
    for i in range(n):
        ai = a[i]
        while len(s) ^ j and a[s[-1]] >= ai:
            s.pop()
        s.append(i)
        while len(s) ^ j and s[j] + k <= i:
            j += 1
        if i + 1 >= k:
            ans.append(a[s[j]])
    return ans

def slide_max(n, k, a):
    ans, s = [], []
    j = 0
    for i in range(n):
        ai = a[i]
        while len(s) ^ j and a[s[-1]] <= ai:
            s.pop()
        s.append(i)
        while len(s) ^ j and s[j] + k <= i:
            j += 1
        if i + 1 >= k:
            ans.append(a[s[j]])
    return ans

n, k = map(int, input().split())
h = list(map(int, input().split()))
a = binary_search(0, n + 1)
ans = []
x, y = slide_min(n, a, h), slide_max(n, a, h)
for i in range(n - a + 1):
    if y[i] - x[i] <= k:
        ans.append(" ".join(map(str, (i + 1, i + a))))
b = len(ans)
print(a, b)
sys.stdout.write("\n".join(ans))

C++ Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair<int,int> pii;
const int maxn=100010;
int h[maxn];
int d1[maxn][20],d2[maxn][20];
void RMQ_init(int* A,int n)
{
	for(int i=0;i<n;i++) d1[i][0]=d2[i][0]=A[i];
	for(int j=1;(1<<j)<=n;j++)
		for(int i=0;i+(1<<j)-1<n;i++)
		{
			d1[i][j]=min(d1[i][j-1],d1[i+(1<<(j-1))][j-1]);
			d2[i][j]=max(d2[i][j-1],d2[i+(1<<(j-1))][j-1]);
		}
}
int RMQ1(int L,int R)
{
	int k=0;
	while((1<<(k+1))<=R-L+1) k++;
	return min(d1[L][k],d1[R-(1<<k)+1][k]);
}
int RMQ2(int L,int R)
{
	int k=0;
	while((1<<(k+1))<=R-L+1) k++;
	return max(d2[L][k],d2[R-(1<<k)+1][k]);
}
int main()
{
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++) scanf("%d",&h[i]);
	int f=0,r=0,maxl=0;
	vector<pii> v;
	RMQ_init(h,n);
	while(r<n)
	{
		while(r<n)
		{
			int minh=RMQ1(f,r),maxh=RMQ2(f,r);
			if(maxh-minh>k) break;
			if(r-f+1>maxl)
			{
				maxl=r-f+1;
				v.clear();
				v.push_back(make_pair(f,r));
			}
			else if(r-f+1==maxl)
				v.push_back(make_pair(f,r));
			r++;
		}
		while(f<r)
		{
			int minh=RMQ1(f,r),maxh=RMQ2(f,r);
			if(maxh-minh<=k) break;
			f++;
		}
	}
	printf("%d %d\n",maxl,(int)v.size());
	for(int i=0;i<v.size();i++) printf("%d %d\n",v[i].first+1,v[i].second+1);
	return 0;
}
		 	   	 		   	 		 	  			 	 	


Comments

Submit
0 Comments
More Questions

287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring