POJ3104 Drying
作者:互联网
知识点:二分
这个题,首先洗衣机烘干的那个数据,是带着自然晾干的数据的,也就是如果它是1的话那么烘干机就不起作用,然后我们想到二分,转化为判定,这个是最小化的二分,判定的时候思路要正确,分母是k-1而不是k,最重要的一点,判断函数里面的那个累加的变量可能到longlong,如果想用longlong那么,那么需要在循环的过程中判断,只要它大于二分的值,就返回错
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
const int N = 1e5 + 5;
int n, k, a[N];
bool check(int x) {
ll cnt = 0;
for (int i = 0; i < n; i++) {
if (a[i] <= x) continue;
int t = a[i] - x;
if (t % (k - 1) == 0) cnt += t / (k - 1);
else cnt += t / (k - 1) + 1;
}
return cnt <= x;
}
void solve(int l, int r) {
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
cout << l;
}
int main() {
cin >> n;
int Max = -1;
for (int i = 0; i < n; i++) {
cin >> a[i];
Max = max(Max, a[i]);
}
cin >> k;
if (k == 1) cout << Max;
else solve(1, 1000000000);
return 0;
}
标签:二分,typedef,POJ3104,Drying,mid,int,include,define 来源: https://blog.csdn.net/qq_33942309/article/details/122785657