题面
来源: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; }
Kimi K3.1 Pro(high) · 采样 1 · 327 行
#include <bits/stdc++.h> using namespace std; struct Orientation { int w = 0, h = 0, R = 0, F = 0; int minX = 0, minY = 0; vector<pair<int,int>> cells; vector<unsigned long long> rows; }; struct Piece { int k = 0; vector<Orientation> orientations; }; struct PackResult { long long area = LLONG_MAX; int W = 0, H = 0; vector<array<int,4>> placement; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Piece> pieces(n); int totalCells = 0; for (int i = 0; i < n; ++i) { int k; cin >> k; pieces[i].k = k; totalCells += k; vector<pair<int,int>> original(k); for (auto &cell : original) { cin >> cell.first >> cell.second; } for (int f = 0; f <= 1; ++f) { for (int r = 0; r < 4; ++r) { vector<pair<int,int>> transformed; transformed.reserve(k); for (auto [x, y] : original) { int a = x, b = y; if (f) a = -a; int nx, ny; if (r == 0) { nx = a; ny = b; } else if (r == 1) { nx = b; ny = -a; } else if (r == 2) { nx = -a; ny = -b; } else { nx = -b; ny = a; } transformed.push_back({nx, ny}); } int minX = transformed[0].first; int minY = transformed[0].second; int maxX = minX, maxY = minY; for (auto [x, y] : transformed) { minX = min(minX, x); minY = min(minY, y); maxX = max(maxX, x); maxY = max(maxY, y); } Orientation o; o.w = maxX - minX + 1; o.h = maxY - minY + 1; o.R = r; o.F = f; o.minX = minX; o.minY = minY; for (auto [x, y] : transformed) { o.cells.push_back({x - minX, y - minY}); } sort(o.cells.begin(), o.cells.end()); bool duplicate = false; for (const auto &old : pieces[i].orientations) { if (old.cells == o.cells) { duplicate = true; break; } } if (duplicate) continue; o.rows.assign(o.h, 0); for (auto [x, y] : o.cells) { o.rows[y] |= (1ULL << x); } pieces[i].orientations.push_back(std::move(o)); } } } auto pack = [&](int width, int mode, int buffer, const vector<int> &initialOrder) -> PackResult { vector<unsigned long long> occupied(2 * totalCells + 64, 0); vector<int> remaining = initialOrder; PackResult result; result.placement.resize(n); auto ensureRow = [&](int row) { if (row >= (int)occupied.size()) { occupied.resize(row + 1, 0); } }; int cursorX = 0, cursorY = 0; int minUsedX = INT_MAX, minUsedY = INT_MAX; int maxUsedX = -1, maxUsedY = -1; while (!remaining.empty()) { ensureRow(cursorY); while (true) { if (cursorX >= width) { cursorX = 0; ++cursorY; ensureRow(cursorY); continue; } if (occupied[cursorY] & (1ULL << cursorX)) { ++cursorX; continue; } break; } int limit = min(buffer, (int)remaining.size()); bool found = false; long long bestScore = LLONG_MAX; int bestIndex = -1, bestOrientation = -1; int bestTX = 0, bestTY = 0; for (int idx = 0; idx < limit; ++idx) { int pieceId = remaining[idx]; const Piece &piece = pieces[pieceId]; for (int oi = 0; oi < (int)piece.orientations.size(); ++oi) { const Orientation &o = piece.orientations[oi]; for (auto [ax, ay] : o.cells) { int tx = cursorX - ax; int ty = cursorY - ay; if (tx < 0 || ty < 0 || tx + o.w > width) continue; ensureRow(ty + o.h); bool fits = true; for (int dy = 0; dy < o.h; ++dy) { unsigned long long mask = o.rows[dy] << tx; if (occupied[ty + dy] & mask) { fits = false; break; } } if (!fits) continue; unsigned long long currentRow = occupied[cursorY]; if (cursorY >= ty && cursorY < ty + o.h) { currentRow |= o.rows[cursorY - ty] << tx; } int run = 0; while (cursorX + run < width && (currentRow & (1ULL << (cursorX + run)))) { ++run; } int contact = 0; for (auto [cx, cy] : o.cells) { int px = tx + cx; int py = ty + cy; const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; for (int d = 0; d < 4; ++d) { int nx = px + dx[d]; int ny = py + dy[d]; if (nx < 0 || nx >= width || ny < 0) { ++contact; } else if (ny < (int)occupied.size() && (occupied[ny] & (1ULL << nx))) { ++contact; } } } int height = ty + o.h - cursorY; long long score; if (mode == 0) { score = (long long)height * 1000000LL - (long long)run * 10000LL - (long long)contact * 100LL - piece.k; } else { score = -(long long)run * 1000000LL + (long long)height * 10000LL - (long long)contact * 100LL - piece.k; } if (score < bestScore) { bestScore = score; found = true; bestIndex = idx; bestOrientation = oi; bestTX = tx; bestTY = ty; } } } } if (!found) { occupied[cursorY] |= (1ULL << cursorX); ++cursorX; continue; } int pieceId = remaining[bestIndex]; const Orientation &o = pieces[pieceId].orientations[bestOrientation]; for (int dy = 0; dy < o.h; ++dy) { occupied[bestTY + dy] |= o.rows[dy] << bestTX; } result.placement[pieceId] = { bestTX - o.minX, bestTY - o.minY, o.R, o.F }; minUsedX = min(minUsedX, bestTX); minUsedY = min(minUsedY, bestTY); maxUsedX = max(maxUsedX, bestTX + o.w - 1); maxUsedY = max(maxUsedY, bestTY + o.h - 1); remaining[bestIndex] = remaining.back(); remaining.pop_back(); ++cursorX; } result.W = maxUsedX - minUsedX + 1; result.H = maxUsedY - minUsedY + 1; result.area = (long long)result.W * result.H; for (auto &p : result.placement) { p[0] -= minUsedX; p[1] -= minUsedY; } return result; }; vector<int> order(n); iota(order.begin(), order.end(), 0); int baseWidth = (int)llround(sqrt((double)totalCells)); baseWidth = max(10, min(64, baseWidth)); int buffer; if (n <= 250) buffer = n; else if (n <= 2000) buffer = 32; else buffer = 22; PackResult best; auto better = [](const PackResult &a, const PackResult &b) { if (a.area != b.area) return a.area < b.area; if (a.H != b.H) return a.H < b.H; return a.W < b.W; }; PackResult first = pack(baseWidth, 0, buffer, order); if (better(first, best)) best = std::move(first); if (best.area > totalCells) { vector<int> shuffled = order; mt19937 rng(123456789); shuffle(shuffled.begin(), shuffled.end(), rng); int secondWidth = (baseWidth == 64) ? 56 : min(64, baseWidth + 7); secondWidth = max(10, secondWidth); PackResult second = pack(secondWidth, 1, buffer, shuffled); if (better(second, best)) best = std::move(second); } if (best.area > totalCells && n <= 1500) { vector<int> sortedOrder = order; stable_sort(sortedOrder.begin(), sortedOrder.end(), [&](int a, int b) { return pieces[a].k > pieces[b].k; }); int thirdWidth = max(10, baseWidth - 5); PackResult third = pack(thirdWidth, 0, buffer, sortedOrder); if (better(third, best)) best = std::move(third); } cout << best.W << ' ' << best.H << '\n'; for (int i = 0; i < n; ++i) { cout << best.placement[i][0] << ' ' << best.placement[i][1] << ' ' << best.placement[i][2] << ' ' << best.placement[i][3] << '\n'; } return 0; }
DeepSeek Flash V4.1-1017 · 采样 1 · 220 行
#include <bits/stdc++.h> using namespace std; struct Cell { int dx, dy; }; struct Ori { Cell cells[10]; int k; int minX, minY; int R, F; int w, h; int maxdx, maxdy; }; struct Piece { int id; int k; Cell orig[10]; vector<Ori> ors; int area; int minW; int maxDim; }; struct Placement { int ori; int x, y; }; int pack(int W, const vector<Piece>& pieces, const vector<int>& order, vector<Placement>& placements) { int n = pieces.size(); vector<int> skyline(W, 0); int currentMax = 0; for (int idx = 0; idx < n; ++idx) { int id = order[idx]; const Piece &p = pieces[id]; int bestOri = -1, bestX = -1, bestY = -1; int bestNewMax = INT_MAX, bestYCost = INT_MAX, bestXCost = INT_MAX; for (int oi = 0; oi < (int)p.ors.size(); ++oi) { const Ori &o = p.ors[oi]; if (o.w > W) continue; int maxX = W - o.w; for (int x = 0; x <= maxX; ++x) { int y = skyline[x + o.cells[0].dx] - o.cells[0].dy; for (int c = 1; c < o.k; ++c) { int val = skyline[x + o.cells[c].dx] - o.cells[c].dy; if (val > y) y = val; } int cand = y + o.maxdy + 1; int newMax = currentMax > cand ? currentMax : cand; if (newMax < bestNewMax || (newMax == bestNewMax && y < bestYCost) || (newMax == bestNewMax && y == bestYCost && x < bestXCost)) { bestNewMax = newMax; bestYCost = y; bestXCost = x; bestOri = oi; bestX = x; bestY = y; } } } const Ori &o = p.ors[bestOri]; for (int c = 0; c < o.k; ++c) { int col = bestX + o.cells[c].dx; int val = bestY + o.cells[c].dy + 1; if (val > skyline[col]) skyline[col] = val; } currentMax = bestNewMax; placements[id] = {bestOri, bestX, bestY}; } return currentMax; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Piece> pieces(n); int totalCells = 0; int globalMinW = 0; for (int i = 0; i < n; ++i) { Piece &p = pieces[i]; p.id = i; cin >> p.k; totalCells += p.k; p.area = p.k; for (int j = 0; j < p.k; ++j) { cin >> p.orig[j].dx >> p.orig[j].dy; } vector<Ori> ors; for (int F = 0; F <= 1; ++F) { for (int R = 0; R <= 2; R += 2) { int tx[10], ty[10]; int minX = 1000000, minY = 1000000, maxX = -1000000, maxY = -1000000; for (int j = 0; j < p.k; ++j) { int x = p.orig[j].dx; int y = p.orig[j].dy; if (F) x = -x; if (R == 2) { x = -x; y = -y; } tx[j] = x; ty[j] = y; if (x < minX) minX = x; if (x > maxX) maxX = x; if (y < minY) minY = y; if (y > maxY) maxY = y; } Cell norm[10]; for (int j = 0; j < p.k; ++j) { norm[j].dx = tx[j] - minX; norm[j].dy = ty[j] - minY; } vector<pair<int,int>> vec; for (int j = 0; j < p.k; ++j) vec.push_back({norm[j].dx, norm[j].dy}); sort(vec.begin(), vec.end()); bool found = false; for (auto &o : ors) { vector<pair<int,int>> ovec; for (int c = 0; c < o.k; ++c) ovec.push_back({o.cells[c].dx, o.cells[c].dy}); sort(ovec.begin(), ovec.end()); if (ovec == vec) { found = true; break; } } if (!found) { Ori o; o.k = p.k; o.minX = minX; o.minY = minY; o.R = R; o.F = F; o.w = maxX - minX + 1; o.h = maxY - minY + 1; o.maxdx = 0; o.maxdy = 0; for (int j = 0; j < p.k; ++j) { o.cells[j] = norm[j]; if (norm[j].dx > o.maxdx) o.maxdx = norm[j].dx; if (norm[j].dy > o.maxdy) o.maxdy = norm[j].dy; } ors.push_back(o); } } } p.ors = ors; p.minW = 1000000; p.maxDim = 0; for (auto &o : p.ors) { if (o.w < p.minW) p.minW = o.w; int d = max(o.w, o.h); if (d > p.maxDim) p.maxDim = d; } if (p.minW > globalMinW) globalMinW = p.minW; } vector<int> order(n); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int a, int b) { const Piece &pa = pieces[a]; const Piece &pb = pieces[b]; if (pa.area != pb.area) return pa.area > pb.area; if (pa.maxDim != pb.maxDim) return pa.maxDim > pb.maxDim; return pa.id < pb.id; }); int base = (int)ceil(sqrt((double)totalCells)); vector<int> candidates; double factors[] = {0.8, 0.95, 1.1, 1.25, 1.4}; for (double f : factors) { int W = (int)ceil(base * f); if (W < globalMinW) W = globalMinW; if (find(candidates.begin(), candidates.end(), W) == candidates.end()) candidates.push_back(W); } if (find(candidates.begin(), candidates.end(), globalMinW) == candidates.end()) candidates.push_back(globalMinW); int bestArea = INT_MAX, bestW = -1, bestH = -1; vector<Placement> bestPlac(n); for (int W : candidates) { vector<Placement> plac(n); int H = pack(W, pieces, order, plac); int maxX = 0; for (int i = 0; i < n; ++i) { const Piece &p = pieces[i]; const Placement &pl = plac[i]; const Ori &o = p.ors[pl.ori]; for (int c = 0; c < o.k; ++c) { int x = pl.x + o.cells[c].dx; if (x > maxX) maxX = x; } } int actualW = maxX + 1; int actualH = H; int area = actualW * actualH; bool better = false; if (area < bestArea) better = true; else if (area == bestArea) { if (actualH < bestH) better = true; else if (actualH == bestH && actualW < bestW) better = true; } if (better) { bestArea = area; bestW = actualW; bestH = actualH; bestPlac = plac; } } cout << bestW << " " << bestH << "\n"; for (int i = 0; i < n; ++i) { const Piece &p = pieces[i]; const Placement &pl = bestPlac[i]; const Ori &o = p.ors[pl.ori]; int X = pl.x - o.minX; int Y = pl.y - o.minY; cout << X << " " << Y << " " << o.R << " " << o.F << "\n"; } return 0; }
MiniMax M3.1 Flash · 采样 1 · 1091 行
#include <bits/stdc++.h> using namespace std; using Cell = pair<int, int>; using ull = unsigned long long; struct CellVectorHash { size_t operator()(const vector<Cell>& v) const noexcept { ull h = 1469598103934665603ULL; for (auto [x, y] : v) { h ^= static_cast<ull>(static_cast<unsigned>(x)) + 0x9e3779b97f4a7c15ULL; h *= 1099511628211ULL; h ^= static_cast<ull>(static_cast<unsigned>(y)) + 0x9e3779b97f4a7c15ULL; h *= 1099511628211ULL; } return static_cast<size_t>(h); } }; struct RNG { ull state; explicit RNG(ull seed = 88172645463325252ULL) : state(seed) {} ull next() { ull x = state; x ^= x << 7; x ^= x >> 9; x ^= x << 8; state = x; return x; } }; struct Orientation { int f = 0, r = 0; int offsetX = 0, offsetY = 0; int w = 0, h = 0; vector<Cell> cells; }; struct Piece { int k = 0; vector<Cell> raw; vector<Orientation> orientations; vector<Cell> canonical; }; static vector<Cell> normalizedCells(vector<Cell> cells) { if (cells.empty()) return cells; int minX = INT_MAX, minY = INT_MAX; for (auto [x, y] : cells) { minX = min(minX, x); minY = min(minY, y); } for (auto& [x, y] : cells) { x -= minX; y -= minY; } sort(cells.begin(), cells.end()); return cells; } static vector<Cell> transformAndNormalize(const vector<Cell>& cells, int f, int r) { vector<Cell> result; result.reserve(cells.size()); for (auto [x0, y0] : cells) { int x = x0, y = y0; if (f) x = -x; for (int t = 0; t < r; ++t) { int nx = y; int ny = -x; x = nx; y = ny; } result.emplace_back(x, y); } return normalizedCells(std::move(result)); } static vector<Cell> canonicalShape(const vector<Cell>& cells) { vector<Cell> best; for (int f = 0; f < 2; ++f) { for (int r = 0; r < 4; ++r) { auto v = transformAndNormalize(cells, f, r); if (best.empty() || lexicographical_compare(v.begin(), v.end(), best.begin(), best.end())) { best = std::move(v); } } } return best; } static bool connectedCells(const vector<Cell>& cells) { if (cells.empty()) return false; int minX = INT_MAX, minY = INT_MAX; int maxX = INT_MIN, maxY = INT_MIN; for (auto [x, y] : cells) { minX = min(minX, x); minY = min(minY, y); maxX = max(maxX, x); maxY = max(maxY, y); } int w = maxX - minX + 1; int h = maxY - minY + 1; vector<unsigned char> used(w * h, 0); queue<Cell> q; auto indexOf = [&](int x, int y) { return (y - minY) * w + (x - minX); }; for (auto [x, y] : cells) { int id = indexOf(x, y); if (!used[id]) { used[id] = 1; q.emplace(x, y); } } int reached = 0; const int dx[4] = {1, -1, 0, 0}; const int dy[4] = {0, 0, 1, -1}; while (!q.empty()) { auto [x, y] = q.front(); q.pop(); ++reached; for (int d = 0; d < 4; ++d) { int nx = x + dx[d]; int ny = y + dy[d]; if (nx < minX || nx > maxX || ny < minY || ny > maxY) continue; int id = indexOf(nx, ny); if (!used[id]) { used[id] = 1; q.emplace(nx, ny); } } } return reached == static_cast<int>(cells.size()); } static ull pairKey(int a, int b) { if (a > b) swap(a, b); return (static_cast<ull>(static_cast<unsigned>(a)) << 32) | static_cast<unsigned>(b); } struct PairBuild { int p = -1, q = -1; int hp = -1, hq = -1; int tx = 0, ty = 0; int w = 0, h = 0; }; static vector<PairBuild> findPairs(const vector<Piece>& pieces) { unordered_map<vector<Cell>, vector<int>, CellVectorHash> index; index.reserve(4096); for (int i = 0; i < static_cast<int>(pieces.size()); ++i) { if (pieces[i].k != 10) continue; index[pieces[i].canonical].push_back(i); } if (index.empty()) return {}; struct ComplementInfo { bool connected = false; vector<Cell> normalized; vector<Cell> canonical; }; unordered_map<ull, ComplementInfo> cache; cache.reserve(65536); unordered_map<ull, PairBuild> edges; const vector<pair<int, int>> dimensions = { {2, 10}, {4, 5}, {5, 4}, {10, 2} }; for (int pid = 0; pid < static_cast<int>(pieces.size()); ++pid) { const Piece& piece = pieces[pid]; if (piece.k != 10) continue; for (auto [W, H] : dimensions) { uint32_t rectangleMask = 0; for (int i = 0; i < W * H; ++i) { rectangleMask |= (1U << i); } for (int hp = 0; hp < static_cast<int>(piece.orientations.size()); ++hp) { const Orientation& o = piece.orientations[hp]; if (o.w > W || o.h > H) continue; for (int ty = 0; ty <= H - o.h; ++ty) { for (int tx = 0; tx <= W - o.w; ++tx) { uint32_t placedMask = 0; for (auto [x, y] : o.cells) { int id = (ty + y) * W + tx + x; placedMask |= (1U << id); } uint32_t complementMask = rectangleMask & ~placedMask; ull cacheKey = static_cast<ull>(complementMask) | (static_cast<ull>(W) << 20) | (static_cast<ull>(H) << 25); auto cit = cache.find(cacheKey); if (cit == cache.end()) { ComplementInfo info; vector<Cell> complement; for (int i = 0; i < W * H; ++i) { if (complementMask & (1U << i)) { complement.emplace_back(i % W, i / W); } } info.connected = connectedCells(complement); if (info.connected) { info.normalized = normalizedCells(complement); info.canonical = canonicalShape(complement); } cit = cache.emplace(cacheKey, std::move(info)).first; } const ComplementInfo& info = cit->second; if (!info.connected) continue; auto iit = index.find(info.canonical); if (iit == index.end()) continue; for (int qid : iit->second) { if (qid == pid) continue; int hq = -1; const Piece& qpiece = pieces[qid]; for (int j = 0; j < static_cast<int>(qpiece.orientations.size()); ++j) { if (qpiece.orientations[j].cells == info.normalized) { hq = j; break; } } if (hq < 0) continue; ull ek = pairKey(pid, qid); if (edges.find(ek) == edges.end()) { PairBuild edge; edge.p = pid; edge.q = qid; edge.hp = hp; edge.hq = hq; edge.tx = tx; edge.ty = ty; edge.w = W; edge.h = H; edges.emplace(ek, edge); } } } } } } } vector<int> degree(pieces.size(), 0); for (const auto& [key, edge] : edges) { ++degree[edge.p]; ++degree[edge.q]; } vector<PairBuild> allEdges; allEdges.reserve(edges.size()); for (const auto& [key, edge] : edges) allEdges.push_back(edge); sort(allEdges.begin(), allEdges.end(), [&](const PairBuild& a, const PairBuild& b) { int da = min(degree[a.p], degree[a.q]); int db = min(degree[b.p], degree[b.q]); if (da != db) return da < db; int ma = max(degree[a.p], degree[a.q]); int mb = max(degree[b.p], degree[b.q]); if (ma != mb) return ma < mb; return pairKey(a.p, a.q) < pairKey(b.p, b.q); }); vector<unsigned char> used(pieces.size(), 0); vector<PairBuild> result; for (const PairBuild& edge : allEdges) { if (used[edge.p] || used[edge.q]) continue; used[edge.p] = used[edge.q] = 1; result.push_back(edge); } return result; } struct Membership { int piece = -1; int orientation = -1; Cell translation{0, 0}; }; struct ObjectOrientation { int w = 0, h = 0; vector<Cell> cells; vector<int> order; vector<unsigned char> hasBelow; vector<Membership> members; }; struct Object { int area = 0; vector<ObjectOrientation> orientations; }; static void prepareObjectOrientation(ObjectOrientation& o) { o.w = 0; o.h = 0; for (auto [x, y] : o.cells) { o.w = max(o.w, x + 1); o.h = max(o.h, y + 1); } o.order.resize(o.cells.size()); iota(o.order.begin(), o.order.end(), 0); sort(o.order.begin(), o.order.end(), [&](int a, int b) { if (o.cells[a].second != o.cells[b].second) { return o.cells[a].second < o.cells[b].second; } return o.cells[a].first < o.cells[b].first; }); o.hasBelow.assign(o.cells.size(), 0); for (int i = 0; i < static_cast<int>(o.cells.size()); ++i) { auto [x, y] = o.cells[i]; for (int j = 0; j < static_cast<int>(o.cells.size()); ++j) { if (o.cells[j].first == x && o.cells[j].second == y - 1) { o.hasBelow[i] = 1; break; } } } } static int matchOrientation(const Piece& piece, const vector<Cell>& local, Cell& translation) { int minX = INT_MAX, minY = INT_MAX; for (auto [x, y] : local) { minX = min(minX, x); minY = min(minY, y); } vector<Cell> normalized = local; for (auto& [x, y] : normalized) { x -= minX; y -= minY; } sort(normalized.begin(), normalized.end()); for (int i = 0; i < static_cast<int>(piece.orientations.size()); ++i) { if (piece.orientations[i].cells == normalized) { translation = { minX - piece.orientations[i].offsetX, minY - piece.orientations[i].offsetY }; return i; } } return -1; } static vector<Cell> rectangleCells(int w, int h) { vector<Cell> cells; cells.reserve(w * h); for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { cells.emplace_back(x, y); } } sort(cells.begin(), cells.end()); return cells; } static vector<Object> buildSingleObjects(const vector<Piece>& pieces) { vector<Object> objects; objects.reserve(pieces.size()); for (int i = 0; i < static_cast<int>(pieces.size()); ++i) { Object object; object.area = pieces[i].k; for (int j = 0; j < static_cast<int>(pieces[i].orientations.size()); ++j) { const Orientation& source = pieces[i].orientations[j]; ObjectOrientation target; target.cells = source.cells; Membership membership; membership.piece = i; membership.orientation = j; membership.translation = {-source.offsetX, -source.offsetY}; target.members.push_back(std::move(membership)); prepareObjectOrientation(target); object.orientations.push_back(std::move(target)); } objects.push_back(std::move(object)); } return objects; } static vector<Object> buildPairedObjects(const vector<Piece>& pieces, const vector<PairBuild>& pairs) { vector<Object> objects; vector<unsigned char> used(pieces.size(), 0); for (const PairBuild& pair : pairs) { const Piece& p = pieces[pair.p]; const Piece& q = pieces[pair.q]; const Orientation& po = p.orientations[pair.hp]; vector<Cell> a; for (auto [x, y] : po.cells) { a.emplace_back(x + pair.tx, y + pair.ty); } vector<Cell> rectangle = rectangleCells(pair.w, pair.h); vector<Cell> b; for (auto cell : rectangle) { bool inP = false; for (auto ac : a) { if (ac == cell) { inP = true; break; } } if (!inP) b.push_back(cell); } Object object; object.area = 20; for (int rotation = 0; rotation < 2; ++rotation) { ObjectOrientation target; vector<Cell> localA = a; vector<Cell> localB = b; vector<Cell> geometry = rectangle; if (rotation == 1) { auto rotateCells = [&](vector<Cell>& cells) { for (auto& [x, y] : cells) { int oldX = x; x = y; y = pair.w - 1 - oldX; } sort(cells.begin(), cells.end()); }; rotateCells(localA); rotateCells(localB); rotateCells(geometry); } target.cells = std::move(geometry); Cell translation; int orientation = matchOrientation(p, localA, translation); assert(orientation >= 0); Membership mp; mp.piece = pair.p; mp.orientation = orientation; mp.translation = translation; target.members.push_back(std::move(mp)); translation = {0, 0}; orientation = matchOrientation(q, localB, translation); assert(orientation >= 0); Membership mq; mq.piece = pair.q; mq.orientation = orientation; mq.translation = translation; target.members.push_back(std::move(mq)); prepareObjectOrientation(target); object.orientations.push_back(std::move(target)); } used[pair.p] = used[pair.q] = 1; objects.push_back(std::move(object)); } for (int i = 0; i < static_cast<int>(pieces.size()); ++i) { if (used[i]) continue; Object object; object.area = pieces[i].k; for (int j = 0; j < static_cast<int>(pieces[i].orientations.size()); ++j) { const Orientation& source = pieces[i].orientations[j]; ObjectOrientation target; target.cells = source.cells; Membership membership; membership.piece = i; membership.orientation = j; membership.translation = {-source.offsetX, -source.offsetY}; target.members.push_back(std::move(membership)); prepareObjectOrientation(target); object.orientations.push_back(std::move(target)); } objects.push_back(std::move(object)); } return objects; } static constexpr int MAX_DX = 20; class Board { public: int width; int words; int stride; int height = 0; int minX = INT_MAX, minY = INT_MAX; int maxX = INT_MIN, maxY = INT_MIN; vector<ull> occupied; vector<ull> shifted; vector<vector<ull>> baseMasks; Board(int w, int totalCells) : width(w) { words = (width + 63) / 64; stride = words + 1; size_t estimatedRows = max<size_t>(64, 3ULL * totalCells / max(1, width) + 64); occupied.reserve(estimatedRows * stride); shifted.reserve(estimatedRows * MAX_DX * stride); baseMasks.resize(MAX_DX + 1); for (int sw = 1; sw <= MAX_DX; ++sw) { if (sw > width) continue; int maxXPosition = width - sw; int count = maxXPosition / 64 + 1; baseMasks[sw].assign(words, 0); for (int q = 0; q < count; ++q) { int bitCount = min(64, maxXPosition - q * 64 + 1); if (bitCount == 64) { baseMasks[sw][q] = ~0ULL; } else { baseMasks[sw][q] = (1ULL << bitCount) - 1; } } } ensureRows(0); } void ensureRows(int newHeight) { if (newHeight <= height) return; occupied.resize(static_cast<size_t>(newHeight) * stride, 0); shifted.resize(static_cast<size_t>(newHeight) * MAX_DX * stride, 0); height = newHeight; } void refreshRow(int y) { const ull* source = occupied.data() + static_cast<size_t>(y) * stride; for (int dx = 0; dx < MAX_DX; ++dx) { ull* destination = shifted.data() + (static_cast<size_t>(y) * MAX_DX + dx) * stride; if (dx == 0) { for (int q = 0; q < words; ++q) { destination[q] = source[q]; } } else { for (int q = 0; q < words; ++q) { destination[q] = (source[q] >> dx) | (source[q + 1] << (64 - dx)); } } } } void add(const vector<Cell>& cells, int x, int y) { for (auto [dx, dy] : cells) { int px = x + dx; int py = y + dy; occupied[static_cast<size_t>(py) * stride + (px >> 6)] |= 1ULL << (px & 63); minX = min(minX, px); maxX = max(maxX, px); minY = min(minY, py); maxY = max(maxY, py); } int maxRow = 0; for (auto [dx, dy] : cells) maxRow = max(maxRow, dy); for (int row = 0; row <= maxRow; ++row) refreshRow(y + row); } bool isOccupied(int x, int y) const { if (x < 0 || x >= width || y < 0 || y >= height) return false; return (occupied[static_cast<size_t>(y) * stride + (x >> 6)] >> (x & 63)) & 1ULL; } }; struct ObjectPlacement { int object = -1; int orientation = -1; int x = 0, y = 0; }; struct PackResult { vector<ObjectPlacement> placements; int minX = INT_MAX, minY = INT_MAX; int maxX = INT_MIN, maxY = INT_MIN; int width = 0, height = 0; }; struct ObjectStats { int area = 0; int boxArea = INT_MAX; int minHeight = INT_MAX; int minWidth = INT_MAX; ull randomKey = 0; }; static vector<int> makeOrder(const vector<Object>& objects, int mode, ull seed) { RNG rng(seed); vector<ObjectStats> stats(objects.size()); for (int i = 0; i < static_cast<int>(objects.size()); ++i) { stats[i].area = objects[i].area; stats[i].randomKey = rng.next(); for (const ObjectOrientation& o : objects[i].orientations) { stats[i].boxArea = min(stats[i].boxArea, o.w * o.h); stats[i].minHeight = min(stats[i].minHeight, o.h); stats[i].minWidth = min(stats[i].minWidth, o.w); } } vector<int> order(objects.size()); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int a, int b) { const ObjectStats& x = stats[a]; const ObjectStats& y = stats[b]; if (x.area != y.area) return x.area > y.area; if (mode == 0) { if (x.boxArea != y.boxArea) return x.boxArea > y.boxArea; if (x.minHeight != y.minHeight) return x.minHeight > y.minHeight; } else if (mode == 1) { if (x.minHeight != y.minHeight) return x.minHeight > y.minHeight; if (x.minWidth != y.minWidth) return x.minWidth > y.minWidth; if (x.boxArea != y.boxArea) return x.boxArea > y.boxArea; } else { if (x.randomKey != y.randomKey) return x.randomKey < y.randomKey; } return x.randomKey < y.randomKey; }); return order; } static bool calculateValidPositions(const ObjectOrientation& orientation, int y, Board& board, vector<ull>& valid) { const vector<ull>& initial = board.baseMasks[orientation.w]; if (initial.empty()) return false; copy(initial.begin(), initial.end(), valid.begin()); for (int index : orientation.order) { auto [dx, dy] = orientation.cells[index]; const ull* forbidden = board.shifted.data() + (static_cast<size_t>(y + dy) * MAX_DX + dx) * board.stride; ull any = 0; for (int q = 0; q < board.words; ++q) { valid[q] &= ~forbidden[q]; any |= valid[q]; } if (any == 0) return false; } return true; } static int placementScore(const ObjectOrientation& orientation, int x, int y, const Board& board) { int contacts = 0; int overhang = 0; for (int index : orientation.order) { auto [dx, dy] = orientation.cells[index]; int px = x + dx; int py = y + dy; if (board.isOccupied(px - 1, py)) ++contacts; if (board.isOccupied(px + 1, py)) ++contacts; if (board.isOccupied(px, py - 1)) ++contacts; if (board.isOccupied(px, py + 1)) ++contacts; if (!orientation.hasBelow[index] && py > 0 && !board.isOccupied(px, py - 1)) { ++overhang; } } return contacts * 2000 - overhang * 200 - x; } static PackResult packObjects(const vector<Object>& objects, const vector<int>& order, int width) { Board board(width, 0); PackResult result; vector<ull> valid(board.words); for (int objectIndex : order) { const Object& object = objects[objectIndex]; bool placed = false; for (int y = 0; y < board.height && !placed; ++y) { bool found = false; int bestScore = INT_MIN; int bestOrientation = -1; int bestX = -1; for (int oi = 0; oi < static_cast<int>(object.orientations.size()); ++oi) { const ObjectOrientation& orientation = object.orientations[oi]; if (orientation.w > width || y + orientation.h > board.height) continue; if (!calculateValidPositions(orientation, y, board, valid)) continue; int collected = 0; for (int q = 0; q < board.words && collected < 12; ++q) { ull bits = valid[q]; while (bits && collected < 12) { int bit = __builtin_ctzll(bits); bits &= bits - 1; int x = q * 64 + bit; int score = placementScore(orientation, x, y, board); if (!found || score > bestScore || (score == bestScore && x < bestX)) { found = true; bestScore = score; bestOrientation = oi; bestX = x; } ++collected; } } } if (found) { board.add(object.orientations[bestOrientation].cells, bestX, y); result.placements.push_back( {objectIndex, bestOrientation, bestX, y}); placed = true; } } if (!placed) { int chosen = -1; for (int oi = 0; oi < static_cast<int>(object.orientations.size()); ++oi) { const ObjectOrientation& o = object.orientations[oi]; if (o.w > width) continue; if (chosen < 0 || o.h < object.orientations[chosen].h || (o.h == object.orientations[chosen].h && o.w < object.orientations[chosen].w)) { chosen = oi; } } assert(chosen >= 0); int y = board.height; board.ensureRows(y + object.orientations[chosen].h); board.add(object.orientations[chosen].cells, 0, y); result.placements.push_back({objectIndex, chosen, 0, y}); } } result.minX = board.minX; result.minY = board.minY; result.maxX = board.maxX; result.maxY = board.maxY; result.width = result.maxX - result.minX + 1; result.height = result.maxY - result.minY + 1; return result; } struct Answer { int w = 0, h = 0; vector<int> x, y, r, f; bool valid = false; }; static Answer expandResult(const PackResult& pack, const vector<Object>& objects, const vector<Piece>& pieces) { Answer answer; answer.w = pack.width; answer.h = pack.height; answer.x.assign(pieces.size(), 0); answer.y.assign(pieces.size(), 0); answer.r.assign(pieces.size(), 0); answer.f.assign(pieces.size(), 0); for (const ObjectPlacement& placement : pack.placements) { const Object& object = objects[placement.object]; const ObjectOrientation& orientation = object.orientations[placement.orientation]; for (const Membership& membership : orientation.members) { const Orientation& source = pieces[membership.piece].orientations[membership.orientation]; answer.x[membership.piece] = membership.translation.first + placement.x - pack.minX; answer.y[membership.piece] = membership.translation.second + placement.y - pack.minY; answer.r[membership.piece] = source.r; answer.f[membership.piece] = source.f; } } return answer; } static bool validateAnswer(const Answer& answer, const vector<Piece>& pieces) { if (answer.w <= 0 || answer.h <= 0) return false; vector<unsigned char> grid( static_cast<size_t>(answer.w) * answer.h, 0); for (int i = 0; i < static_cast<int>(pieces.size()); ++i) { for (auto [x0, y0] : pieces[i].raw) { int x = x0, y = y0; if (answer.f[i]) x = -x; for (int t = 0; t < answer.r[i]; ++t) { int nx = y; int ny = -x; x = nx; y = ny; } x += answer.x[i]; y += answer.y[i]; if (x < 0 || x >= answer.w || y < 0 || y >= answer.h) return false; size_t id = static_cast<size_t>(y) * answer.w + x; if (grid[id]) return false; grid[id] = 1; } } return true; } static bool betterAnswer(const Answer& a, const Answer& b) { if (!b.valid) return true; long long areaA = 1LL * a.w * a.h; long long areaB = 1LL * b.w * b.h; if (areaA != areaB) return areaA < areaB; if (a.h != b.h) return a.h < b.h; return a.w < b.w; } static Answer emergencyAnswer(const vector<Piece>& pieces, int totalCells) { Answer answer; int width = max(20, static_cast<int>(ceil(sqrt(2.0 * totalCells))) + 20); int currentX = 0, currentY = 0, rowHeight = 0; answer.w = width; answer.x.resize(pieces.size()); answer.y.resize(pieces.size()); answer.r.resize(pieces.size()); answer.f.resize(pieces.size()); for (int i = 0; i < static_cast<int>(pieces.size()); ++i) { int chosen = -1; for (int j = 0; j < static_cast<int>(pieces[i].orientations.size()); ++j) { const Orientation& o = pieces[i].orientations[j]; if (chosen < 0 || o.h < pieces[i].orientations[chosen].h || (o.h == pieces[i].orientations[chosen].h && o.w < pieces[i].orientations[chosen].w)) { chosen = j; } } const Orientation& o = pieces[i].orientations[chosen]; if (currentX + o.w > width) { currentY += rowHeight; currentX = 0; rowHeight = 0; } answer.x[i] = currentX - o.offsetX; answer.y[i] = currentY - o.offsetY; answer.r[i] = o.r; answer.f[i] = o.f; currentX += o.w; rowHeight = max(rowHeight, o.h); } answer.h = currentY + rowHeight; answer.valid = validateAnswer(answer, pieces); return answer; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Piece> pieces(n); int totalCells = 0; for (int i = 0; i < n; ++i) { int k; cin >> k; pieces[i].k = k; pieces[i].raw.resize(k); totalCells += k; for (auto& [x, y] : pieces[i].raw) { cin >> x >> y; } for (int f = 0; f < 2; ++f) { for (int r = 0; r < 4; ++r) { Orientation orientation; orientation.f = f; orientation.r = r; vector<Cell> transformed; transformed.reserve(k); int minX = INT_MAX, minY = INT_MAX; for (auto [x0, y0] : pieces[i].raw) { int x = x0, y = y0; if (f) x = -x; for (int t = 0; t < r; ++t) { int nx = y; int ny = -x; x = nx; y = ny; } transformed.emplace_back(x, y); minX = min(minX, x); minY = min(minY, y); } orientation.offsetX = minX; orientation.offsetY = minY; for (auto& [x, y] : transformed) { x -= minX; y -= minY; } sort(transformed.begin(), transformed.end()); orientation.cells = transformed; orientation.w = 0; orientation.h = 0; for (auto [x, y] : transformed) { orientation.w = max(orientation.w, x + 1); orientation.h = max(orientation.h, y + 1); } bool duplicate = false; for (const Orientation& existing : pieces[i].orientations) { if (existing.cells == orientation.cells) { duplicate = true; break; } } if (!duplicate) { pieces[i].orientations.push_back(std::move(orientation)); } } } pieces[i].canonical = pieces[i].orientations.front().cells; for (const Orientation& o : pieces[i].orientations) { if (lexicographical_compare(o.cells.begin(), o.cells.end(), pieces[i].canonical.begin(), pieces[i].canonical.end())) { pieces[i].canonical = o.cells; } } } vector<PairBuild> pairs = findPairs(pieces); vector<Object> singleObjects = buildSingleObjects(pieces); vector<Object> pairedObjects = buildPairedObjects(pieces, pairs); int maximumWidth = 1; for (const Object& object : pairedObjects) { for (const ObjectOrientation& o : object.orientations) { maximumWidth = max(maximumWidth, o.w); } } double baseWidth = sqrt(static_cast<double>(totalCells)); vector<double> factors; if (n <= 500) { factors = {0.75, 1.05, 1.40, 1.80}; } else if (n <= 1500) { factors = {0.85, 1.15, 1.50}; } else if (n <= 4000) { factors = {0.95, 1.25, 1.55}; } else { factors = {1.05, 1.45}; } Answer best; int bestKind = 0; int attempt = 0; auto tryObjects = [&](const vector<Object>& objects, int kind, double factor, int mode) { int width = max(maximumWidth, static_cast<int>(ceil(baseWidth * factor))); width = max(width, 12); vector<int> order = makeOrder(objects, mode, 0x123456789abcdefULL + attempt * 977); PackResult packed = packObjects(objects, order, width); Answer answer = expandResult(packed, objects, pieces); if (!validateAnswer(answer, pieces)) return; if (betterAnswer(answer, best)) { best = std::move(answer); bestKind = kind; } ++attempt; }; bool usePaired = pairs.size() >= max<size_t>(3, n / 100); tryObjects(singleObjects, 0, factors[0], 0); if (usePaired) tryObjects(pairedObjects, 1, factors[0], 1); tryObjects(bestKind == 0 ? singleObjects : pairedObjects, bestKind, factors[1], 1); if (n <= 4000) { tryObjects(bestKind == 0 ? pairedObjects : singleObjects, bestKind == 0 ? 1 : 0, factors[1], 0); } if (n <= 1500) { tryObjects(bestKind == 0 ? singleObjects : pairedObjects, bestKind, factors[2], 0); tryObjects(bestKind == 0 ? pairedObjects : singleObjects, bestKind == 0 ? 1 : 0, factors[2], 1); } if (n <= 500) { tryObjects(bestKind == 0 ? singleObjects : pairedObjects, bestKind, factors[3], 0); tryObjects(bestKind == 0 ? pairedObjects : singleObjects, bestKind == 0 ? 1 : 0, factors[3], 1); } if (!best.valid) best = emergencyAnswer(pieces, totalCells); cout << best.w << ' ' << best.h << '\n'; for (int i = 0; i < n; ++i) { cout << best.x[i] << ' ' << best.y[i] << ' ' << best.r[i] << ' ' << best.f[i] << '\n'; } return 0; }