Created
April 7, 2021 15:06
-
-
Save solairerove/4756921977831aeef80b8fc93f969ac8 to your computer and use it in GitHub Desktop.
Задача на программирование: точки и отрезки
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
1. Заведите массив Х с началами отрезков и отсортируйте быстрой сортировкой | |
2. Повторите шаг 1 для концов отрезков - массив У | |
3. Напишите 2 функции для искомой точки А: | |
1-я будет искать в массиве Х все элементы, которые меньше или равны(!) А и возвращать их количество N | |
2-я будет искать в массиве У все элементы, которые строго (!) меньше А и возвращать их количество M | |
4. В цикле прогоните каждую точку из списка через обе функции - ответом будет разность N - M | |
**/ | |
import java.util.*; | |
class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int n = sc.nextInt(); | |
int m = sc.nextInt(); | |
int[] x = new int[n]; | |
int[] y = new int[n]; | |
for (int i = 0; i < n; i++) { | |
x[i] = sc.nextInt(); | |
y[i] = sc.nextInt(); | |
} | |
quickSort(x); | |
quickSort(y); | |
for (int i = 0; i < m; i++) { | |
int a = sc.nextInt(); | |
int res = findInX(x, a) - findInY(y, a); | |
System.out.print(String.format("%s ", res)); | |
} | |
} | |
private static void quickSort(int[] arr) { | |
quickSort(arr, 0, arr.length - 1); | |
} | |
private static void quickSort(int[] arr, int low, int high) { | |
if (high <= low) return; | |
int lt = low, gt = high, i = low + 1, v = arr[low]; | |
while (i <= gt) { | |
if (arr[i] < v) swap(arr, lt++, i++); | |
else if (arr[i] > v) swap(arr, i, gt--); | |
else i++; | |
} | |
quickSort(arr, low, lt - 1); | |
quickSort(arr, gt + 1, high); | |
} | |
private static void swap(int[] arr, int i, int j) { | |
int temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} | |
private static int findInX(int[] x, int a) { | |
return findInX(x, a, 0, x.length); | |
} | |
private static int findInX(int[] x, int a, int low, int high) { | |
int n = x.length; | |
if (n == 0) return 0; | |
if (a < x[low]) return low; | |
if (a > x[high - 1]) return high; | |
for (;;) { | |
if (low + 1 == high) { | |
return low + 1; | |
} | |
int mid = low + (high - low) / 2; | |
if (a < x[mid]) high = mid; | |
else low = mid; | |
} | |
} | |
private static int findInY(int[] y, int a) { | |
return findInY(y, a, 0, y.length); | |
} | |
private static int findInY(int[] y, int a, int low, int high) { | |
int n = y.length; | |
if (n == 0) return 0; | |
if (a < y[low]) return low; | |
if (a > y[high - 1]) return high; | |
for (;;) { | |
if (low + 1 == high) { | |
return a == y[low] ? low : (low + 1); | |
} | |
int mid = low + (high - low) / 2; | |
if (a <= y[mid]) high = mid; | |
else low = mid; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment