QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#102829#3190. GatheringPetroTarnavskyi#WA 21ms5568kbC++172.7kb2023-05-03 18:25:542023-05-03 18:25:56

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-03 18:25:56]
  • 评测
  • 测评结果:WA
  • 用时:21ms
  • 内存:5568kb
  • [2023-05-03 18:25:54]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a); i<(b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)
#define MP make_pair
#define PB push_back
#define F first
#define S second

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

struct point
{
	int x, y;
};

pair<point, point> inter(pair<point, point> a, pair<point, point> b)
{
	return {{max(a.F.x, b.F.x), max(a.F.y, b.F.y)}, {min(a.S.x, b.S.x), min(a.S.y, b.S.y)}};
}

vector<point> v;

LL calc(point p)
{
	int n = SZ(v);
	LL ans = 0;
	FOR(i, 0, n)
	{
		ans += abs(v[i].x - p.x) + abs(v[i].y - p.y);
	}	
	return ans;
}

bool bad(point p)
{
	return (p.x & 1) ^ (p.y & 1);
}

LL ter(point a, point b, point di)
{
	if (bad(a))
		a.x += di.x, a.y += di.y;
	if (bad(b))
		b.x -= di.x, b.y -= di.y;
	
	a = {(a.x + a.y) / 2, (a.x - a.y) / 2};	
	b = {(b.x + b.y) / 2, (b.x - b.y) / 2};	
	
	int len = abs(a.x - b.x);
	if (len == 0) return calc(a);
	int l = 0, r = len;
	int dx = (b.x - a.x) / len;
	
	int dy = (b.y - a.y) / len;
	assert(1ll * dy * len == (b.y - a.y));
	
	while (l + 2 < r)
	{
		int d = (r - l) / 3;
		int l2 = l + d;
		int r2 = r - d;
		if (calc({a.x + dx * l2, a.y + dy * l2}) < calc({a.x + dx * r2, a.y + dy * r2}))
			r = r2;
		else
			l = l2;
	}
	LL best = 4e18;
	FOR(L, l, r + 1)
		best = min(best, calc({a.x + dx * L, a.y + dy * L}));
	return best;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n;
	cin >> n;
	v.resize(n);
	VI x(n), y(n);
	FOR(i, 0, n) 
	{
		cin >> v[i].x >> v[i].y;
		x[i] = v[i].x;
		y[i] = v[i].y;
	}

	int d;
	cin >> d;
	
	vector<pair<point, point>> a(n);
	FOR(i, 0, n)
	{
		a[i] = {{x[i] - d + y[i], x[i] - d - y[i]}, {x[i] + d + y[i], x[i] + d - y[i]}};
	}
	pair<point, point> sq = a[0];
	FOR(i, 1, n)
	{
		sq = inter(sq, a[i]);
	}
	if (sq.F.x > sq.S.x || sq.F.y > sq.S.y || (sq.F.x == sq.S.x && sq.F.y == sq.S.y && bad(sq.F)))
	{
		cout << "impossible\n";
		return 0;
	}
	sort(ALL(x));
	sort(ALL(y));
	pair<point, point> b = {{x[n / 2] + y[n / 2], x[n / 2] - y[n / 2]}, {x[n / 2] + y[n / 2], x[n / 2] - y[n / 2]}};
	
	auto res = inter(sq, b);
	if (res.F.x <= res.S.x && res.F.y <= res.S.y)
	{
		point p = {(b.F.x + b.F.y) / 2, ((b.F.x - b.F.y) / 2)};
		cout << calc(p) << endl;
		return 0;
	}
	
	LL ans = 4e18;
	point p1 = {sq.F.x, sq.F.y};
	point p2 = {sq.F.x, sq.S.y};
	point p3 = {sq.S.x, sq.S.y};
	point p4 = {sq.S.x, sq.F.y};
	
	ans = min(ans, ter(p1, p2, {0, 1}));
	ans = min(ans, ter(p2, p3, {1, 0}));
	ans = min(ans, ter(p3, p4, {0, -1}));
	ans = min(ans, ter(p4, p1, {-1, 0}));
	
	cout << ans << endl;	
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3368kb

input:

5
3 1
4 1
5 9
2 6
5 3
10

output:

18

result:

ok single line: '18'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3392kb

input:

5
3 1
4 1
5 9
2 6
5 3
5

output:

20

result:

ok single line: '20'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3332kb

input:

5
3 1
4 1
5 9
2 6
5 3
4

output:

impossible

result:

ok single line: 'impossible'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3348kb

input:

2
0 0
0 0
0

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3440kb

input:

2
0 0
0 1
1

output:

1

result:

ok single line: '1'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3456kb

input:

2
0 0
0 1
0

output:

impossible

result:

ok single line: 'impossible'

Test #7:

score: 0
Accepted
time: 2ms
memory: 3388kb

input:

4
0 0
0 10
10 0
10 10
10

output:

40

result:

ok single line: '40'

Test #8:

score: 0
Accepted
time: 2ms
memory: 3404kb

input:

5
0 0
0 10
9 0
9 10
0 0
10

output:

47

result:

ok single line: '47'

Test #9:

score: 0
Accepted
time: 2ms
memory: 3368kb

input:

5
0 0
0 10
9 0
9 10
9 0
10

output:

47

result:

ok single line: '47'

Test #10:

score: 0
Accepted
time: 2ms
memory: 3428kb

input:

6
0 0
0 10
10 0
10 10
1 8
9 2
12

output:

54

result:

ok single line: '54'

Test #11:

score: 0
Accepted
time: 1ms
memory: 3340kb

input:

6
0 0
0 10
10 0
10 10
1 8
9 2
15

output:

54

result:

ok single line: '54'

Test #12:

score: 0
Accepted
time: 2ms
memory: 3296kb

input:

7
0 0
0 100
100 0
100 100
0 0
10 10
23 28
150

output:

481

result:

ok single line: '481'

Test #13:

score: 0
Accepted
time: 1ms
memory: 3332kb

input:

5
0 0
0 100
100 0
100 100
50 20
130

output:

400

result:

ok single line: '400'

Test #14:

score: 0
Accepted
time: 2ms
memory: 3456kb

input:

5
0 0
0 100
100 0
100 100
50 10
130

output:

410

result:

ok single line: '410'

Test #15:

score: 0
Accepted
time: 2ms
memory: 3380kb

input:

5
0 0
0 100
100 0
100 100
60 0
130

output:

430

result:

ok single line: '430'

Test #16:

score: 0
Accepted
time: 2ms
memory: 3332kb

input:

5
0 0
0 100
100 0
100 100
60 10
130

output:

420

result:

ok single line: '420'

Test #17:

score: 0
Accepted
time: 2ms
memory: 3440kb

input:

5
0 0
0 100
100 0
100 100
65 35
130

output:

400

result:

ok single line: '400'

Test #18:

score: 0
Accepted
time: 2ms
memory: 3448kb

input:

5
0 0
0 100
100 0
100 100
70 30
130

output:

410

result:

ok single line: '410'

Test #19:

score: 0
Accepted
time: 1ms
memory: 3348kb

input:

5
0 0
0 100
100 0
100 100
90 10
130

output:

450

result:

ok single line: '450'

Test #20:

score: 0
Accepted
time: 0ms
memory: 3396kb

input:

7
0 0
0 75
100 25
100 100
10 60
10 60
10 90
125

output:

391

result:

ok single line: '391'

Test #21:

score: 0
Accepted
time: 1ms
memory: 3340kb

input:

7
100 0
25 0
75 100
0 100
40 10
40 10
10 10
125

output:

391

result:

ok single line: '391'

Test #22:

score: 0
Accepted
time: 0ms
memory: 3380kb

input:

7
100 100
100 25
0 75
0 0
90 40
90 40
90 10
125

output:

391

result:

ok single line: '391'

Test #23:

score: 0
Accepted
time: 2ms
memory: 3336kb

input:

7
0 100
75 100
25 0
100 0
60 90
60 90
90 90
125

output:

391

result:

ok single line: '391'

Test #24:

score: 0
Accepted
time: 1ms
memory: 3344kb

input:

7
100 0
100 75
0 25
0 100
90 60
90 60
90 90
125

output:

391

result:

ok single line: '391'

Test #25:

score: 0
Accepted
time: 2ms
memory: 3404kb

input:

7
100 100
25 100
75 0
0 0
40 90
40 90
10 90
125

output:

391

result:

ok single line: '391'

Test #26:

score: 0
Accepted
time: 1ms
memory: 3340kb

input:

7
0 100
0 25
100 75
100 0
10 40
10 40
10 10
125

output:

391

result:

ok single line: '391'

Test #27:

score: 0
Accepted
time: 0ms
memory: 3336kb

input:

7
0 0
75 0
25 100
100 100
60 10
60 10
90 10
125

output:

391

result:

ok single line: '391'

Test #28:

score: 0
Accepted
time: 2ms
memory: 3384kb

input:

7
0 0
0 75
100 25
100 100
20 20
20 30
10 80
125

output:

445

result:

ok single line: '445'

Test #29:

score: 0
Accepted
time: 1ms
memory: 3452kb

input:

7
100 0
25 0
75 100
0 100
80 20
70 20
20 10
125

output:

445

result:

ok single line: '445'

Test #30:

score: 0
Accepted
time: 2ms
memory: 3376kb

input:

7
100 100
100 25
0 75
0 0
80 80
80 70
90 20
125

output:

445

result:

ok single line: '445'

Test #31:

score: 0
Accepted
time: 2ms
memory: 3440kb

input:

7
0 100
75 100
25 0
100 0
20 80
30 80
80 90
125

output:

445

result:

ok single line: '445'

Test #32:

score: 0
Accepted
time: 2ms
memory: 3380kb

input:

7
100 0
100 75
0 25
0 100
80 20
80 30
90 80
125

output:

445

result:

ok single line: '445'

Test #33:

score: 0
Accepted
time: 2ms
memory: 3384kb

input:

7
100 100
25 100
75 0
0 0
80 80
70 80
20 90
125

output:

445

result:

ok single line: '445'

Test #34:

score: 0
Accepted
time: 2ms
memory: 3340kb

input:

7
0 100
0 25
100 75
100 0
20 80
20 70
10 20
125

output:

445

result:

ok single line: '445'

Test #35:

score: 0
Accepted
time: 0ms
memory: 3440kb

input:

7
0 0
75 0
25 100
100 100
20 20
30 20
80 10
125

output:

445

result:

ok single line: '445'

Test #36:

score: 0
Accepted
time: 4ms
memory: 3556kb

input:

8534
116302263 676752
668674805 908095735
71666532 896336333
736731265 314989458
535244751 391441865
108520141 206814702
534045436 974836612
238077914 413854218
705377000 397905153
440974757 972995558
282367380 881784893
823504433 879663491
70219520 215814456
726604669 318196447
939145515 30877684
9...

output:

impossible

result:

ok single line: 'impossible'

Test #37:

score: -100
Wrong Answer
time: 21ms
memory: 5568kb

input:

81186
32136890 931344747
142033094 612429664
615834245 42381126
65593714 138671771
634339601 802658530
946304224 323905944
750713949 324946495
702821643 940601429
475865193 905399131
916652024 597291728
899826486 18853429
677992867 225924365
751262914 328663250
446106639 47265033
60274957 927220015
...

output:

impossible

result:

wrong answer 1st lines differ - expected: '42340175200180', found: 'impossible'