UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212777#3830. DPanjunnan00ms0kbC++111.8kb2024-10-20 11:02:102024-10-20 12:43:05

answer

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

// 检查给定的x是否能满足条件
bool canAchieve(const vector<int>& a, int n, int m, int k, int x) {
    vector<int> b(n, 0);
    for (int i = 0; i < m; i++) {
        // 每次操作可以从每个位置开始
        for (int j = 0; j < n; j++) {
            if (b[j] < a[j]) { // 如果当前b[j]还没达到目标ai
                int needed = a[j] - b[j]; // 需要加上的量
                int length = min(x, n - j); // 当前操作可以影响的长度

                // 试图从j开始进行操作
                for (int d = 0; d < length; d++) {
                    // 当前影响的值是(x - d) * k
                    int addValue = (x - d) * k;
                    // 如果影响值加上当前b[j+d]后的值仍然比目标小
                    if (b[j + d] + addValue < a[j + d]) {
                        b[j + d] += addValue; // 更新b[j+d]的值
                    }
                }
            }
        }
    }

    // 最后检查b是否满足条件
    for (int i = 0; i < n; i++) {
        if (b[i] < a[i]) return false;
    }
    return true;
}

int main() {
    int n, m, k;
    cin >> n >> m >> k; // 输入n, m, k
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i]; // 输入数组a
    }

    int left = 0, right = INT_MAX; // 二分查找的范围
    int answer = -1;

    while (left <= right) {
        int mid = left + (right - left) / 2; // 中间值
        if (canAchieve(a, n, m, k, mid)) {
            answer = mid; // 如果能达到,记录这个值
            right = mid - 1; // 尝试更小的x
        } else {
            left = mid + 1; // 尝试更大的x
        }
    }

    cout << answer << endl; // 输出结果
    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

input:

5 2 0
1 4 2 4 2

output:


result:


Subtask #2:

score: 0
Time Limit Exceeded

Test #6:

score: 0
Time Limit Exceeded

input:

100000 3 0
55571 62024 7086 67802 12577 42004 72323 45260 40287 39641 64847 42428 20318 80027 1675 1...

output:


result:


Subtask #3:

score: 0
Time Limit Exceeded

Test #11:

score: 0
Time Limit Exceeded

input:

1000 1 1
993 705 891 648 406 9 983 913 370 408 607 47 85 199 767 448 723 255 253 302 754 135 641 726...

output:


result:


Subtask #4:

score: 0
Time Limit Exceeded

Test #16:

score: 0
Time Limit Exceeded

input:

100000 4 1
10156 27005 39480 62054 63362 28009 65831 14391 93683 7991 17027 26313 1403 15929 30567 1...

output:


result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #21:

score: 0
Time Limit Exceeded

input:

1000 169 2
853 394 985 195 859 84 227 50 641 365 409 407 845 112 7 582 920 965 590 869 550 781 40 44...

output:


result:


Subtask #6:

score: 0
Time Limit Exceeded

Test #26:

score: 0
Time Limit Exceeded

input:

100000 31778 2
9482 16920 65207 41338 19891 97532 56865 946 56851 5908 19270 8065 53369 97241 69805 ...

output:


result: