POI2006MAG-Warehouse

切比雪夫距离 #曼哈顿距离 #POI #Year2006

切比雪夫距离向曼哈顿距离转换 \((x,y)\rightarrow (\frac{x+y}{2},\frac{x-y}{2})\)

直接转换会炸精度,考虑先不除以 \(2\)

然后根据小学数学,可以将 \(x,y\) 分别排序,然后前缀和+二分求出中间的那个

但是这样会导致答案不一定为整点,所以上下波动 \(\pm 1\)

// Author: xiaruize
#ifndef ONLINE_JUDGE
#define debug(x) cerr << "On Line:" << __LINE__ << #x << "=" << x << endl
bool start_of_memory_use;
#else
#define debug(x)
#endif
#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
clock_t start_clock = clock();
#endif
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define mms(arr, n) memset(arr, n, sizeof(arr))
#define rep(i, a, n) for (int i = (a); i <= (n); ++i)
#define per(i, n, a) for (int i = (n); i >= (a); --i)
int max(int a, int b)

{
	if (a > b)
		return a;
	return b;
}
int min(int a, int b)
{
	if (a < b)
		return a;
	return b;
}
const int INF = 0x5fffffffffffffff;
const int MOD = 1000000007;
const int N = 2e5 + 10;

int n;
int x[N], y[N], t[N];
pii a[N], b[N];

int calc(int cx, int cy)
{
	int res = 0;
	rep(i, 1, n)
	{
		res += max(abs(x[i] - cx), abs(y[i] - cy)) * t[i];
	}
	return res;
}

void solve()
{
	cin >> n;
	rep(i, 1, n) cin >> x[i] >> y[i] >> t[i];
	rep(i, 1, n)
	{
		a[i] = {x[i] + y[i], t[i]};
		b[i] = {x[i] - y[i], t[i]};
	}
	sort(a + 1, a + n + 1);
	sort(b + 1, b + n + 1);
	rep(i, 1, n)
	{
		a[i].second += a[i - 1].second;
		b[i].second += b[i - 1].second;
	}
	int l = 1, r = n;
	while (l < r)
	{
		int mid = l + r >> 1;
		if (a[mid].second * 2 >= a[n].second)
			r = mid;
		else
			l = mid + 1;
	}
	int tx = a[r].first;
	l = 1, r = n;
	while (l < r)
	{
		int mid = l + r >> 1;
		if (b[mid].second * 2 >= b[n].second)
			r = mid;
		else
			l = mid + 1;
	}
	int ty = b[r].first;
	// cerr << tx << ' ' << ty << endl;
	int nx = (tx + ty) / 2, ny = (tx - ty) / 2;
	pii res = {nx, ny};
	int mx = INF;
	rep(i, 0, 1)
	{
		rep(j, 0, 1)
		{
			int cur = calc(nx + i, ny + j);
			if (cur < mx)
			{
				mx = cur;
				res = {nx + i, ny + j};
			}
		}
	}
	cout << res.first << ' ' << res.second << endl;
}

#ifndef ONLINE_JUDGE
bool end_of_memory_use;
#endif

signed main()
{
	// freopen(".in","r",stdin);
	// freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int testcase = 1;
	// cin >> testcase;
	while (testcase--)
		solve();
#ifndef ONLINE_JUDGE
	cerr << "Memory use:" << (&end_of_memory_use - &start_of_memory_use) / 1024.0 / 1024.0 << "MiB" << endl;
	cerr << "Time use:" << (double)clock() / CLOCKS_PER_SEC * 1000.0 << "ms" << endl;
#endif
	return 0;
}
posted @ 2024-04-04 17:07  xiaruize  阅读(17)  评论(0)    收藏  举报