题目 › fcs-0

FrontierCS #0

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

查看原题跳到结果

题面

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

# Pack the Polyominoes (Reflections Allowed) ## Problem You are given a set of n polyominoes placed on a unit square grid. Each polyomino consists of between 1 and 10 unit cells, connected edge-to-edge (4-connectivity). Your task is to place all polyominoes—allowing **rotations, reflections, and translations**—into an axis-aligned rectangle of the grid with no overlaps and no cells outside the rectangle, minimizing the rectangle’s area. A placement specifies, for every polyomino, an integer translation, an optional reflection, and a rotation by a multiple of 90°, such that: - every cell lands on integer grid coordinates, - no two placed cells overlap, - all placed cells lie within a single axis-aligned rectangle of width W and height H. The objective is to minimize A = W × H. In case of ties on area, prefer smaller H, then smaller W. --- ## Input Format - Line 1: integer n (100 <= n <= 10^4) — number of polyominoes. - For each polyomino i = 1 … n: - One line: integer kᵢ (1 ≤ kᵢ ≤ 10) — number of cells. - Next kᵢ lines: two integers xᵢⱼ, yᵢⱼ — cell coordinates in the polyomino’s local frame. These coordinates define a 4-connected polyomino. Notes: - Coordinates may be negative. - Shapes may have internal holes. - Shapes are defined by structure; initial placement does not matter. --- ## Output Format - Line 1: two integers W and H — width and height of the chosen rectangle. - Then n lines — one per polyomino i, each containing: $$X_i\ Y_i\ R_i\ F_i$$ Where: - (Xᵢ, Yᵢ): integer translation - Rᵢ ∈ {0,1,2,3}: number of 90° clockwise rotations - Fᵢ ∈ {0,1}: reflection flag (**1 = reflect across the y-axis before rotation**, 0 = no reflection) All transformed cells must satisfy: 0 ≤ xᵢⱼ′ < W, 0 ≤ yᵢⱼ′ < H --- ## Rules 1. All cells occupy unit squares with integer coordinates. 2. **Allowed transforms: reflection (optional) → rotation (0°, 90°, 180°, 270°) → translation (in that order).** 3. Distinct polyomino cells must not overlap. 4. Every cell must lie inside [0, W) × [0, H). 5. Scoring: Minimize A = W × H. --- ## Constraints - 100 ≤ n ≤ 10000 - 1 ≤ kᵢ ≤ 10 --- ## Validation A solution is valid if: - All rules are satisfied. - The judge verifies non-overlap and bounding-box compliance. --- ## Scoring Let x be A=W*H, the rectangle area. C is the total number of occupied cells in the pieces. The physical reference (the 60-point level) is a=C/0.6. Define the lower-band continuation: L(x) = 100*C/x. For positive x and positive physical reference a, define the improvement factor r=a/x. The physical reference has r=1 and earns 60 points. For a valid output on this branch, the points P are: P = 100, if x=0; P = L(x), if L(x)<=60; P = min(100,60+ln(r)), otherwise. Here ln is the natural logarithm. In the upper branch before the 100-point cap, r=exp(P-60): each additional point requires an e-fold improvement in the physical objective (division by e). All feasibility, correctness, and hard resource-limit checks remain required; invalid output receives 0. Across test files, use the arithmetic mean of the file scores. ## Example This small example illustrates placement and scoring. Official inputs have n >= 100. Input ``` 3 1 0 0 3 0 0 1 0 0 1 4 0 0 1 0 0 1 1 1 ``` Output ``` 3 3 2 0 0 0 2 2 2 0 0 0 0 0 ``` This places the monomino at (2,0), rotates the triomino by 180° (R=2), and fits all shapes into a 3×3 rectangle without reflections. The pieces occupy C=8 cells and A=9, giving 60+ln((8/0.6)/9), approximately 60.393 points. --- ## Generation Let $S$ be the set of all pieces from size 1-10. Each piece is chosen uniformly at random with replacement from $S$. $n$ is chosen as $10^p$, where $p \sim U(2, 4)$.

结果

Kimi K3.1 ProKimi

max

60.3

平均得分 ↑

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

Kimi K3.1 Pro(high)Kimi high

high

60.4

平均得分 ↑

  • 60.4采样 1 · 部分分
  • 14.6k平均输出 token(含思考) ↓
  • 25.9输出速度 tokens/s ↑
  • 565 秒延迟 ↓

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

60.2

平均得分 ↑

  • 60.2采样 1 · 部分分
  • 31.7k平均输出 token(含思考) ↓
  • 200.4输出速度 tokens/s ↑
  • 159 秒延迟 ↓

MiniMax M3.1 FlashMiniMax

auto(max)

34.1

平均得分 ↑

  • 34.1采样 1 · 部分分
  • 71.4k平均输出 token(含思考) ↓
  • 147.4输出速度 tokens/s ↑
  • 486 秒延迟 ↓

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

代码

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

struct Cell {
    int x, y;
};

struct Orientation {
    int w = 0, h = 0, r = 0, f = 0;
    long long mx = 0, my = 0;
    vector<Cell> cells;
    vector<pair<int, int>> rowReq;
    array<uint16_t, 10> rowMask{};
};

struct Piece {
    int k = 0;
    int bboxArea = 0;
    int maxDim = 0;
    vector<Orientation> orientations;
};

struct Placement {
    int orient = 0;
    int x = 0, y = 0;
};

struct Result {
    int W = 0, H = 0;
    vector<Placement> placements;
};

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

static Result packStrip(const vector<Piece>& pieces, const vector<int>& order, int W) {
    const int n = (int)pieces.size();
    const int words = (W + 63) / 64;
    constexpr int MAX_WORDS = 8;

    vector<uint64_t> occupied;
    vector<int> freeCells;
    int H = 0;
    int firstNonFull = 0;

    Result result;
    result.placements.resize(n);

    const int dx[4] = {1, -1, 0, 0};
    const int dy[4] = {0, 0, 1, -1};

    for (int id : order) {
        const Piece& piece = pieces[id];
        bool placed = false;

        for (int y = firstNonFull; y <= H && !placed; ++y) {
            if (y < H && freeCells[y] == 0) continue;

            int bestOrient = -1;
            int bestX = INT_MAX;
            int bestTop = INT_MAX;
            int bestContact = -1;

            for (int oi = 0; oi < (int)piece.orientations.size(); ++oi) {
                const Orientation& o = piece.orientations[oi];
                if (o.w > W) continue;

                int top = max(H, y + o.h);
                if (bestOrient != -1 && top > bestTop) continue;

                bool possible = true;
                for (auto [ry, need] : o.rowReq) {
                    int row = y + ry;
                    if (row < H && freeCells[row] < need) {
                        possible = false;
                        break;
                    }
                }
                if (!possible) continue;

                int numX = W - o.w + 1;
                int usedWords = (numX + 63) / 64;
                uint64_t valid[MAX_WORDS];

                for (int i = 0; i < words; ++i) valid[i] = ~0ULL;
                for (int i = usedWords; i < words; ++i) valid[i] = 0;

                int rem = numX & 63;
                if (rem) valid[usedWords - 1] = (1ULL << rem) - 1;
                else valid[usedWords - 1] = ~0ULL;

                bool any = true;
                for (const Cell& c : o.cells) {
                    int row = y + c.y;
                    if (row >= H) continue;

                    const uint64_t* bits = &occupied[(size_t)row * words];
                    int sh = c.x;
                    any = false;

                    if (sh == 0) {
                        for (int i = 0; i < usedWords; ++i) {
                            valid[i] &= ~bits[i];
                            any |= (valid[i] != 0);
                        }
                    } else {
                        for (int i = 0; i < usedWords; ++i) {
                            uint64_t shifted = bits[i] >> sh;
                            if (i + 1 < words) {
                                shifted |= bits[i + 1] << (64 - sh);
                            }
                            valid[i] &= ~shifted;
                            any |= (valid[i] != 0);
                        }
                    }
                    if (!any) break;
                }
                if (!any) continue;

                for (int wi = 0; wi < usedWords; ++wi) {
                    uint64_t mask = valid[wi];
                    while (mask) {
                        int b = __builtin_ctzll(mask);
                        mask &= mask - 1;
                        int x = wi * 64 + b;
                        if (x >= numX) continue;

                        int contact = 0;
                        for (const Cell& c : o.cells) {
                            int ax = x + c.x;
                            int ay = y + c.y;

                            for (int d = 0; d < 4; ++d) {
                                int sx = c.x + dx[d];
                                int sy = c.y + dy[d];

                                if (sx >= 0 && sx < o.w && sy >= 0 && sy < o.h &&
                                    ((o.rowMask[sy] >> sx) & 1U)) {
                                    continue;
                                }

                                int nx = ax + dx[d];
                                int ny = ay + dy[d];

                                if (nx < 0 || nx >= W || ny < 0) {
                                    ++contact;
                                } else if (ny < H) {
                                    if ((occupied[(size_t)ny * words + (nx >> 6)] >> (nx & 63)) & 1ULL) {
                                        ++contact;
                                    }
                                }
                            }
                        }

                        bool better = false;
                        if (bestOrient == -1) better = true;
                        else if (top != bestTop) better = top < bestTop;
                        else if (contact != bestContact) better = contact > bestContact;
                        else if (x != bestX) better = x < bestX;

                        if (better) {
                            bestOrient = oi;
                            bestX = x;
                            bestTop = top;
                            bestContact = contact;
                        }
                    }
                }
            }

            if (bestOrient != -1) {
                const Orientation& o = piece.orientations[bestOrient];
                int newH = max(H, y + o.h);

                if (newH > H) {
                    occupied.resize((size_t)newH * words, 0ULL);
                    freeCells.resize(newH, W);
                    H = newH;
                }

                for (const Cell& c : o.cells) {
                    int ax = bestX + c.x;
                    int ay = y + c.y;
                    occupied[(size_t)ay * words + (ax >> 6)] |= 1ULL << (ax & 63);
                    --freeCells[ay];
                }

                result.placements[id] = {bestOrient, bestX, y};
                placed = true;

                while (firstNonFull < H && freeCells[firstNonFull] == 0) {
                    ++firstNonFull;
                }
            }
        }
    }

    int minX = W;
    int maxX = -1;
    for (int i = 0; i < n; ++i) {
        const Placement& p = result.placements[i];
        const Orientation& o = pieces[i].orientations[p.orient];
        for (const Cell& c : o.cells) {
            int x = p.x + c.x;
            minX = min(minX, x);
            maxX = max(maxX, x);
        }
    }

    for (Placement& p : result.placements) p.x -= minX;
    result.W = maxX - minX + 1;
    result.H = H;
    return result;
}

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

    int n;
    cin >> n;

    vector<Piece> pieces(n);
    int totalCells = 0;
    int minimumStripWidth = 1;

    for (int i = 0; i < n; ++i) {
        int k;
        cin >> k;
        pieces[i].k = k;
        totalCells += k;

        vector<pair<long long, long long>> inputCells(k);
        long long minX = LLONG_MAX, maxX = LLONG_MIN;
        long long minY = LLONG_MAX, maxY = LLONG_MIN;

        for (auto& [x, y] : inputCells) {
            cin >> x >> y;
            minX = min(minX, x);
            maxX = max(maxX, x);
            minY = min(minY, y);
            maxY = max(maxY, y);
        }

        int bw = int(maxX - minX + 1);
        int bh = int(maxY - minY + 1);
        pieces[i].bboxArea = bw * bh;
        pieces[i].maxDim = max(bw, bh);

        vector<vector<pair<int, int>>> seen;

        for (int f = 0; f <= 1; ++f) {
            for (int r = 0; r < 4; ++r) {
                vector<pair<long long, long long>> transformed;
                transformed.reserve(k);

                long long ox = LLONG_MAX, oy = LLONG_MAX;

                for (auto [x, y] : inputCells) {
                    long long a = f ? -x : x;
                    long long b = y;
                    long long qx, qy;

                    if (r == 0) {
                        qx = a; qy = b;
                    } else if (r == 1) {
                        qx = b; qy = -a;
                    } else if (r == 2) {
                        qx = -a; qy = -b;
                    } else {
                        qx = -b; qy = a;
                    }

                    transformed.push_back({qx, qy});
                    ox = min(ox, qx);
                    oy = min(oy, qy);
                }

                vector<pair<int, int>> key;
                key.reserve(k);
                for (auto [x, y] : transformed) {
                    key.push_back({int(x - ox), int(y - oy)});
                }
                sort(key.begin(), key.end());

                bool duplicate = false;
                for (const auto& old : seen) {
                    if (old == key) {
                        duplicate = true;
                        break;
                    }
                }
                if (duplicate) continue;
                seen.push_back(key);

                Orientation o;
                o.r = r;
                o.f = f;
                o.mx = ox;
                o.my = oy;

                for (auto [x, y] : key) {
                    o.cells.push_back({x, y});
                    o.w = max(o.w, x + 1);
                    o.h = max(o.h, y + 1);
                }

                sort(o.cells.begin(), o.cells.end(), [](const Cell& a, const Cell& b) {
                    if (a.y != b.y) return a.y < b.y;
                    return a.x < b.x;
                });

                int counts[10] = {};
                for (const Cell& c : o.cells) {
                    o.rowMask[c.y] |= uint16_t(1U << c.x);
                    ++counts[c.y];
                }
                for (int y = 0; y < o.h; ++y) {
                    if (counts[y]) o.rowReq.push_back({y, counts[y]});
                }

                pieces[i].orientations.push_back(std::move(o));
            }
        }

        int pieceMinWidth = INT_MAX;
        for (const Orientation& o : pieces[i].orientations) {
            pieceMinWidth = min(pieceMinWidth, o.w);
        }
        minimumStripWidth = max(minimumStripWidth, pieceMinWidth);
    }

    vector<vector<int>> orders(5, vector<int>(n));
    for (int t = 0; t < 5; ++t) {
        iota(orders[t].begin(), orders[t].end(), 0);
    }

    sort(orders[0].begin(), orders[0].end(), [&](int a, int b) {
        if (pieces[a].k != pieces[b].k) return pieces[a].k > pieces[b].k;
        if (pieces[a].bboxArea != pieces[b].bboxArea)
            return pieces[a].bboxArea > pieces[b].bboxArea;
        if (pieces[a].maxDim != pieces[b].maxDim)
            return pieces[a].maxDim > pieces[b].maxDim;
        return a < b;
    });

    sort(orders[1].begin(), orders[1].end(), [&](int a, int b) {
        if (pieces[a].maxDim != pieces[b].maxDim)
            return pieces[a].maxDim > pieces[b].maxDim;
        if (pieces[a].k != pieces[b].k) return pieces[a].k > pieces[b].k;
        if (pieces[a].bboxArea != pieces[b].bboxArea)
            return pieces[a].bboxArea > pieces[b].bboxArea;
        return a < b;
    });

    sort(orders[2].begin(), orders[2].end(), [&](int a, int b) {
        if (pieces[a].k != pieces[b].k) return pieces[a].k > pieces[b].k;
        uint64_t ha = mixHash((uint64_t)a + 12345);
        uint64_t hb = mixHash((uint64_t)b + 12345);
        if (ha != hb) return ha < hb;
        return a < b;
    });

    sort(orders[3].begin(), orders[3].end(), [&](int a, int b) {
        if (pieces[a].bboxArea != pieces[b].bboxArea)
            return pieces[a].bboxArea > pieces[b].bboxArea;
        if (pieces[a].k != pieces[b].k) return pieces[a].k > pieces[b].k;
        return a < b;
    });

    int base = max(minimumStripWidth, (int)llround(sqrt((double)totalCells)));
    base = min(base, 512);

    vector<int> widths;
    auto addWidth = [&](int w) {
        w = max(w, minimumStripWidth);
        w = min(w, 512);
        if (find(widths.begin(), widths.end(), w) == widths.end()) {
            widths.push_back(w);
        }
    };

    if (base <= 64) {
        addWidth(base);
        addWidth(base + 1);
        addWidth(base - 1);
        addWidth(base + 2);
        addWidth(base - 2);
        addWidth((base * 5 + 3) / 4);
        addWidth((base * 3 + 3) / 4);
        addWidth(base / 2);
        addWidth(base * 2);
    } else {
        addWidth(base);
        addWidth((base * 5 + 3) / 4);
        addWidth((base * 3 + 3) / 4);
        addWidth(base + 2);
        addWidth(base - 2);
        addWidth(base / 2);
        addWidth(base * 2);
    }

    vector<pair<int, int>> configurations;
    for (int w : widths) configurations.push_back({w, 0});
    configurations.push_back({base, 1});
    configurations.push_back({base, 2});
    configurations.push_back({base, 3});
    configurations.push_back({base, 4});

    long long workEstimate = 1LL * n * totalCells;
    int maxRuns;
    if (workEstimate > 500000000LL) maxRuns = 2;
    else if (workEstimate > 100000000LL) maxRuns = 3;
    else if (workEstimate > 20000000LL) maxRuns = 4;
    else if (workEstimate > 2000000LL) maxRuns = 7;
    else maxRuns = 12;

    Result best;
    long long bestArea = LLONG_MAX;
    int bestTieH = INT_MAX;
    int bestTieW = INT_MAX;

    maxRuns = min(maxRuns, (int)configurations.size());

    for (int run = 0; run < maxRuns; ++run) {
        auto [w, orderType] = configurations[run];
        Result current = packStrip(pieces, orders[orderType], w);

        long long area = 1LL * current.W * current.H;
        int tieH = min(current.W, current.H);
        int tieW = max(current.W, current.H);

        if (area < bestArea ||
            (area == bestArea && (tieH < bestTieH ||
             (tieH == bestTieH && tieW < bestTieW)))) {
            bestArea = area;
            bestTieH = tieH;
            bestTieW = tieW;
            best = std::move(current);
        }

        if (bestArea == totalCells) break;
    }

    bool rotateWhole = best.H > best.W;
    int outputW = rotateWhole ? best.H : best.W;
    int outputH = rotateWhole ? best.W : best.H;

    cout << outputW << ' ' << outputH << '\n';

    for (int i = 0; i < n; ++i) {
        const Placement& p = best.placements[i];
        const Orientation& o = pieces[i].orientations[p.orient];

        long long X = (long long)p.x - o.mx;
        long long Y = (long long)p.y - o.my;
        int R = o.r;
        int F = o.f;

        if (rotateWhole) {
            long long newX = (long long)best.H - 1 - Y;
            long long newY = X;
            X = newX;
            Y = newY;
            R = (R + 3) & 3;
        }

        cout << X << ' ' << Y << ' ' << R << ' ' << F << '\n';
    }

    return 0;
}