point2D

 avatar
NguyenAnhQuan
c_cpp
7 months ago
1.0 kB
1
Indexable
Never
#include <iostream>
#include <cstdlib>

#define LIM 1000000
#define X first
#define Y second
#define EL cout << "\n";

using namespace std;

int n;
pair <int, int> a[LIM + 5];

int cpr(pair <int, int> x, pair <int, int> y) {
	if (x.X <= y.X) {
		if (x.X < y.X) {
			return -1;
		}
		else {
			if (x.Y > y.Y) {
				return -1;
			}
			else if (x.Y < y.Y) {
				return 1;
			}
		}
	}
	else {
		return 1;
	}

	return 0;
}

void qs(int l, int r)
{
	int i = l, j = r;
	pair <int, int> p = a[l + rand() % (r - l + 1)];
	while (i <= j) {
		//while (a[i] < p) i++;
		while (cpr(a[i], p) == -1) i++;
		//while (a[j] > p) j--;
		while (cpr(a[j], p) == 1) j--;
		if (i <= j) {
			swap(a[i], a[j]);
			i++; j--;
		}
	}
	if (j > l) qs(l, j);
	if (i < r) qs(i, r);
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	//Input 
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i].X >> a[i].Y;

	qs(1, n);

	// Output
	for (int i = 1; i <= n; i++)
		cout << a[i].X << " " << a[i].Y << "\n";

	// cout << cpr({1, 1}, {2, 1});
	return 0;
}
Leave a Comment