题目 › fcs-166

FrontierCS #166

FrontierCS 算法赛道 v1.1 · 2026-05 · L5 开放式 · C++17 · 时限 10 秒 · 0–100 连续分

查看原题跳到结果

题面

来源:FrontierCS 官方 statement.txt,与模型收到的题面相同。

Story -------- AtCoder frequently hosts onsite contests and has decided to build its own contest venue. The planned construction site is in a mountain area, and the ground must first be leveled. Leveling will involve using a dump truck, which incurs costs for loading, unloading, and transporting soil. Determine the method to level the ground with as little cost as possible. Problem Statement -------- There is an $N \times N$ grid of land. Let $(0,0)$ be the coordinates of the top-left square and $(i,j)$ be the coordinates of the square located $i$ squares down and $j$ squares to the right from there. The height $h_{i,j}$ of each square $(i,j)$ is given as input. The total height of all squares is exactly $0$. Initially, the dump truck is at the square $(0,0)$ with an empty load. In each turn, you can perform one of the following three operations: - Specify an integer $d$ satisfying $0<d\leq 10^6$ to load $d$ units of soil from the current square onto the dump truck. This operation decreases the height of the current square by $d$ and increases the amount of soil loaded onto the dump truck by $d$. The height can become negative. This operation incurs a cost of $d$. - Specify an integer $d$ satisfying $0<d\leq 10^6$ to unload $d$ units of soil from the dump truck to the current square. This operation increases the height of the current square by $d$ and decreases the amount of soil loaded onto the dump truck by $d$. The value of $d$ must not exceed the amount of soil currently loaded onto the dump truck. This operation incurs a cost of $d$. - Move the dump truck to an adjacent square (up, down, left, or right). The dump truck cannot move outside the $N \times N$ grid. If the dump truck has $d$ units of soil loaded, this operation incurs a cost of $100+d$. You can perform a maximum of $100000$ turns of operation. Your task is to determine the sequence of operations that minimizes the cost to make the height of every square $0$. Raw task objective -------- The formulas in this section define the raw reward V, not final benchmark points. The Scoring section specifies the per-file award and its aggregation. Let $\mathrm{cost}$ be the total cost of the output operation sequence. Let $\mathrm{base}$ be the sum of $|h_{i,j}|$ for all $(i,j)$. Let $h_{i,j}'$ be the final height of square $(i,j)$. Define $\mathrm{diff}$ as the sum of $100|h_{i,j}'|+10000$ for all $(i,j)$ such that $h_{i,j}' \neq 0$. The raw task reward V is defined as follows. \\[ \mathrm{round}\left(10^9\times \frac{\mathrm{base}}{\mathrm{cost}+\mathrm{diff}}\right) \\] Input -------- Input is given from Standard Input in the following format: ~~~ $N$ $h_{0,0}$ $\cdots$ $h_{0,N-1}$ $\vdots$ $h_{N-1,0}$ $\cdots$ $h_{N-1,N-1}$ ~~~ In all test cases, $N$ is fixed at $20$. The initial height $h_{i,j}$ of square $(i,j)$ is an integer that satisfies $-100 \leq h_{i,j} \leq 100$, and the total sum of all heights is guaranteed to be $0$. Output -------- Let $T$ be the number of operation turns. The operation in the $t$-th turn is represented by the string $s_t$ as follows: - The operation to load $d$ units of soil from the current square onto the dump truck: `+d` - The operation to unload $d$ units of soil from the dump truck to the current square: `-d` - The operation to move the dump truck to an adjacent square: `U`, `D`, `L`, and `R` for up, down, left, and right, respectively. Then, output to Standard Output in the following format: ~~~ $s_0$ $\vdots$ $s_{T-1}$ ~~~ <a href="https://img.atcoder.jp/ahc034/vImT4eac.html?lang=en&seed=0&output=sample">Show example</a> Input Generation -------- <details> <p> You are not required to understand this. We recommend changing the seed value in the web visualizer to observe what kind of inputs are generated. </p> <p> Let $\mathrm{rand}(L,U)$ be a function that generates a uniform random integer between $L$ and $U$, inclusive. Let $\mathrm{noise}(y,x,\mathrm{seed})$ be a function that generates two-dimensional <a href="https://en.wikipedia.org/wiki/Perlin_noise">Perlin noise</a> scaled to the range between $-1$ and $1$ based on a random seed value $\mathrm{seed}$. </p> <p> First, generate a seed value for Perlin noise generation as $\mathrm{seed}=\mathrm{rand}(0,2^{32}-1)$. </p> <p> Next, for each $(i,j)$, generate $h_{i,j}=\mathrm{round}(\mathrm{noise}(i/10,j/10,\mathrm{seed})\times 50)$. If $h_{i,j}=0$ for all $(i,j)$, then regenerate $\mathrm{seed}$. </p> <p> Let $S$ be the sum of $h_{i,j}$. To make $S=0$, perform the following transformation. </p> <p> Shuffle all coordinates $(0,0),(0,1),\cdots,(N-1,N-1)$ in a uniformly random order, and let the $k$-th coordinate be $(i_k,j_k)$. If $S>0$, then for each $k=0,1,\cdots,S-1$, decrease $h_{i_{k\% N^2},j_{k\% N^2}}$ by $1$. If $S<0$, then for each $k=0,1,\cdots,-S-1$, increase $h_{i_{k\% N^2},j_{k\% N^2}}$ by $1$. </p> </details> Tools (Input generator and visualizer) -------- - <a href="https://img.atcoder.jp/ahc034/vImT4eac.html?lang=en">Web version</a>: This is more powerful than the local version providing animations. - <a href="https://img.atcoder.jp/ahc034/vImT4eac.zip">Local version</a>: You need a compilation environment of <a href="https://www.rust-lang.org/">Rust language</a>. - <a href="https://img.atcoder.jp/ahc034/vImT4eac_windows.zip">Pre-compiled binary for Windows</a>: If you are not familiar with the Rust language environment, please use this instead. Please be aware that sharing visualization results or discussing solutions/ideas during the contest is prohibited. {sample example} ## Scoring All feasibility and interaction limits apply before scoring; invalid output receives zero. Points are averaged over test files. For the logarithmic branch, write x for the original minimizing quantity and a for its physical 60-point reference. The improvement factor is r=a/x, and the upper-band award is P=min(100,60+ln(r)). Here ln is natural: r=1 corresponds to 60 points, and on the uncapped logarithmic upper band, r=exp(P-60). Each additional point multiplies the physical improvement factor by e (divides a minimizing objective by e or multiplies a maximizing objective by e, as applicable). The lower-band, zero-objective, and completion rules below still apply. Let V be the raw task reward defined in the problem, and let B and R be the stored baseline and reference rewards. They are calibration values, not assertions of optimality. The stored reward reference R is distinct from the physical reference a used in the logarithm; a is defined by the inverse or stage-specific formula below. Set L=60*max(0,(V-B)/(R-B)), with R>B, and T=R. For nonzero minimizing objectives, and all maximizing objectives, keep L exactly whenever L<=60. The following describes the upper band and any valid zero-cost optimum. Here E is the operation cost plus sum(10000+100*abs(h)) over cells whose final height h is nonzero; K is the sum of absolute initial cell heights. For E>0, the raw reward is V=round(1e9*K/E); for E=0 the raw reward is set to zero, but the valid zero-cost award below takes priority. Use physical reference a=A=1e9*K/R. A valid E=0 earns 100 before testing L. For E>0 and L>60, take x=E, a=A, and r=a/x; award min(100,60+ln(r)). A nonpositive anchor has no defined positive-cost logarithmic comparison, so retain the existing nonzero-cost branch. The stored reference reward is rounded. Its physical anchor is chosen by analytically inverting the unrounded reward at the center R (or T for a special branch). The lower-band condition keeps every result with the original 60-point reward at exactly 60. A rounded reward is not evidence of zero physical error or cost. The checker reports points/100.

结果

Kimi K3.1 ProKimi

max

0.0

平均得分 ↑

  • 0.0采样 1 · 运行错误
  • 30.0k平均输出 token(含思考) ↓
  • 30.5输出速度 tokens/s ↑
  • 986 秒延迟 ↓

Kimi K3.1 Pro(high)Kimi high

high

27.5

平均得分 ↑

  • 27.5采样 1 · 部分分
  • 14.0k平均输出 token(含思考) ↓
  • 26.2输出速度 tokens/s ↑
  • 537 秒延迟 ↓

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

2.1

平均得分 ↑

  • 2.1采样 1 · 部分分
  • 35.3k平均输出 token(含思考) ↓
  • 179.6输出速度 tokens/s ↑
  • 197 秒延迟 ↓

MiniMax M3.1 FlashMiniMax

auto(max)

0.0

平均得分 ↑

  • 0.0采样 1 · 编译错误
  • 69.5k平均输出 token(含思考) ↓
  • 84.4输出速度 tokens/s ↑
  • 825 秒延迟 ↓

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

代码

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

using ll = long long;
const ll INF = (1LL << 60);

struct Plan {
    vector<string> ops;
    ll cost = INF;
    bool valid = false;
};

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

    int N;
    cin >> N;
    const int M = N * N;
    vector<int> h(M);
    bool any = false;
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            cin >> h[i * N + j];
            if (h[i * N + j] != 0) any = true;
        }
    }
    if (!any) return 0;

    auto startTime = chrono::steady_clock::now();

    vector<vector<int>> dist(M, vector<int>(M));
    for (int a = 0; a < M; ++a) {
        for (int b = 0; b < M; ++b) {
            dist[a][b] = abs(a / N - b / N) + abs(a % N - b % N);
        }
    }

    auto adjacent = [&](int a, int b) {
        return dist[a][b] == 1;
    };

    struct Eval {
        ll cost = INF;
        int start = 0;
        int steps = 0;
    };

    auto evaluate = [&](const vector<int>& c) {
        Eval ev;
        vector<ll> pref(M + 1, 0);
        vector<int> prevBefore(M, -1);
        ll sumAfter = 0;
        ll mn = 0;
        int prev = -1;
        int lastAny = -1;

        for (int k = 0; k < M; ++k) {
            prevBefore[k] = prev;
            pref[k + 1] = pref[k] + h[c[k]];
            sumAfter += pref[k + 1];
            mn = min(mn, pref[k + 1]);
            if (h[c[k]] != 0) {
                prev = k;
                lastAny = k;
            }
        }

        ll transport = sumAfter - (ll)M * mn;
        int bestMetric = INT_MAX;

        for (int s = 0; s < M; ++s) {
            if (pref[s] != mn) continue;
            int p = prevBefore[s];
            if (p < 0) p = lastAny;
            int tail = (p - s + M) % M;
            int metric = dist[0][c[s]] + tail;
            if (metric < bestMetric) {
                bestMetric = metric;
                ev.start = s;
                ev.steps = tail;
            }
        }

        ev.cost = transport + 100LL * bestMetric;
        return ev;
    };

    vector<pair<int, int>> baseCoords;
    for (int j = 0; j < N; ++j) baseCoords.push_back({0, j});
    for (int i = 1; i < N; ++i) {
        if (i & 1) {
            for (int j = N - 1; j >= 1; --j) baseCoords.push_back({i, j});
        } else {
            for (int j = 1; j < N; ++j) baseCoords.push_back({i, j});
        }
    }
    for (int i = N - 1; i >= 1; --i) baseCoords.push_back({i, 0});

    auto normalizedReverse = [&](const vector<int>& c) {
        vector<int> r(M);
        r[0] = c[0];
        for (int k = 1; k < M; ++k) r[k] = c[M - k];
        return r;
    };

    vector<int> bestCycle;
    ll bestCost = INF;

    for (int t = 0; t < 8; ++t) {
        vector<int> c;
        c.reserve(M);
        for (auto [rr, cc] : baseCoords) {
            int r = rr, col = cc;
            if (t & 1) swap(r, col);
            if (t & 2) r = N - 1 - r;
            if (t & 4) col = N - 1 - col;
            c.push_back(r * N + col);
        }

        int z = 0;
        while (c[z] != 0) ++z;
        rotate(c.begin(), c.begin() + z, c.begin());

        for (int rev = 0; rev < 2; ++rev) {
            Eval ev = evaluate(c);
            if (ev.cost < bestCost) {
                bestCost = ev.cost;
                bestCycle = c;
            }
            if (!rev) c = normalizedReverse(c);
        }
    }

    mt19937 rng(712367821);
    vector<int> current = bestCycle;
    ll currentCost = bestCost;
    vector<int> posInCycle(M);
    auto rebuildPos = [&]() {
        for (int k = 0; k < M; ++k) posInCycle[current[k]] = k;
    };
    rebuildPos();

    uniform_real_distribution<double> prob(0.0, 1.0);
    double progress = 0.0;
    bool intensified = false;
    ll iteration = 0;
    const double searchSeconds = 1.65;

    while (true) {
        ++iteration;
        if ((iteration & 1023) == 0) {
            double elapsed = chrono::duration<double>(
                chrono::steady_clock::now() - startTime).count();
            if (elapsed >= searchSeconds) break;
            progress = elapsed / searchSeconds;
            if (progress > 0.72 && !intensified) {
                current = bestCycle;
                currentCost = bestCost;
                rebuildPos();
                intensified = true;
            }
        }

        if ((iteration & 4095) == 0) {
            vector<int> rev = normalizedReverse(current);
            Eval ev = evaluate(rev);
            if (ev.cost < currentCost) {
                current = rev;
                currentCost = ev.cost;
                rebuildPos();
                if (currentCost < bestCost) {
                    bestCost = currentCost;
                    bestCycle = current;
                }
            }
            continue;
        }

        int i = (int)(rng() % M);
        int A = current[i];
        int B = current[(i + 1) % M];
        int ar = A / N, ac = A % N;
        int br = B / N, bc = B % N;
        int dr = br - ar, dc = bc - ac;
        int side = (rng() & 1) ? 1 : -1;
        int pr = -dc * side;
        int pc = dr * side;

        int cr = ar + pr, cc = ac + pc;
        int dr2 = br + pr, dc2 = bc + pc;
        if (cr < 0 || cr >= N || cc < 0 || cc >= N ||
            dr2 < 0 || dr2 >= N || dc2 < 0 || dc2 >= N) {
            continue;
        }

        int C = cr * N + cc;
        int D = dr2 * N + dc2;
        int j = posInCycle[C];
        if (current[(j + 1) % M] != D) continue;

        int a = min(i, j), b = max(i, j);
        if (a == b || b == a + 1 || (a == 0 && b == M - 1)) continue;
        if (!adjacent(current[a], current[b]) ||
            !adjacent(current[a + 1], current[(b + 1) % M])) {
            continue;
        }

        reverse(current.begin() + a + 1, current.begin() + b + 1);
        for (int k = a + 1; k <= b; ++k) posInCycle[current[k]] = k;

        Eval ev = evaluate(current);
        ll delta = ev.cost - currentCost;
        double temp = 10000.0 * pow(0.0001, progress) + 0.01;
        bool accept = (delta <= 0 || prob(rng) < exp(-(double)delta / temp));

        if (accept) {
            currentCost = ev.cost;
            if (currentCost < bestCost) {
                bestCost = currentCost;
                bestCycle = current;
            }
        } else {
            reverse(current.begin() + a + 1, current.begin() + b + 1);
            for (int k = a + 1; k <= b; ++k) posInCycle[current[k]] = k;
        }
    }

    auto makeCyclePlan = [&]() {
        Plan plan;
        plan.ops.clear();
        plan.cost = 0;
        plan.valid = true;

        Eval ev = evaluate(bestCycle);
        ll load = 0;
        int cur = 0;

        auto addMove = [&](int to) {
            int r = cur / N, c = cur % N;
            int nr = to / N, nc = to % N;
            string op;
            if (nr == r + 1) op = "D";
            else if (nr == r - 1) op = "U";
            else if (nc == c + 1) op = "R";
            else op = "L";
            plan.ops.push_back(op);
            plan.cost += 100 + load;
            cur = to;
        };

        int target = bestCycle[ev.start];
        int tr = target / N, tc = target % N;
        while (cur % N < tc) addMove(cur + 1);
        while (cur % N > tc) addMove(cur - 1);
        while (cur / N < tr) addMove(cur + N);
        while (cur / N > tr) addMove(cur - N);

        for (int rel = 0; rel <= ev.steps; ++rel) {
            int idx = (ev.start + rel) % M;
            int cell = bestCycle[idx];
            if (cur != cell) {
                plan.valid = false;
                return plan;
            }

            if (h[cell] > 0) {
                int d = h[cell];
                plan.ops.push_back("+" + to_string(d));
                plan.cost += d;
                load += d;
            } else if (h[cell] < 0) {
                int d = -h[cell];
                if (load < d) {
                    plan.valid = false;
                    return plan;
                }
                plan.ops.push_back("-" + to_string(d));
                plan.cost += d;
                load -= d;
            }

            if (rel < ev.steps) {
                int next = bestCycle[(idx + 1) % M];
                if (!adjacent(cur, next)) {
                    plan.valid = false;
                    return plan;
                }
                addMove(next);
            }
        }

        if (load != 0 || plan.ops.size() > 100000) plan.valid = false;
        return plan;
    };

    Plan answer = makeCyclePlan();

    auto greedyPlan = [&](int variant) {
        Plan plan;
        plan.cost = 0;
        plan.valid = true;
        vector<int> w = h;
        int remaining = 0;
        for (int x : w) if (x != 0) ++remaining;

        int cur = 0;
        ll load = 0;
        bool horizontalFirst = (variant & 1);
        int amountWeight = (variant >= 2) ? 15 : 0;

        auto addMove = [&](int to, const string& op) {
            plan.ops.push_back(op);
            plan.cost += 100 + load;
            cur = to;
        };

        while (remaining > 0) {
            if ((int)plan.ops.size() > 90000) {
                plan.valid = false;
                return plan;
            }

            if (load == 0 && w[cur] > 0) {
                int d = w[cur];
                plan.ops.push_back("+" + to_string(d));
                plan.cost += d;
                load += d;
                w[cur] = 0;
                --remaining;
                continue;
            }

            if (load > 0 && w[cur] < 0) {
                int d = (int)min<ll>(load, -w[cur]);
                plan.ops.push_back("-" + to_string(d));
                plan.cost += d;
                load -= d;
                w[cur] += d;
                if (w[cur] == 0) --remaining;
                continue;
            }

            int targetCell = -1;
            ll bestScore = INF;
            for (int cell = 0; cell < M; ++cell) {
                if (load == 0) {
                    if (w[cell] <= 0) continue;
                } else {
                    if (w[cell] >= 0) continue;
                }

                ll score = 100LL * dist[cur][cell];
                if (load == 0) {
                    score -= amountWeight * (ll)w[cell];
                } else if (variant >= 2 && -w[cell] < load) {
                    score += 40;
                }
                if (variant == 3) score += (ll)(rng() % 7);

                if (score < bestScore) {
                    bestScore = score;
                    targetCell = cell;
                }
            }

            if (targetCell < 0) {
                plan.valid = false;
                return plan;
            }

            int r = cur / N, c = cur % N;
            int tr = targetCell / N, tc = targetCell % N;
            bool canHorizontal = (c != tc);
            bool canVertical = (r != tr);
            bool goHorizontal = canHorizontal &&
                (!canVertical || horizontalFirst);

            if (goHorizontal) {
                if (tc > c) addMove(cur + 1, "R");
                else addMove(cur - 1, "L");
            } else {
                if (tr > r) addMove(cur + N, "D");
                else addMove(cur - N, "U");
            }
        }

        if (load != 0 || plan.ops.size() > 100000) plan.valid = false;
        return plan;
    };

    for (int variant = 0; variant < 4; ++variant) {
        Plan p = greedyPlan(variant);
        if (p.valid && (!answer.valid || p.cost < answer.cost)) {
            answer = move(p);
        }
    }

    for (const string& op : answer.ops) {
        cout << op << '\n';
    }
    return 0;
}