CF260E Dividing Kingdom(暴力+线段树)

版权声明:蒟蒻的博文,dalao转载标明出处就好吖 https://blog.csdn.net/jokingcoder/article/details/88957579

原题

E. Dividing Kingdom

A country called Flatland is an infinite two-dimensional plane. Flatland has n n cities, each of them is a point on the plane.

Flatland is ruled by king Circle IV. Circle IV has 9 9 sons. He wants to give each of his sons part of Flatland to rule. For that, he wants to draw four distinct straight lines, such that two of them are parallel to the Ox axis, and two others are parallel to the Oy axis. At that, no straight line can go through any city. Thus, Flatland will be divided into 9 9 parts, and each son will be given exactly one of these parts. Circle IV thought a little, evaluated his sons’ obedience and decided that the i i -th son should get the part of Flatland that has exactly a i a_i cities.

Help Circle find such four straight lines that if we divide Flatland into 9 9 parts by these lines, the resulting parts can be given to the sons so that son number i got the part of Flatland which contains a i a_i cities.

input

The first line contains integer n ( 9 n 1 0 5 ) (9 ≤ n ≤ 10^5) — the number of cities in Flatland. Next n lines each contain two space-separated integers: xi, yi ( 1 0 9 x i , y i 1 0 9 ) (-10^9 \leq x_i,y_i \leq 10^9) — the coordinates of the i i -th city. No two cities are located at the same point. The last line contains nine space-separated integers: a 1 , a 2 , . . . , a 9 , i = 1 9 a i = n a_1,a_2,...,a_9,\sum_{i=1}^9a_i=n .

Output

If there is no solution, print a single integer -1.

Otherwise, print in the first line two distinct real space-separated numbers: x 1 , x 2 x_1,x_2 — the abscissas of the straight lines that are parallel to the O y Oy axis. And in the second line print two distinct real space-separated numbers: y 1 , y 2 y_1,y_2 — the ordinates of the straight lines, parallel to the O x Ox . If there are multiple solutions, print any of them.

When the answer is being checked, a city is considered to lie on a straight line, if the distance between the city and the line doesn’t exceed 1 0 6 10^{-6} . Two straight lines are considered the same if the distance between them doesn’t exceed 1 0 6 10^{-6} .

Examples

input

9
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
1 1 1 1 1 1 1 1 1

output

1.5000000000 2.5000000000
1.5000000000 2.5000000000

input

15
4 4
-1 -3
1 5
3 -4
-4 4
-1 1
3 -3
-4 -5
-3 3
3 2
4 1
-4 2
-2 -5
-3 4
-1 4
2 1 2 1 2 1 3 2 1

output

-3.5000000000 2.0000000000
3.5000000000 -1.0000000000

input

10
-2 10
6 0
-16 -6
-4 13
-4 -2
-17 -10
9 15
18 16
-5 2
10 -5
2 1 1 1 1 1 1 1 1

output

-1

Note

The solution for the first sample test is shown below:
p1
The solution for the second sample test is shown below:
p2
There is no solution for the third sample test.

Solution

发现被分成了 9 9 个块, 可以用 9 ! = 362880 9!=362880 种排列来考虑每种是否符合
对于每次 c h e c k check ,我们可以用线段树来维护 3 × 3 3 \times 3 的块中
第一行的第一列也就是代码中的 q w q [ 0 ] qwq[0]
前两行的第一列也就是代码中的 q w q [ 0 ] + q w q [ 3 ] qwq[0]+qwq[3]
第一行的前两列也就是代码中的 q w q [ 0 ] + q w q [ 1 ] qwq[0]+qwq[1]
前两行的前两列也就是代码中的 q w q [ 0 ] + q w q [ 1 ] + q w q [ 3 ] + q w q [ 4 ] qwq[0]+qwq[1]+qwq[3]+qwq[4]
只要这些条件满足了
就一定能保证每个块一一对应
证明很显然啊qwq
时间复杂度 O ( 9 ! × l o g n ) \mathcal{O(9!\times logn)}

Code

#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <vector>
#define N 100010

using namespace std;

struct Node {
	int x, y;
}p[N];
vector<int> v[N << 2];
int a[9], q[9], qwq[9], x[N], y[N], n;

inline bool cmp(Node x, Node y) {
	return x.x < y.x;
}

inline void build(int l, int r, int rt) {
	for (int i = l; i <= r; ++i) {
		v[rt].push_back(p[i].y);
	}
	sort(v[rt].begin(), v[rt].end());
	if (l == r) return;
	int mid = l + r >> 1;
	build(l, mid, rt << 1);
	build(mid + 1, r, rt << 1 | 1);
}

inline int query(int l, int r, int rt, int xx, int yy) {
	if (p[l].x > xx) return 0;
	if (p[r].x <= xx) return upper_bound(v[rt].begin(), v[rt].end(), yy) - v[rt].begin();
	int mid = l + r >> 1;
	return query(l, mid, rt << 1, xx, yy) + query(mid + 1, r, rt << 1 | 1, xx, yy);
}

inline void doit() {
	int x0 = qwq[0] + qwq[3] + qwq[6], x1 = x0 + qwq[1] + qwq[4] + qwq[7], y0 = qwq[0] + qwq[1] + qwq[2], y1 = y0 + qwq[3] + qwq[4] + qwq[5];
	if (x[x0] == x[x0 + 1] || y[y0] == y[y0 + 1] || x[x1] == x[x1 + 1] || y[y1] == y[y1 + 1]) return;
	if (query(1, n, 1, x[x0], y[y0]) != qwq[0] || query(1, n, 1, x[x0], y[y1]) != qwq[0] + qwq[3] ||
		query(1, n, 1, x[x1], y[y0]) != qwq[0] + qwq[1] || query(1, n, 1, x[x1], y[y1]) != qwq[0] + qwq[1] + qwq[3] + qwq[4]) return;
	printf("%.8lf %.8lf\n%.8lf %.8lf", (double)x[x0] + 0.5, (double)x[x1] + 0.5, (double)y[y0] + 0.5, (double)y[y1] + 0.5);
	exit(0);
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d", &x[i], &y[i]);
		p[i].x = x[i];
		p[i].y = y[i];
	}
	for (int i = 0; i < 9; ++i) {
		scanf("%d", &a[i]);
		q[i] = i;
	}
	sort(x + 1, x + n + 1);
	sort(y + 1, y + n + 1);
	sort(p + 1, p + n + 1, cmp);
	build(1, n, 1);
	do {
		for (int i = 0; i < 9; ++i) qwq[i] = a[q[i]];
		doit();
	}while(next_permutation(q, q + 9));
	puts("-1");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/jokingcoder/article/details/88957579