求大佬调调,卡在第七八个点wa
查看原帖
求大佬调调,卡在第七八个点wa
772926
lingkerio楼主2024/1/27 09:34

按说,如果有输出,则是mid满足题意而mid+1不满足,这样的贪心应该是正确的,但还是WA了,求大佬教教

#include <iostream>
#include <numeric>
using namespace std;

int arr[100002];

int main()
{
    ios::sync_with_stdio(false);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n, k;
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
    }

    long acc = accumulate(arr + 1, arr + 1 + n, 0);
    long ans = 0;

    if (acc < k)
        cout << 0;
    else {
        int left = 1, right = acc / k;
        while (left <= right) {
            int mid = (left + right + 1) / 2;
            long temp = 0;
            for (int i = 1; i <= n; i++) {
                temp += arr[i] / mid;
            }

            if (temp < k)
                right = mid-1;
            else {
                long temp1 = 0;
                for (int i = 1; i <= n; i++) {
                    temp1 += arr[i] / (mid + 1);
                }
                if (temp1 < k) {
                    ans = mid;
                    break;
                    }
                else
                    left = mid+1;
            }
        }
        cout << ans;
    }
}
2024/1/27 09:34
加载中...