D. Wooden Toy Festival

链接:https://codeforces.com/problemset/problem/1840/D
洛谷链接:https://www.luogu.com.cn/problem/CF1840D
思路:
这个题目之前好像碰到过类似的,感觉就是对长度二分,然后遍历,注意有个坑的点在于应该是2*len因为把第一个点当成左端点,来找右端点自然是2*len
卡了好久,还是看题解过的,感觉思路僵硬,没走到正确的路上
代码:

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<cmath>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
#include<set>

typedef long long ll;
using namespace std;
const int N = 1e9 + 10;

bool jd(int mi, vector<int>lst)
{
	int cy = lst[0] +2 * mi;//cy:右端点
    int times = 1;
	int i = 0;
	while (times <= 4)
	{
		if (times == 4)return false;
		while (i<lst.size()and  lst[i] <= cy)i++;
		if (i == lst.size())break;
		else
		{
			cy = lst[i] +2 * mi;
			times++;
		}
	}
	return true;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--)
	{
		vector<int>lst;
		int x, n; cin >> n;
		map<int, int>vis;
		for (int i = 0; i < n; i++)
		{
			cin >> x;
			if (!vis[x])lst.push_back(x);
			vis[x] = 1;
		}
		int ans = 0;
		if (lst.size() > 3)
		{
			sort(lst.begin(), lst.end());
			int l = 1, r = lst[lst.size() - 1] - 1;
			r /= 3; r++;
			while (l < r)
			{
				int mid = l + (r - l) / 2;
				if (jd(mid,lst))r = mid;
				else l = mid + 1;
			}
			ans = l;
		}
		cout << ans << '\n';
	}
	return 0;
}

posted on 2024-05-06 20:45  WHUStar  阅读(4)  评论(0编辑  收藏  举报