P8198 [传智杯 #4 决赛] 背单词的小智 —二分答案

#include 
#define bug printf("************************\n");
#define fuck(x) cout << #x << " -> " << x << endl
#define endl '\n'
#define int long long
using namespace std;
constexpr int N = 1e6, inf = 0x3f3f3f3f;
int arr[N];
int n, k;
int low, high;
bool check(int mid)
{
    int now = 0;
    int next = 0;
    int sum = 0;
    int cnt = 1;
    for (int i = 0; i < n; i++)
    {
        sum += arr[i];
        if (sum >= mid)
        {
            cnt++;
            sum = arr[i];
        }
    }
    return cnt > k;
}

int serchhigh()
{
    while (low < high)
    {
        int mid = low + high + 1 >> 1;
        if (check(mid))
            low = mid;
        else
            high = mid - 1;
    }
    return low;
}
void solve()
{
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
        arr[i] = arr[i] * arr[i];
        low = max(low, arr[i]);
        high += arr[i];
    }
    int ans = serchhigh();
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    solve();

    return 0;
}

你可能感兴趣的:(ou_fan的学习记录,算法,c++,数据结构)