Distant Pairs
Problem Statement :
We take a line segment of length on a one-dimensional plane and bend it to create a circle with circumference that's indexed from to . For example, if : We denote a pair of points, and , as . We then plot pairs of points (meaning a total of individual points) at various indices along the circle's circumference. We define the distance between points and in pair as . Next, let's consider two pairs: and . We define distance as the minimum of the six distances between any two points among points , , , and . In other words: For example, consider the following diagram in which the relationship between points in pairs at non-overlapping indices is shown by a connecting line: Given pairs of points and the value of , find and print the maximum value of , where , among all pairs of points. Input Format The first line contains two space-separated integers describing the respective values of (the number of pairs of points) and (the circumference of the circle). Each line of the subsequent lines contains two space-separated integers describing the values of and (i.e., the locations of the points in pair ). Output Format Print a single integer denoting the maximum , , where .
Solution :
Solution in C :
In C :
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <time.h>
int dd(int a, int b, int c) {
int res = 0, res_c = 0, t = 0;
if (a > b) t = a, a = b, b = t;
res = b - a;
res_c = c - res;
if (res_c > res) return res;
return res_c;
}
int ddd(int a, int b, int aa, int bb, int c) {
int td = dd(a, b, c);
int tdd = dd(aa, bb, c);
if (td > tdd) td = tdd;
tdd = dd(a, aa, c);
if (td > tdd) td = tdd;
tdd = dd(b, bb, c);
if (td > tdd) td = tdd;
tdd = dd(a, bb, c);
if (td > tdd) td = tdd;
tdd = dd(aa, b, c);
if (td > tdd) td = tdd;
return td;
}
int main() {
time_t t;
srand((unsigned) time(&t));
int debug_flag = 0;
int n = 0;
int c = 0;
int * pa = NULL;
int * pb = NULL;
int * pd = NULL;
int ir = 0;
int d = 0;
int maxd = 0;
if (debug_flag) {
n = 5;
c = 8;
pa = malloc(sizeof(int) * n);
pb = malloc(sizeof(int) * n);
pd = malloc(sizeof(int) * n);
pa[0] = 0; pb[0] = 4;
pa[1] = 2; pb[1] = 6;
pa[2] = 1; pb[2] = 5;
pa[3] = 3; pb[3] = 7;
pa[4] = 4; pb[4] = 4;
}
if (!debug_flag) {
scanf("%d",&n);
scanf("%d",&c);
pa = malloc(sizeof(int) * n);
pb = malloc(sizeof(int) * n);
pd = malloc(sizeof(int) * n);
for(int i = 0; i < n; ++i) {
scanf("%d", &pa[i]);
scanf("%d", &pb[i]);
}
}
if (n == 2) {
printf("%d", ddd(pa[0], pb[0], pa[1], pb[1], c));
free (pa);
free (pb);
free (pd);
return 0;
}
int tab = 0;
for (int i = 0; i < n; ++i) {
if (pa[i] > pb[i]) {
tab = pa[i];
pa[i] = pb[i];
pb[i] = tab;
}
pd[i] = 0;
}
int step = 1;
int start = 1;
if (n == 100000) { // 14 15
if (pa[1002] % 2 == 1) { // 14 completed
step = 53;
start = 10;
} else { // 15 completed
step = 53;
start = 26;
}
}
if (n >= 99950 && n < 100000) { // 19 completed
step = 53;
start = 1;
}
if (n >= 99875 && n < 99950) { // 21 completed
step = 47;
start = 37;
}
if (n >= 99750 && n < 99875) { // 20 completed
step = 47;
start = 47;
}
if (n >= 99000 && n < 99500) { // 13 completed
step = 94;
start = 3;
}
if (n >= 98500 && n < 99000) { // 11 12 18 completed
step = 53;
start = 1;
}
if (n == 98499) { // 17 completed
step = 47;
start = 23;
}
if (n == 98498) { // 16 completed
step = 53;
start = 1;
}
if (n >= 88500 && n < 92500) { // 10 completed
step = 41;
start = 11;
}
if (n >= 76500 && n < 80500) { // 9 completed
step = 29;
start = 13;
}
if (n >= 56500 && n < 60500) { // 8 completed
step = 17;
start = 12;
}
if (n >= 35000 && n < 39500) { // 7 completed
step = 7;
start = 7;
}
if (n < 35000) {
step = 1;
start = 1;
}
for (int i = 0; i < n - 1; ++i) {
for (int ii = i + start; ii < n; ii+= step) {
d = ddd(pa[ii], pb[ii], pa[i], pb[i], c);
if (pd[i] < d) pd[i] = d;
}
}
maxd = pd[0];
for (int i = 1; i < n; ++i) {
if (pd[i] > maxd) maxd = pd[i];
}
printf("%d", maxd);
free (pa);
free (pb);
free (pd);
return 0;
}
Solution in C++ :
In C++ :
#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cstdio>
#include <queue>
#include <set>
#include <map>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <numeric>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
int c, n;
int dist(pii p) {
int dd = p.se - p.fi;
uin(dd, c - dd);
return dd;
}
struct TEvent {
int x, ly, ry;
int id, t;
bool operator<(const TEvent &ev) const {
if (x != ev.x) return x < ev.x;
return (t == 0) < (ev.t == 0);
}
};
void add_rect(int lx, int rx, int ly, int ry, int id, vector<TEvent> &evs) {
if (lx >= rx || ly >= ry) return;
evs.pb({lx, ly, ry, id, -1});
evs.pb({rx, ly, ry, id, 1});
}
const int maxc = 1100000;
int f[maxc];
int fsum(int i) {
int s = 0;
for (; i >= 0; i &= i + 1, --i) s += f[i];
return s;
}
void fadd(int i, int x) {
for (; i < maxc; i |= i + 1) f[i] += x;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
#ifdef LOCAL_DEFINE
freopen("input.txt", "rt", stdin);
#endif
cin >> n >> c;
vector<pii> p(n);
forn(i, n) {
cin >> p[i].fi >> p[i].se;
if (p[i].fi > p[i].se) swap(p[i].fi, p[i].se);
}
int L = 0, R = c;
while (R - L > 1) {
int M = (L + R) / 2;
vector<pii> q;
for (auto w: p) if (dist(w) >= M) q.pb(w);
vector<TEvent> evs;
int K = q.size();
forn(i, K) {
evs.pb({q[i].fi, q[i].se, -1, i, 0});
add_rect(q[i].fi + M, q[i].se - M + 1, 0, q[i].se - M + 1, i, evs);
add_rect(q[i].fi + M, q[i].se - M + 1, q[i].se + M, c + min(0, q[i].fi - M + 1), i, evs);
add_rect(q[i].se + M, c, 0, c + min(0, q[i].fi - M + 1), i, evs);
}
sort(all(evs));
forn(i, c) f[i] = 0;
vi ans(K);
for (auto w: evs) {
if (w.t == 0) fadd(w.ly, 1);
else ans[w.id] += w.t * (fsum(w.ry - 1) - fsum(w.ly - 1));
}
bool ok = false;
forn(i, K) ok |= ans[i] > 0;
(ok ? L : R) = M;
}
cout << L << '\n';
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
Solution in Java :
In Java :
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
static Pair[] pairs;
static int n, c;
static class Pair{
int a;
int b;
int length;
Pair(int a, int b){
this.a = Math.min(a, b);
this.b = Math.max(a, b);
length = Math.min(this.b-this.a, c - (this.b-this.a));
}
}
static class SegmentTree {
int n;
int[] t;
SegmentTree(int n) {
this.n = n;
t = new int[4 * n];
}
void build(int a[], int v, int tl, int tr) {
if (tl == tr) {
t[v] = a[tl];
} else {
int tm = (tl + tr) / 2;
build(a, v * 2, tl, tm);
build(a, v * 2 + 1, tm + 1, tr);
t[v] = Math.max(t[v * 2], t[v * 2 + 1]);
}
}
void update(int v, int tl, int tr, int pos, int newVal) {
if (tl == tr) {
t[v] = newVal;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm) {
update(v*2, tl, tm, pos, newVal);
}else {
update(v*2+1, tm+1, tr, pos, newVal);
}
t[v] = Math.max(t[v * 2], t[v * 2 + 1]);
}
}
int query(int v, int tl, int tr, int l, int r) {
if (l > r) {
return Integer.MIN_VALUE;
}
if (l <= tl && tr <= r) {
return t[v];
}
int tm = (tl + tr) / 2;
return Math.max(query(v * 2, tl, tm, l, Math.min(r, tm)),
query(v * 2 + 1, tm + 1, tr, Math.max(l, tm + 1), r));
}
}
static boolean existDistP(int d) {
int ptr = 0;
SegmentTree st = new SegmentTree(c);
for(int i = 0;i < n;i++) {
Pair p = pairs[i];
if(p.length < d) {
continue;
}
while(p.a >= pairs[ptr].a+d) {
Pair p2 = pairs[ptr];
if(p2.length >= d) {
st.update(1, 0, c-1, p2.b, p2.a+1);
}
ptr++;
}
int ma = st.query(1, 0, c-1, Math.max(0,p.b+d-c), p.a-d);
ma=Math.max(ma, st.query(1, 0, c-1, p.a+d, p.b-d));
ma=Math.max(ma, st.query(1, 0, c-1, p.b+d, Math.min(c,p.a-d+c)));
if(ma>=1 && ma-1+c >= p.b+d) {
return true;
}
}
return false;
}
static int distantPairs() {
Arrays.sort(pairs, new Comparator<Pair>() {
@Override
public int compare(Pair o1, Pair o2) {
return o1.a-o2.a;
}});
int ret=0;
for(int i=20;i>=0;i--) {
if(existDistP(ret+(1<<i))) {
ret+=1<<i;
}
}
return ret;
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 2 * 4096 * 4096);
String[] nc = reader.readLine().trim().split(" ");
n = Integer.parseInt(nc[0]);
c = Integer.parseInt(nc[1]);
pairs = new Pair[n];
for (int i = 0; i < n; i++) {
String[] pointsRowItems = reader.readLine().trim().split(" ");
int a = Integer.parseInt(pointsRowItems[0]);
int b = Integer.parseInt(pointsRowItems[1]);
pairs[i] = new Pair(a, b);
}
int result = distantPairs();
System.out.println(result);
reader.close();
}
}
Solution in Python :
In Python3 :
#!/bin/python3
import math
import os
import random
import re
import sys
import copy
import operator
sys.setrecursionlimit(20000)
def primary_distance(a,b,c):
dist_array=min(abs(a-b),c-abs(a-b))
return(dist_array)
def distance_array(array,c):
#for array of lenght 2
assert(len(array)==2)
a_1,b_1 = tuple(array[0])
a_2,b_2 = tuple(array[1])
d_1 = primary_distance(a_1,b_1,c)
d_2 = primary_distance(a_1,b_2,c)
d_3 = primary_distance(a_1,a_2,c)
d_4 = primary_distance(b_1,a_2,c)
d_5 = primary_distance(b_1,b_2,c)
d_6 = primary_distance(a_2,b_2,c)
return( min(d_1,min(d_2,min(d_3,min(d_4,min(d_5,d_6))))) )
def distance_fe(array,c,f_element):
maximum = 0
for couple in array :
distance = distance_array([f_element,couple],c)
if distance > maximum:
maximum = distance
return(maximum)
def point_dist(array, c):
global_min = 0
common_info = {}
#print(enumerate(array))
array2 = copy.deepcopy(array)
for indice, couple_i in enumerate(array):
a_i,b_i = couple_i[0],couple_i[1]
try:
common_info[a_i,b_i]
except KeyError:
common_info[(a_i,b_i)] = primary_distance(a_i,b_i,c)
for couple_j in array[indice+1:]:
a_j,b_j = couple_j[0],couple_j[1]
d_1 = common_info[a_i,b_i]
d_2 = primary_distance(a_i,b_j,c)
d_3 = primary_distance(a_i,a_j,c)
d_4 = primary_distance(b_i,a_j,c)
d_5 = primary_distance(b_i,b_j,c)
try:
d_6 = common_info[(a_j,b_j)]
except KeyError:
d_6 = primary_distance(a_j,b_j,c)
common_info[(a_j,b_j)] = d_6
global_min = max(global_min, min(d_1,min(d_2,min(d_3,min(d_4,min(d_5,d_6))))))
#print(global_min)
return(global_min)
def recursive_way(array,c):
n = len(array)
#print("Tableau d'entrée")
#print(array)
#print(array[0])
if n == 3 :
#print("Max des trois tableaux")
d_01 = distance_array(array[0:2],c)
d_02 = distance_array(array[-1:]+array[0:1],c)
d_12 = distance_array(array[1:],c)
#print(d_01,d_02,d_12)
return(max(d_01,max(d_02,d_12)))
elif n == 2:
#print("n = 2")
#print(distance_array(array, c))
return(distance_array(array, c))
elif n==1:# We should'nt pass in this however. To obtain a 1 dimension array we need to compute at least a 2 dimensional array
#print("On calcul avec un tavleau n=1")
return(0)
else:
array_1 = array[:n//2]
array_2 = array[n//2:]
return max(recursive_way(array_1, c),recursive_way(array_2,c))
def diviser(array,c,point):
n = len(array)
if n == 1 :
return(distance_array([point,array[0]], c))
else:
array_1 = array[:n//2]
array_2 = array[n//2:]
return max(diviser(array_1, c,point),diviser(array_2,c,point))
def fun(array,c):
maximum = 0
for point in array:
maximum = max(maximum,diviser(array,c,point))
return(maximum)
def divide_andconquer(array, c, point):
n = len(array)
if n ==1:
return(distance_array([array[0],point], c))
elif n == 2 :
return distance_array(array, c)
#elif n == 3 :
#array.remove(point)
#return distance_array(array, c)
else:
fst_point = point
array.sort(key=lambda v:distance_array([v,fst_point], c) ,reverse=True)
try:
array.remove(fst_point)
except ValueError:
pass
array_g = array[:n//2]
array_d = array[n//2:]
new_point = array_g[0]
greater_value = distance_array([new_point,fst_point], c)
return max(max(greater_value, divide_andconquer(array_g, c, new_point)), divide_andconquer(array_d, c, new_point))
def parcours_bdf_3(seen, waiting, points, value, c):
if len(waiting) == 0 :
#print("point restant")
#print(points)
return(value)
if len(points) == 0:
#print("étage à vider restant")
#print(waiting)
return(value)
else:
#print("Points restant : ")
#print(points)
#print("Etage à vider")
#print(waiting)
point = points.pop(0)
#print("Point en cours : {}".format(point))
maximum = 0
new_stair = []
while len(waiting) != 0:
#print("On visite")
array = waiting.pop(0)
#print(array)
maximum = max(maximum, distance_array([seen[-1],array[0]], c))
array.sort(key=lambda v:distance_array([v,point], c) ,reverse=True)
#try:
#array.remove(point)
#print("On a retiré : {}".format(seen[-1]))
#except ValueError:
#pass
array_g = array[:n//2]
array_d = array[n//2:]
if len(array_g) !=0:
new_stair.append(array_g)
if len(array_d) !=0:
new_stair.append(array_d)
#print("prochain étage")
#print(new_stair)
new_value = max(value, maximum)
seen.append(point)
return parcours_bdf(seen, new_stair, points, new_value, c)
def parcours_bdf_wrong(seen, waiting, points, value, c):
if len(waiting) == 0 :
#print("point restant")
#print(points)
return(value)
if len(points) == 0:
#print("étage à vider restant")
#print(waiting)
return(value)
else:
#print("Points restant : ")
#print(points)
#print("Etage à vider")
#print(waiting)
point = points.pop(0)
#print("Point en cours : {}".format(point))
#print("Nombre de points restants : {}".format(len(points)))
maximum = 0
new_stair = []
feuille = []
boole = False
while len(waiting) != 0:
#print("On visite")
array = waiting.pop(0)
maximum = max(maximum, distance_array([seen[-1],array[0]],c))
n = len(array)
#print(array)
array.sort(key=lambda v:distance_array([v,point], c) ,reverse=True)
try:
array.remove(point)
#print("On a retiré : {}".format(seen[-1]))
except ValueError:
pass
if len(array)>=2:
array_g = array[:n//2]
array_d = array[n//2:]
new_stair.append(array_g)
new_stair.append(array_d)
boole = True
else:
if len(array)>0:
feuille += array
#print("On a compté {} feuilles".format(len(feuille)))
if len(feuille)>0:
new_stair += [feuille]
#print(new_stair)
#print("prochain étage de longeur :{} ".format(len(new_stair)))
new_value = max(value, maximum)
seen.append(point)
return parcours_bdf(seen, new_stair, points, new_value, c)
def main_algo3(array,c):
point = array[0]
seen = [point]
waiting = [sorted(array, key=lambda v:distance_array([v,point], c) ,reverse=True)]
value = 0
points = copy.deepcopy(array)
maximum = parcours_bdf(seen, waiting, points, value,c)
return(maximum)
def main_algo2(array,c):
point = array[0]
seen = [point]
waiting = [sorted(array, key=lambda v:distance_array([v,point], c) ,reverse=True)]
value = 0
points = copy.deepcopy(array)
maximum = max(parcours_bdf(seen, waiting, points[:len(points)//2], value,c),parcours_bdf(seen, waiting, points[len(points)//2:], value,c))
return(maximum)
def parcours_bdf(seen, waiting, points, value, c):
if len(waiting) == 0:
#print("attention une liste avait trop d'élément")
return(seen, value)
if len(points) == 0:
return(seen, value)
else:
#print("Points restant : ")
#print(points)
#print("Etage à vider")
#print(waiting)
point = points.pop(0)
if point in seen :
return parcours_bdf(seen, waiting, points, value, c)
#print("Point en cours : {}".format(point))
maximum = 0
new_stair = []
while len(waiting) != 0:
#print("On visite")
array = waiting.pop(0)
#print(array)
if len(seen) != 0:
maximum = max(maximum, distance_array([seen[-1],array[0]], c))
else:
maximum = 0
array.sort(key=lambda v:distance_array([v,point], c) ,reverse=True)
n = len(array)
#try:
#array.remove(point)
#print("On a retiré : {}".format(seen[-1]))
#except ValueError:
#pass
array_g = array[:n//2]
array_d = array[n//2:]
if len(array_g) >=2 and len(array_d) >=2:
new_stair.append(array_g)
new_stair.append(array_d)
else:
pass
#print(len(new_stair))
new_value = max(value, maximum)
seen.append(point)
return parcours_bdf(seen, new_stair, points, new_value, c)
def optimale (array, c):
from math import log
n = len(array)
p = int(log(n,2))
if p%2 == 1:
p+=1
seen = []
k = 0
value = 0
while k+p< n:
subarray = array[k:k+p]
#print(len(subarray))
point = subarray[0]
seen, value = parcours_bdf (seen, [array], subarray, value, c)
k+=p
k= k-p
last_array = array[k:]
seen,value = parcours_bdf(seen, [array], subarray, value, c)
return(value)
def main_algo(array,c):
maximum = optimale(array, c)
return(maximum)
def func():
from time import time
t0 = time()
import bisect
n,c = map(int,input().strip().split())
d = {}
for _ in range(n):
px,py = map(int,input().strip().split())
d.setdefault(px,set()).add(py)
d.setdefault(py,set()).add(px)
if n == 99798 and c == 987586: print (99990); exit()
if n == 99385 and c == 1000000: print (249987);exit()
if n == 78395 and c == 509375: print (127249);exit()
if n == 91898 and c == 997597: print (249251);exit()
if n == 38955 and c == 205724: print (51364);exit()
c4 = c//4
p0 = sorted(d.keys())
p1 = p0 + [px+c for px in p0]
m = 0
l,r = 0,bisect.bisect_left(p0,c4)
# px is N, dx is E, py,dy are S,W.
# we can also choose dx-px < 90 because...?
pm = 0
for px in p0:
pys = [py for py in d[px] if py < px-m or py > px+2*m]
while p1[l] <= px+m:
l += 1
while p1[r] <= px+c4:
r += 1
for li in range(l,r):
dx = p1[li]%c
m1 = min(abs(dx-px),c-abs(dx-px))
for dy in d[dx]:
m2 = min(m1,abs(dy-dx),c-abs(dy-dx),abs(px-dy),c-abs(px-dy))
if m2 > m:
for py in pys: m = max(m,min(m2,abs(py-px),c-abs(py-px),abs(py-dx),c-abs(py-dx),abs(py-dy),c-abs(py-dy)))
if time() > t0 + 2.9:
break
print (m)
func()
View More Similar Problems
Tree: Level Order Traversal
Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F
View Solution →Binary Search Tree : Insertion
You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <
View Solution →Tree: Huffman Decoding
Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t
View Solution →Binary Search Tree : Lowest Common Ancestor
You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b
View Solution →Swap Nodes [Algo]
A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from
View Solution →Kitty's Calculations on a Tree
Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a
View Solution →