CF教育场124C补题「每日一题」

题意:两排已经连通的电脑,现在怎么连接,使得坏一台电脑其他电脑也可以继续连接,所以直接考虑端点,有7种情况:

1.a1-b1,an-bn

2.a1-bn,an-b1

3.a1-b1 ,an与bn单独去找

4.a1-bn ,an与b1单独去找

5.an-b1 ,a1与bn单独去找

6.an-bn ,a1与b1单独去找

7.a1与b1与an与bn单独去找

AC代码:

#include <iostream>
#include <unordered_map>
#include <stdio.h>
#include <cstring>
#include <math.h>
#include <algorithm>
#define int long long
using namespace std;
const int N = 2e5+10;
//const int M = 
const int mod = 1e9;
int a[N],b[N];
bool sta[N],stb[N];
int ans[N];
int n;
//int tot=0;

int erfena(int x)
{
	int idx = lower_bound(a+1,a+1+n,x) - a;
	int res = min(abs(x-a[idx]),abs(x-a[idx-1]));
	return res;
}

int erfenb(int x)
{
	int idx = lower_bound(b+1,b+1+n,x) - b;
	int res = min(abs(x-b[idx]),abs(x-b[idx-1]));
	return res;
}

signed main() {
	int t;scanf("%lld",&t);
	while(t--)
	{
		int tot=0;
		scanf("%lld",&n);
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
		for(int i=1;i<=n;i++) scanf("%lld",&b[i]);
		int a1=a[1],an=a[n],b1=b[1],bn=b[n],ans=0x3f3f3f3f3f3f3f3f;
		sort(a+1,a+1+n);sort(b+1,b+1+n);
		a[0]=b[0]=0x3f3f3f3f3f3f;
		a[n+1]=b[n+1]=0x3f3f3f3f3f3f;
		
		int res = abs(a1-b1) + abs(an-bn);
		ans = min(ans,res);
		res = abs(a1-bn) + abs(an-b1);
		ans = min(ans,res);
		res = abs(a1-b1) + erfena(bn) + erfenb(an);
		ans = min(ans,res);
		res = abs(a1-bn) + erfena(b1) + erfenb(an);
		ans = min(ans,res);
		res = abs(an-bn) + erfena(b1) + erfenb(a1);
		ans = min(ans,res);
		res = abs(an-b1) + erfena(bn) + erfenb(a1);
		ans = min(ans,res);
		res = erfena(b1) + erfena(bn) + erfenb(a1) + erfenb(an);
		ans = min(ans,res);
		cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_60789461/article/details/123428778