题目 › fcs-160

FrontierCS #160

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

查看原题跳到结果

题面

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

Story -------- AtCoder's CEO Takahashi prepares for Halloween tomorrow. At AtCoder's Halloween party, Takahashi will dress up in disguise and receive a piece of candy from 100 employees in turn by saying, "trick or treat!" He prepares a square box that can contain $10\times 10$ pieces of candy in a grid pattern, and each employee puts a piece of candy in an empty space so that no candies overlap. There are $3$ types of candies: strawberry, watermelon, and pumpkin flavors. He knows which flavor of candy each employee will put in by preliminary survey, but he doesn't know where each employee will put it. Since he is a clean freak, he will move the pieces of candy by tilting the box forward, backward, left, or right, just once for each piece of candy received, and eventually wants to make sure that the same type of candy is clustered together as much as possible. Please help him by writing a program to determine the direction to tilt. Problem Statement -------- There is a box that can contain $10\times 10$ pieces of candy in a grid pattern. The box is initially empty, and $100$ pieces of candy will be placed in order. There are $3$ flavors of candy, and we know in advance the flavor $f_t (1\leq f_t\leq 3)$ of the $t$-th candy. On the other hand, we do not know in advance to which cell each candy will be placed, and it will be chosen uniformly at random among the empty cells. You cannot change the order in which the pieces of candy are received. Each time you receive one piece of candy, you must tilt the box forward, backward, left, or right exactly once. When you tilt the box, each piece of candy moves in that direction simultaneously until it reaches the edge or hits another candy. For example, tilting the box forward packs the candies toward the front of each column while preserving their order within that column. 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. We define the connectivity of pieces of candy as follows and consider the connected components. > Two pieces of candy are connected if and only if they are of the same flavor and can reach each other through only pieces of candy of the same flavor in the four directions (front, back, left, right). Let $n_1,\cdots,n_k$ be the list of sizes of connected components in the final state after receiving 100 pieces of candy, and let $d_i$ be the total number of pieces of candy of flavor $i$. The raw task reward V is \\[\mathrm{round}\left(10^6\times\frac{\sum_{i=1}^k n_i^2}{\sum_{i=1}^3 d_i^2}\right)\\] Your task is to write a program to determine the tilting directions so that you can get as high a score as possible. Input and Output -------- First, the flavor of each piece of candy is given from Standard Input in the following format. ~~~ $f_1$ $f_2$ $\cdots$ $f_{100}$ ~~~ Each $f_t$ is an integer value between $1$ and $3$, representing the flavor of the $t$-th piece of candy. After reading the above information, repeat the following process $100$ times. In the $t$-th process ($1\leq t\leq 100$), a single integer $p_t$ between $1$ and $101-t$ is given from Standard Input. Number the empty cells from $1$ to $101-t$ by scanning rows from front to back and, within each row, from left to right, skipping occupied cells. Then the $t$-th piece of candy is placed in the $p_t$-th empty cell. After reading $p_t$, by representing forward, backward, left, and right by `F`, `B`, `L`, and `R`, respectively, output a single character to Standard Output to indicate which direction to tilt the box. <font color="red">**The output must be followed by a new line, and you have to flush Standard Output.**</font> Otherwise, the submission might be judged as <span class='label label-warning' data-toggle='tooltip' data-placement='top' title="Time Limit Exceeded">TLE</span>. <font color="red">**Note that $p_{t+1}$ will not be given until you output the $t$-th direction.**</font> Since nothing happens at the 100th tilt, you may skip the output. #### Example <table class="table table-bordered"> <thead> <tr> <th>$t$</th> <th>Input</th> <th>Output</th> </tr> </thead> <tbody> <tr> <td>Prior information</td> <td><pre>2 2 1 3 1 2 1 2 1 $\cdots$ 3</pre></td> <td></td> </tr> <tr> <td>1</td> <td><pre>3</pre></td> <td><pre>R</pre></td> </tr> <tr> <td>2</td> <td><pre>98</pre></td> <td><pre>B</pre></td> </tr> <tr> <td>$\vdots$</td> <td></td> <td></td> </tr> <tr> <td>100</td> <td><pre>1</pre></td> <td><pre>L</pre></td> </tr> </tbody> </table> <a href="https://img.atcoder.jp/ahc015/b639c75d.html?lang=en&seed=0&output=R%0D%0AB%0D%0AB%0D%0AR%0D%0AF%0D%0AR%0D%0AF%0D%0AR%0D%0AR%0D%0AF%0D%0AB%0D%0AL%0D%0AB%0D%0AL%0D%0AF%0D%0AF%0D%0AB%0D%0AF%0D%0AB%0D%0AL%0D%0AL%0D%0AL%0D%0AL%0D%0AB%0D%0AF%0D%0AF%0D%0AR%0D%0AR%0D%0AF%0D%0AL%0D%0AL%0D%0AB%0D%0AL%0D%0AL%0D%0AL%0D%0AB%0D%0AL%0D%0AR%0D%0AF%0D%0AL%0D%0AB%0D%0AL%0D%0AF%0D%0AF%0D%0AF%0D%0AL%0D%0AL%0D%0AR%0D%0AB%0D%0AB%0D%0AF%0D%0AL%0D%0AF%0D%0AR%0D%0AB%0D%0AL%0D%0AF%0D%0AF%0D%0AR%0D%0AL%0D%0AR%0D%0AL%0D%0AR%0D%0AR%0D%0AB%0D%0AR%0D%0AB%0D%0AR%0D%0AR%0D%0AF%0D%0AB%0D%0AF%0D%0AR%0D%0AR%0D%0AF%0D%0AB%0D%0AF%0D%0AB%0D%0AF%0D%0AR%0D%0AB%0D%0AF%0D%0AF%0D%0AF%0D%0AB%0D%0AB%0D%0AL%0D%0AL%0D%0AR%0D%0AF%0D%0AB%0D%0AL%0D%0AB%0D%0AF%0D%0AB%0D%0AR%0D%0AF%0D%0AF%0D%0AL%0D%0AL%0D%0A">Show example</a> Input Generation -------- Let $\mathrm{rand}(L,U)$ be a function that generates a uniform random integer between $L$ and $U$, inclusive. Each $f_t$ is generated by $\mathrm{rand}(1,3)$. Each $p_t$ is generated by $\mathrm{rand}(1,101-t)$. Tools (Input generator and visualizer) -------- - <a href="https://img.atcoder.jp/ahc015/b639c75d.html?lang=en">Web version</a>: This is more powerful than the local version providing animations and manual play. - <a href="https://img.atcoder.jp/ahc015/b639c75d.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/ahc015/b639c75d_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. ## 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 maximizing quantity or quality and a for its physical 60-point reference. The improvement factor is r=x/a, 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. If R<=B, set U=1000000, L=100*max(0,(V-B)/(U-B)), and T=B+0.6*(U-B). Otherwise set L=60*max(0,(V-B)/(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. Let X be the integer sum of squared same-color connected-component sizes, and D the sum of squared total populations of each color. Its physical reference is a=A=T*D/1e6. When L>60, take x=X, a=A, and r=x/a; award min(100,60+ln(r)). The checker reports points/100.

结果

Kimi K3.1 ProKimi

max

4.1

平均得分 ↑

  • 4.1采样 1 · 部分分
  • 23.9k平均输出 token(含思考) ↓
  • 29.8输出速度 tokens/s ↑
  • 805 秒延迟 ↓

Kimi K3.1 Pro(high)Kimi high

high

12.9

平均得分 ↑

  • 12.9采样 1 · 部分分
  • 12.8k平均输出 token(含思考) ↓
  • 26.4输出速度 tokens/s ↑
  • 488 秒延迟 ↓

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

2.6

平均得分 ↑

  • 2.6采样 1 · 部分分
  • 24.4k平均输出 token(含思考) ↓
  • 174.1输出速度 tokens/s ↑
  • 141 秒延迟 ↓

MiniMax M3.1 FlashMiniMax

auto(max)

0.0

平均得分 ↑

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

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

代码

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

using Board = array<uint8_t, 100>;

static const char DIR_CHAR[4] = {'F', 'B', 'L', 'R'};

Board tiltBoard(const Board &b, int dir) {
    Board out{};
    if (dir == 0) {
        for (int c = 0; c < 10; ++c) {
            int w = 0;
            for (int r = 0; r < 10; ++r) {
                uint8_t v = b[r * 10 + c];
                if (v) out[w++ * 10 + c] = v;
            }
        }
    } else if (dir == 1) {
        for (int c = 0; c < 10; ++c) {
            int w = 9;
            for (int r = 9; r >= 0; --r) {
                uint8_t v = b[r * 10 + c];
                if (v) out[w-- * 10 + c] = v;
            }
        }
    } else if (dir == 2) {
        for (int r = 0; r < 10; ++r) {
            int w = 0;
            for (int c = 0; c < 10; ++c) {
                uint8_t v = b[r * 10 + c];
                if (v) out[r * 10 + w++] = v;
            }
        }
    } else {
        for (int r = 0; r < 10; ++r) {
            int w = 9;
            for (int c = 9; c >= 0; --c) {
                uint8_t v = b[r * 10 + c];
                if (v) out[r * 10 + w--] = v;
            }
        }
    }
    return out;
}

void putCandy(Board &b, int flavor, int rank) {
    for (int i = 0; i < 100; ++i) {
        if (b[i] == 0) {
            if (rank == 0) {
                b[i] = static_cast<uint8_t>(flavor);
                return;
            }
            --rank;
        }
    }
}

int componentScore(const Board &b) {
    array<uint8_t, 100> used{};
    int st[100];
    int result = 0;

    for (int s = 0; s < 100; ++s) {
        if (b[s] == 0 || used[s]) continue;
        uint8_t color = b[s];
        int top = 0;
        int sz = 0;
        st[top++] = s;
        used[s] = 1;

        while (top) {
            int x = st[--top];
            ++sz;
            int r = x / 10;
            int c = x % 10;

            if (r > 0) {
                int y = x - 10;
                if (!used[y] && b[y] == color) {
                    used[y] = 1;
                    st[top++] = y;
                }
            }
            if (r < 9) {
                int y = x + 10;
                if (!used[y] && b[y] == color) {
                    used[y] = 1;
                    st[top++] = y;
                }
            }
            if (c > 0) {
                int y = x - 1;
                if (!used[y] && b[y] == color) {
                    used[y] = 1;
                    st[top++] = y;
                }
            }
            if (c < 9) {
                int y = x + 1;
                if (!used[y] && b[y] == color) {
                    used[y] = 1;
                    st[top++] = y;
                }
            }
        }
        result += sz * sz;
    }
    return result;
}

int heuristic(const Board &b) {
    int score = componentScore(b);
    int proximity = 0;

    for (int r = 0; r < 10; ++r) {
        for (int c = 0; c < 10; ++c) {
            uint8_t v = b[r * 10 + c];
            if (!v) continue;

            if (r < 9) {
                if (c > 0 && b[(r + 1) * 10 + c - 1] == v) ++proximity;
                if (c < 9 && b[(r + 1) * 10 + c + 1] == v) ++proximity;
            }
            if (r < 8 && b[(r + 2) * 10 + c] == v) ++proximity;
            if (c < 8 && b[r * 10 + c + 2] == v) ++proximity;
        }
    }
    return score * 16 + proximity;
}

uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15ULL;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
    x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
    return x ^ (x >> 31);
}

struct RNG {
    uint64_t s;
    explicit RNG(uint64_t seed) : s(seed ? seed : 0x123456789abcdefULL) {}

    uint64_t next() {
        uint64_t x = s;
        x ^= x >> 12;
        x ^= x << 25;
        x ^= x >> 27;
        s = x;
        return x * 2685821657736338717ULL;
    }

    int integer(int n) {
        return static_cast<int>(next() % static_cast<uint64_t>(n));
    }
};

double exactValue(const Board &b, int turn, const array<int, 100> &flavors) {
    if (turn >= 100) return componentScore(b);

    int emptyCount = 100 - turn;
    double total = 0.0;

    for (int rank = 0; rank < emptyCount; ++rank) {
        Board placed = b;
        putCandy(placed, flavors[turn], rank);

        if (turn == 99) {
            total += componentScore(placed);
        } else {
            double best = -1.0;
            for (int d = 0; d < 4; ++d) {
                Board nb = tiltBoard(placed, d);
                best = max(best, exactValue(nb, turn + 1, flavors));
            }
            total += best;
        }
    }
    return total / emptyCount;
}

double greedyRollout(const Board &start, int nextTurn,
                     const array<int, 100> &flavors, uint64_t seed) {
    if (100 - nextTurn <= 2) return exactValue(start, nextTurn, flavors);

    RNG placeRng(splitmix64(seed));
    RNG policyRng(splitmix64(seed ^ 0xd1b54a32d192ed03ULL));
    Board b = start;

    for (int turn = nextTurn; turn <= 97; ++turn) {
        int rank = placeRng.integer(100 - turn);
        Board placed = b;
        putCandy(placed, flavors[turn], rank);

        if (turn == 97) {
            double best = -1.0;
            for (int d = 0; d < 4; ++d) {
                Board nb = tiltBoard(placed, d);
                best = max(best, exactValue(nb, 98, flavors));
            }
            return best;
        }

        int bestValue = -1;
        int ties = 0;
        Board bestBoard{};

        for (int d = 0; d < 4; ++d) {
            Board nb = tiltBoard(placed, d);
            int value = heuristic(nb);

            if (value > bestValue) {
                bestValue = value;
                bestBoard = nb;
                ties = 1;
            } else if (value == bestValue) {
                ++ties;
                if (policyRng.integer(ties) == 0) bestBoard = nb;
            }
        }
        b = bestBoard;
    }

    return exactValue(b, 98, flavors);
}

double beamRollout(const Board &start, int nextTurn,
                   const array<int, 100> &flavors, uint64_t seed) {
    struct Node {
        Board b;
        int value;
    };

    int horizon = 100 - nextTurn;
    int width = (horizon <= 5 ? 8 : 4);

    RNG rng(splitmix64(seed));
    array<int, 100> ranks{};
    for (int turn = nextTurn; turn <= 97; ++turn) {
        ranks[turn] = rng.integer(100 - turn);
    }

    vector<Node> current;
    current.push_back({start, 0});

    for (int turn = nextTurn; turn <= 97; ++turn) {
        if (turn == 97) {
            double best = -1.0;
            for (const Node &node : current) {
                Board placed = node.b;
                putCandy(placed, flavors[turn], ranks[turn]);
                for (int d = 0; d < 4; ++d) {
                    Board nb = tiltBoard(placed, d);
                    best = max(best, exactValue(nb, 98, flavors));
                }
            }
            return best;
        }

        vector<Node> next;
        next.reserve(current.size() * 4);

        for (const Node &node : current) {
            Board placed = node.b;
            putCandy(placed, flavors[turn], ranks[turn]);

            for (int d = 0; d < 4; ++d) {
                Board nb = tiltBoard(placed, d);
                next.push_back({nb, heuristic(nb)});
            }
        }

        sort(next.begin(), next.end(), [](const Node &a, const Node &b) {
            return a.value > b.value;
        });
        if (static_cast<int>(next.size()) > width) next.resize(width);
        current = move(next);
    }

    return exactValue(current.front().b, 98, flavors);
}

double rollout(const Board &start, int nextTurn,
               const array<int, 100> &flavors, uint64_t seed) {
    int horizon = 100 - nextTurn;
    if (horizon <= 4) return exactValue(start, nextTurn, flavors);
    if (horizon <= 6) return beamRollout(start, nextTurn, flavors, seed);
    return greedyRollout(start, nextTurn, flavors, seed);
}

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

    array<int, 100> flavors{};
    for (int i = 0; i < 100; ++i) {
        if (!(cin >> flavors[i])) return 0;
    }

    const auto startTime = chrono::steady_clock::now();
    const double TIME_BUDGET = 1.80;

    Board board{};

    for (int turn = 0; turn < 100; ++turn) {
        int p;
        if (!(cin >> p)) return 0;
        putCandy(board, flavors[turn], p - 1);

        if (turn == 99) return 0;

        Board candidates[4];
        int candidateHeuristic[4];

        for (int d = 0; d < 4; ++d) {
            candidates[d] = tiltBoard(board, d);
            candidateHeuristic[d] = heuristic(candidates[d]);
        }

        int nextTurn = turn + 1;
        int horizon = 100 - nextTurn;
        double values[4] = {0, 0, 0, 0};

        if (horizon <= 4) {
            for (int d = 0; d < 4; ++d) {
                values[d] = exactValue(candidates[d], nextTurn, flavors);
            }
        } else {
            double sums[4] = {0, 0, 0, 0};
            int iterations = 0;

            double elapsed = chrono::duration<double>(
                chrono::steady_clock::now() - startTime).count();
            double remaining = TIME_BUDGET - elapsed;
            int decisionsLeft = 99 - turn;

            if (remaining > 0.01) {
                double localEnd = elapsed + remaining / decisionsLeft;
                uint64_t baseSeed =
                    0x6a09e667f3bcc909ULL ^
                    (static_cast<uint64_t>(turn + 1) *
                     0x9e3779b97f4a7c15ULL);

                while (true) {
                    if (iterations > 0) {
                        double now = chrono::duration<double>(
                            chrono::steady_clock::now() - startTime).count();
                        if (now >= localEnd) break;
                    }

                    uint64_t seed = splitmix64(
                        baseSeed +
                        static_cast<uint64_t>(iterations + 1) *
                            0xbf58476d1ce4e5b9ULL);

                    for (int d = 0; d < 4; ++d) {
                        sums[d] += rollout(
                            candidates[d], nextTurn, flavors, seed);
                    }
                    ++iterations;
                }
            }

            if (iterations > 0) {
                for (int d = 0; d < 4; ++d) {
                    values[d] = sums[d] / iterations;
                }
            } else {
                for (int d = 0; d < 4; ++d) {
                    values[d] = candidateHeuristic[d];
                }
            }
        }

        int bestDir = 0;
        for (int d = 1; d < 4; ++d) {
            if (values[d] > values[bestDir] + 1e-9 ||
                (abs(values[d] - values[bestDir]) <= 1e-9 &&
                 candidateHeuristic[d] > candidateHeuristic[bestDir])) {
                bestDir = d;
            }
        }

        board = candidates[bestDir];
        cout << DIR_CHAR[bestDir] << '\n' << flush;
    }

    return 0;
}