题目 › usaco-1523

OohMoo Milk

USACO 2025 US Open · 金组 · 2025-03 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

Farmer John is trying to make his world's famous OohMoo Milk to sell for a profit. He has $N$ $(1 \leq N \leq 10^5)$ bottles that he is trying to fill. Each bottle initially contains some amount of milk $m_i$ $(0 \leq m_i \leq 10^9)$. Every day, he takes $A$ $(1 \le A \le N)$ bottles and fills each bottle with one unit of milk.

Unfortunately, Farmer Nhoj, Farmer John's competitor in the business of OohMoo Milk, knows about Farmer John's production processes and has a plan to curtail his business. Every day, after Farmer John fills his $A$ bottles, Farmer Nhoj will sneakily steal one unit of milk from each of $B$ $(0 \le B < A)$ different nonempty bottles. To remain sneaky, Farmer Nhoj chooses $B$ so that it is strictly less than $A$, so that it is less likely for Farmer John to discover him.

After $D$ ($1 \leq D \leq 10^9$) days, Farmer John will sell his OohMoo Milk. If a bottle has $M$ units of milk, it will sell for $M^2$ moonies.

Let $P$ be the unique profit such that FJ can guarantee that he makes at least $P$ profit regardless of how FN behaves, and FN can guarantee that FJ makes at most $P$ profit regardless of how FJ behaves. Output the value of $P$ modulo $10^9+7$.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line of the input contains $N$ and $D$, where $N$ is the number of bottles and $D$ is the number of days that take place.

The second line of the input contains $A$ and $B$ representing the number of units of milk that Farmer John fills and Farmer Nhoj steals respectively.

The third line of the input contains $N$ space-separated integers $m_i$ representing the initial amount of milk in each bottle.

OUTPUT FORMAT (print output to the terminal / stdout):

Output the value of $P$ modulo $10^9+7$.

SAMPLE INPUT:

5 4
4 2
4 10 8 10 10

SAMPLE OUTPUT:

546

On the first day, Farmer John could add milk to the second, third, fourth, and fifth bottles. Then, Farmer Nhoj could remove milk from the second and fourth bottles.

Thus, the new amount of milk in each bottle is $$[4, 10, 8, 10, 10] \to [4, 11, 9, 11, 11] \to [4, 10, 9, 10, 11].$$

After four days, the amount of milk in each bottle could be $$[4, 10, 8, 10, 10] \to [4, 10, 9, 10, 11] \to [4, 10, 10, 11, 11] \to [4, 11, 11, 11, 11] \to [4, 11, 11, 12, 12].$$

The total amount of moonies Farmer John would make in this situation is $4^2+11^2+11^2+12^2+12^2 = 546$. It can be shown that this is the value of $P$.

SAMPLE INPUT:

10 5
5 1
1 2 3 4 5 6 7 8 9 10

SAMPLE OUTPUT:

777

SAMPLE INPUT:

5 1000000000
3 1
0 1 2 3 4

SAMPLE OUTPUT:

10

Make sure you output $P$ modulo $10^9+7$.

SCORING:

  • Inputs 4-6: $N,D\le 1000$.
  • Inputs 7-10: $D\le 10^6$.
  • Inputs 11-20: No additional constraints.

Problem credits: Suhas Nagar

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 13.6k平均输出 token(含思考) ↓
  • 25.6输出速度 tokens/s ↑
  • 450 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 32.2k平均输出 token(含思考) ↓
  • 189.7输出速度 tokens/s ↑
  • 163 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 85.7k平均输出 token(含思考) ↓
  • 139.5输出速度 tokens/s ↑
  • 648 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 90 行
#include <bits/stdc++.h>
using namespace std;

using int64 = long long;
const int64 MOD = 1000000007;

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

    int N, A, B;
    int64 D;
    cin >> N >> D;
    cin >> A >> B;

    vector<int64> m(N);
    for (auto &x : m) cin >> x;
    sort(m.begin(), m.end());

    int64 answer = 0;
    auto addSquare = [&](int64 x) {
        int64 r = x % MOD;
        answer = (answer + r * r) % MOD;
    };

    // Only the initially largest A bottles need to be considered active.
    int inactive = N - A;
    for (int i = 0; i < inactive; ++i) addSquare(m[i]);

    vector<int64> a(m.begin() + inactive, m.end());
    vector<int64> pref(A + 1, 0), prefUpper(A + 1, 0);
    vector<int64> prefSq(A + 1, 0), prefUpperSq(A + 1, 0);

    for (int i = 0; i < A; ++i) {
        pref[i + 1] = pref[i] + a[i];
        prefUpper[i + 1] = prefUpper[i] + a[i] + D;

        int64 r1 = a[i] % MOD;
        int64 r2 = (a[i] + D) % MOD;
        prefSq[i + 1] = (prefSq[i] + r1 * r1) % MOD;
        prefUpperSq[i + 1] = (prefUpperSq[i] + r2 * r2) % MOD;
    }

    const int64 target = pref[A] + (int64)(A - B) * D;

    // At level L, bottle i has clamp(L, a[i], a[i]+D) milk.
    // Find the water-filling level for the required total amount.
    auto baseSum = [&](int64 L, int *upIndex = nullptr,
                       int *lowIndex = nullptr) -> int64 {
        int up = upper_bound(a.begin(), a.end(), L - D) - a.begin();
        int low = upper_bound(a.begin(), a.end(), L) - a.begin();

        if (upIndex) *upIndex = up;
        if (lowIndex) *lowIndex = low;

        return prefUpper[up]
             + (int64)(low - up) * L
             + (pref[A] - pref[low]);
    };

    int64 lo = 0, hi = 2000000000LL;
    while (lo < hi) {
        int64 mid = (lo + hi + 1) / 2;
        if (baseSum(mid) <= target) lo = mid;
        else hi = mid - 1;
    }

    int up, low;
    int64 base = baseSum(lo, &up, &low);
    int64 freeCount = low - up;
    int64 raised = target - base; // Number of free bottles at level lo+1.
    int64 atBase = freeCount - raised;

    auto rangeSum = [](const vector<int64> &p, int l, int r) -> int64 {
        return (p[r] - p[l] + MOD) % MOD;
    };

    // Bottles fixed at their upper bounds.
    answer = (answer + rangeSum(prefUpperSq, 0, up)) % MOD;
    // Bottles fixed at their initial amounts.
    answer = (answer + rangeSum(prefSq, low, A)) % MOD;

    int64 r0 = lo % MOD;
    int64 r1 = (lo + 1) % MOD;
    answer = (answer + (atBase % MOD) * r0 % MOD * r0) % MOD;
    answer = (answer + (raised % MOD) * r1 % MOD * r1) % MOD;

    cout << answer << '\n';
    return 0;
}