POJ3258(最小值最大化,二分)

River Hopscotch - POJ 3258 - Virtual Judge

根据题意,要求最大化牛牛从一个石头跳到另一个相邻的石头的最小距离,如果暴力的话绝对会超时,因此考虑二分,可知区间左端点是0,区间右端点是l,当去除的石头数量小于等于m的时候,区间左端点向中间移,当去除的石头数量大于m的时候,区间右端点向中间移动,因此最终能够得到最大化的两个相邻石头的最小距离,还需要设置一个起跳地点,每次都是从0开始,当从起跳点跳到a[i]小于mid的时候,说明这个石头可以去掉来增大最小值,如果大于mid,那么就不移走这个石头而是把这个石头当作下一个起跳点

(对于二分的使用范围还是不太清楚,一开始光想着暴力了,正如二分战神u宝的话:”cf红名以下的题皆可二分“%%%%)

AC代码:

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast")
// #include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <cstring>
#include <numeric>
#include <functional>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
// using i64 = long long;
#define lowbit(x) ((x) & -(x))
#define endl '\n'
#define IOS1 ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define IOS2 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<char> vc;
typedef long long ll;
typedef long long i64;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T>
T power1(T a, int b) {
	T res = 1;
	for (; b; b >>= 1, a = a * a) {
		if (b & 1) {
			res = res * a;
		}
	}
	return res;
}
template<class T>
T power2(T a, int b, T c) {
	T res = 1;
	for (; b; b >>= 1, a = a * a % c) {
		if (b & 1) {
			res = res * a % c;
		}
	}
	return res % c;
}
template <typename T>
T Myabs(T a) {
	return a >= 0 ? a : -a;
}
template <typename T>
inline void read(T& x)
{
	x = 0; int f = 1; char ch = getchar();
	while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
	while (isdigit(ch)) { x = x * 10 + ch - '0', ch = getchar(); }
	x *= f;
}
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
// const int mod = 998244353;
const double PI = acos(-1.0);
const double eps = 1e-6;
inline int sgn(double x) {
	return x < -eps ? -1 : x > eps;
}
/*
Tips:
   1.int? long long?
   2.don't submit wrong answer
   3.figure out logic first, then start writing please
   4.know about the range
   5.check if you have to input t or not
   6.modulo of negative numbers is not a%b, it is a%b + abs(b)
*/

void solve() {
	int l, n, m;
	cin >> l >> n >> m;
	vector<int> a(n + 1);
	a[0] = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	sort(a.begin() + 1, a.begin() + 1 + n);
	int maxx = l;
	int minn = 0;
	int ans, cnt;
	while (minn <= maxx) {
		ans = 0;
		cnt = 0;
		int mid = (minn + maxx) >> 1;
		for (int i = 1; i <= n; i++) {
			if (a[i] - ans < mid) {
				cnt++;
			}
			else {
				ans = a[i];
			}
		}
		if (cnt <= m) {
			minn = mid + 1;
		}
		else {
			maxx = mid - 1;
		}
	}
	cout << maxx << endl;
	return;
}
signed main() {
	//IOS1;
	IOS2;
#ifdef ONLINE_JUDGE
#else
	freopen("in.txt", "r", stdin);
#endif
	int __t = 1;
	// cin >> __t;
	for (int _t = 1; _t <= __t; _t++) {
		solve();
	}
	return 0;
}

2022/2/26纪念今天第一天用了freopen和对拍

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值