题目 › usaco-1478

Watering the Plants

USACO 2025 January · 铂金组 · 2025-01 · L4 顶尖 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

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

Bessie's garden has $N$ plants labeled $1$ through $N$ ($2\leq N\leq 5\cdot 10^5$) from left to right. Bessie knows that plant $i$ requires at least $w_i$ ($0\leq w_i \leq 10^6$) units of water.

Bessie has a very peculiar irrigation system with $N-1$ canals, numbered $1$ through $N-1$. Each canal $i$ has an associated unit cost $c_i$ ($1\le c_i\le 10^6$), such that Bessie can pay $c_i k$ to provide plants $i$ and $i+1$ each with $k$ units of water, where $k$ is a non-negative integer.

Bessie is busy and may not have time to use all the canals. For each $2\leq i \leq N$ compute the minimum cost required to water plants $1$ through $i$ using only the first $i-1$ canals.

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

The first line contains a single positive integer $N$.

The second line contains $N$ space-separated integers $w_1, \ldots, w_N$.

The third line contains $N-1$ space-separated integers $c_1, \ldots, c_{N-1}$.

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

Output $N-1$ newline-separated integers. The $(i-1)$th integer should contain the minimum cost to water the first $i$ plants using the first $i-1$ canals.

SAMPLE INPUT:

3
39 69 33
30 29

SAMPLE OUTPUT:

2070
2127

The minimum cost to water the first $2$ plants using the first canal is to pay $30 \cdot 69 = 2070$ by using the first canal $69$ times.

The minimum cost to water the first $3$ plants is to use the first canal $39$ times and the second canal $33$ times, paying $39 \cdot 30 + 29 \cdot 33 = 2127$.

SAMPLE INPUT:

3
33 82 36
19 1

SAMPLE OUTPUT:

1558
676

SAMPLE INPUT:

8
35 89 44 1 35 3 62 50
7 86 94 62 63 9 49

SAMPLE OUTPUT:

623
4099
4114
6269
6272
6827
8827

SCORING:

  • Input 4: $N \leq 200$, and all $w_i \leq 200$.
  • Inputs 5-6: All $w_i \leq 200$.
  • Inputs 7-10: $N \leq 5000$.
  • Inputs 11-14: All $w_i$ and $c_i$ are generated independently and uniformly at random.
  • Inputs 15-19: No additional constraints.

Problem credits: Benjamin Qi

结果

Kimi K3.1 ProKimi

max

3/3

解出的采样 ↑

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

Kimi K3.1 Pro(high)Kimi high

high

3/3

解出的采样 ↑

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

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 47.8k平均输出 token(含思考) ↓
  • 171.4输出速度 tokens/s ↑
  • 257 秒延迟 ↓
  • 2.14×CPU 时间 / 官方解 ↓
  • 不确定 1、同阶 2时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

2/3

解出的采样 ↑

  • 0.0采样 1 · 编译错误
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 46.9k平均输出 token(含思考) ↓
  • 117.9输出速度 tokens/s ↑
  • 378 秒延迟 ↓
  • 0.73×CPU 时间 / 官方解 ↓
  • 同阶 2时间复杂度(相对官方解)

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

代码

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

using ll = long long;

struct Node {
    Node *l, *r;
    ll len, sz, sum;
    int slope, mn, mx;
    unsigned pri;
    int lazyA;
    ll lazyB;
    bool rev;
};

static Node* pool;
static int poolPtr = 0;
static unsigned rngState = 123456789u;

static inline unsigned rnd() {
    rngState ^= rngState << 13;
    rngState ^= rngState >> 17;
    rngState ^= rngState << 5;
    return rngState;
}

static inline ll sz(Node* t) {
    return t ? t->sz : 0;
}

static Node* makeNode(ll len, int slope) {
    if (len <= 0) return nullptr;
    Node* t = &pool[poolPtr++];
    t->l = t->r = nullptr;
    t->len = t->sz = len;
    t->slope = t->mn = t->mx = slope;
    t->sum = (ll)slope * len;
    t->pri = rnd();
    t->lazyA = 1;
    t->lazyB = 0;
    t->rev = false;
    return t;
}

static inline void pull(Node* t) {
    t->sz = t->len + sz(t->l) + sz(t->r);
    t->sum = (ll)t->slope * t->len;
    t->mn = t->mx = t->slope;
    if (t->l) {
        t->sum += t->l->sum;
        t->mn = min(t->mn, t->l->mn);
        t->mx = max(t->mx, t->l->mx);
    }
    if (t->r) {
        t->sum += t->r->sum;
        t->mn = min(t->mn, t->r->mn);
        t->mx = max(t->mx, t->r->mx);
    }
}

// Replaces every slope s in the subtree by a*s+b.
static void apply(Node* t, int a, ll b) {
    if (!t) return;
    t->slope = (int)(a * (ll)t->slope + b);
    t->sum = a * t->sum + b * t->sz;
    if (a == 1) {
        t->mn = (int)(t->mn + b);
        t->mx = (int)(t->mx + b);
    } else {
        int oldMn = t->mn, oldMx = t->mx;
        t->mn = (int)(-oldMx + b);
        t->mx = (int)(-oldMn + b);
    }
    t->lazyA *= a;
    t->lazyB = a * t->lazyB + b;
}

static inline void push(Node* t) {
    if (!t) return;
    if (t->rev) {
        swap(t->l, t->r);
        if (t->l) t->l->rev ^= 1;
        if (t->r) t->r->rev ^= 1;
        t->rev = false;
    }
    if (t->lazyA != 1 || t->lazyB != 0) {
        apply(t->l, t->lazyA, t->lazyB);
        apply(t->r, t->lazyA, t->lazyB);
        t->lazyA = 1;
        t->lazyB = 0;
    }
}

static Node* merge(Node* a, Node* b) {
    if (!a) return b;
    if (!b) return a;
    if (a->pri > b->pri) {
        push(a);
        a->r = merge(a->r, b);
        pull(a);
        return a;
    } else {
        push(b);
        b->l = merge(a, b->l);
        pull(b);
        return b;
    }
}

// Splits into the first k units of length and the remainder.
static void split(Node* t, ll k, Node*& a, Node*& b) {
    if (!t) {
        a = b = nullptr;
        return;
    }
    push(t);
    ll leftSize = sz(t->l);
    if (k < leftSize) {
        split(t->l, k, a, t->l);
        b = t;
        pull(b);
        return;
    }
    if (k > leftSize + t->len) {
        split(t->r, k - leftSize - t->len, t->r, b);
        a = t;
        pull(a);
        return;
    }

    ll take = k - leftSize;
    if (take == 0) {
        a = t->l;
        t->l = nullptr;
        b = t;
        pull(b);
    } else if (take == t->len) {
        b = t->r;
        t->r = nullptr;
        a = t;
        pull(a);
    } else {
        Node* rightPart = makeNode(t->len - take, t->slope);
        rightPart->r = t->r;
        pull(rightPart);
        t->len = take;
        t->r = nullptr;
        a = t;
        b = rightPart;
        pull(a);
    }
}

// Returns the length before the first segment whose slope is at least c.
static ll firstGE(Node* t, ll c) {
    if (!t || t->mx < c) return sz(t);
    push(t);
    if (t->l && t->l->mx >= c) return firstGE(t->l, c);
    ll leftSize = sz(t->l);
    if (t->slope >= c) return leftSize;
    return leftSize + t->len + firstGE(t->r, c);
}

static ll prefixSum(Node* t, ll k) {
    if (!t || k <= 0) return 0;
    push(t);
    ll leftSize = sz(t->l);
    if (k <= leftSize) return prefixSum(t->l, k);

    ll result = t->l ? t->l->sum : 0;
    k -= leftSize;
    if (k <= t->len) return result + (ll)t->slope * k;

    result += (ll)t->slope * t->len;
    return result + prefixSum(t->r, k - t->len);
}

static inline ll evaluate(Node* t, ll base, int lastSlope, ll length, ll d) {
    if (d >= length) {
        return base + (t ? t->sum : 0) + (ll)lastSlope * (d - length);
    }
    return base + prefixSum(t, d);
}

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

    int N;
    cin >> N;

    vector<ll> w(N + 1), c(N);
    for (int i = 1; i <= N; ++i) cin >> w[i];
    for (int i = 1; i < N; ++i) cin >> c[i];

    pool = new Node[2 * N + 5];

    // V_2(d) = c_1 * max(w_1,d).  Its derivative is represented by T.
    ll base = c[1] * w[1];
    ll length = w[1];
    int lastSlope = (int)c[1];
    Node* T = makeNode(length, 0);

    string output;
    output.reserve((size_t)N * 12);
    output += to_string(evaluate(T, base, lastSlope, length, w[2]));
    output.push_back('\n');

    for (int j = 2; j < N; ++j) {
        ll W = w[j];
        ll cost = c[j];

        // Restrict the derivative to [0,W], extending with its final slope if needed.
        if (W < length) {
            Node *P, *discarded;
            split(T, W, P, discarded);
            T = P;
        } else if (W > length) {
            T = merge(T, makeNode(W - length, lastSlope));
        }

        // Remove the suffix with slopes >= cost.  It becomes a flat prefix.
        ll pos = firstGE(T, cost);
        Node *A, *X;
        if (pos == 0) {
            A = nullptr;
            X = T;
        } else if (pos == W) {
            A = T;
            X = nullptr;
        } else {
            split(T, pos, A, X);
        }

        ll flatLength = sz(X);
        base += (A ? A->sum : 0) + cost * flatLength;

        // Reverse the remaining segments and replace every slope s by cost-s.
        if (A) {
            apply(A, -1, cost);
            A->rev ^= 1;
        }
        T = merge(makeNode(flatLength, 0), A);

        length = W;
        lastSlope = (int)cost;

        output += to_string(evaluate(T, base, lastSlope, length, w[j + 1]));
        output.push_back('\n');
    }

    cout << output;
    return 0;
}