P3853 [TJOI2007] 路标设置

文章讨论了如何在路标设置问题中使用二分搜索算法,指出直接二分加并非最优解,而是发现遍历方式在某些情况下更有效。作者通过实例P3853/TJOI2007解释了二分法在特定条件下的局限性,强调了遍历策略的价值。
摘要由CSDN通过智能技术生成

二分答案

怎么加路标是关键

一开始我想的是二分加,每次加中间,那边距离多出来就往哪边去加

但实际上压根不能这么算,很奇怪,为啥呢?感觉应该是会浪费的缘故,就往中间插实际上不如隔着x顺序插好,被二分和遍历背刺的第二次,有时候二分做不到优化甚至压根不正确,遍历才是真爱啊

// Problem: P3853 [TJOI2007] 路标设置
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3853
// Memory Limit: 128 MB
// Time Limit: 1000 ms
// Date: 2024-03-26 21:01:02
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define endl '\n'
#define int int64_t
#define ld long double
using namespace std;
int dx[] = { -1,0,1,0 };
int dy[] = { 0,1,0,-1 };
const int NC = 2e3 + 10;
char ches[NC][NC];
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int a[N], b[N], n, m, k;
bool check(int x) {
	//需要几个路标
	int tot = 0;
	for (int i = 2; i <= n; ++i) {
		if (a[i] - a[i - 1] <= x) continue;
		int cnt = 0;
		int sub = a[i] - a[i - 1];
		tot += (sub - 1)/x;
		//cout << "tot = " << tot << endl;
		if (tot > k) return false;
	}
	return true;
}
void solve() {
	cin >> m >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	sort(a + 1, a + n + 1);
	int l = 1, r = m, ans = m;
	while (l <= r) {
		int mid = l + r >> 1;
		if (check(mid)) {
			ans = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	cout << ans << endl;
}
signed main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t = 1;
	//cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值