AtCoder Heuristic Contest 074

Layered Barcode
Display Editorial

Problem

  • 64 windows, 192 sheets, 256 items
    • Each item's barcode is a random pattern with exactly 32 blacks
  • Design the sheet patterns (white, black, hole) first, then process 1,024 orders one by one
    • Each order is uniformly random among the items other than the previous one
    • One operation: take out a sheet visible through some window and reinsert it at any depth
  • Score 2KE + T (smaller is better)
    • One incorrect order (E) costs as much as 384 operations → display every order correctly, then reduce the number of operations T

Design intent

Divisions compared (as of 17:00 on the final day)

Outline

Solution 0: The sample program

  • The sample program in the problem statement
  • Prints item k's pattern as is on sheet k (full-match sheets)
    • With 192 sheets, only items 0–191 get one
  • Every sheet covers every window, so only the top sheet can be moved
    • Move the top sheet to the bottom until the ordered sheet comes to the top
    • Orders for items 192–255 (1/4) are incorrect (each costs 384 operations)
  • Full-match sheets are meant as a hint toward other solutions
points

Visualizer: solution 0

  • Moves only the top sheet, cycling until the target sheet comes
  • Orders for items 192–255, which have no sheet, are left incorrect with no operations

seed=0, all 1,024 orders. Enter an order number to jump
Keys: p play / pause, . / , one operation forward / back,
n / b next order / start of the current order

Solution 1: Toggling each window

  • For each window j, place a sheet that is white only at window j and one that is black only at window j (128 sheets)
  • Window j shows the color of the upper of the two
    • To change a window's color, move the upper sheet just below the lower one (1 operation)
  • Operations = windows that differ from the previous item = 32 on average
  • Many participants submitted this solution, and their scores tied
  • Improving from here was somewhat hard
points

Visualizer: solution 1

  • For each mismatched window, move the upper sheet just below the lower one

seed=0, all 1,024 orders. Enter an order number to jump
Keys: p play / pause, . / , one operation forward / back,
n / b next order / start of the current order

Solution 1b: Breaking the tie with full-match sheets

  • Combine the full-match sheets of solution 0 with the toggle sheets of solution 1
  • Use toggle sheets only for windows 0–62 (126 sheets), and make the other 66 sheets full-match sheets
    • The full-match sheets line up at the bottom, and the head of the line shows in window 63
    • Window 63 is toggled by sending the head of the line to its end
  • For each order, choose the cheaper way
    • Toggling: the number of mismatched windows
    • Full-match sheet (if any): its position in the line + 1
  • → operations per order
points

Visualizer: solution 1b

  • When a full-match sheet comes to the top, all windows match at once

seed=0, all 1,024 orders. Enter an order number to jump
Keys: p play / pause, . / , one operation forward / back,
n / b next order / start of the current order

Side note: toggling each block

  • For each block of B windows, place B+1 comb-shaped sheets
    • Sheet i is black at windows 0 to i−1, white at window i, and holes elsewhere
    • Breadth-first search over the (B+1)! orderings
  • Given only the statement and left to do everything, AI tends to pick this approach (→ How far AI gets on its own)
  • B = 2 gives no gain, and larger B helps only a little
    • → We want one operation to fix many windows
B1 (sol. 1)2345664 (AI)
Operations per window0.500.500.440.420.410.400.33
points

Visualizer: block toggles

  • Reorders 5 sheets for each block of 4 windows

seed=0, all 1,024 orders. Enter an order number to jump
Keys: p play / pause, . / , one operation forward / back,
n / b next order / start of the current order

Solution 2: The staircase

  • For each window h, make a bundle of sheets (tier h)
    • A tier-h sheet is opaque only at windows 0 to h, printed with some item's colors there
  • Stacking tiers 0, 1, …, 63 in this order, the head (topmost sheet) of tier h shows in window h
  • Two kinds of operations
    • Toggle: move the head of tier h just below a sheet of the target color
    • Dig out a sheet that matches the target at windows 0 to h and put it on top. Return it to the head of its tier at the next order
  • For each order, choose the sheet that needs the fewest operations (or toggles only)

Tier sizes and results

  • Start with 2 sheets per tier, and add sheets one at a time to the tier where the expected operations decrease the most
    • Using a sheet in tier h with l sheets: (l−1)/2 to dig it out, plus (63−h)/2 toggles for the remaining windows
    • Deeper tiers, which cover more windows 0 to h, get more sheets
  • operations per order (60% of solution 1)
  • The stack on the right: the sheet patterns form a staircase
points

Visualizer: solution 2

  • Putting the dug-out sheet on top fixes windows 0 to h
  • The remaining windows are toggled at the tier heads

seed=0, all 1,024 orders. Enter an order number to jump
Keys: p play / pause, . / , one operation forward / back,
n / b next order / start of the current order

Solution 3: Pair sheets

  • We want a sheet for every item, but 192 sheets < 256 items
  • Two similar items share one sheet (64 pair sheets + 128 single sheets)
    • Windows where the two items differ become holes, shown by the head of that window's tier
    • Pair greedily, starting from the pairs with the fewest differing windows
  • Tier h needs both a sheet that is white at window h and one that is black. A pair sheet can go only in a tier where its two items agree at window h
  • For each order, dig out the item's sheet, put it on top, and toggle the windows it does not cover
points

Visualizer: solution 3

  • Each order digs out the item's sheet and puts it on top
  • The tier heads show the windows where the pair sheet has holes

seed=0, all 1,024 orders. Enter an order number to jump
Keys: p play / pause, . / , one operation forward / back,
n / b next order / start of the current order

Solution 4: Staircase windows and batch windows

  • In solution 3, a tier-h sheet fixes only windows 0 to h. The rest are toggled one by one
  • Split the windows into two kinds
    • Staircase windows (0–29) form the staircase. A sheet cannot be moved unless it is visible through some window, so these windows are how we grab the tier heads
    • Batch windows (30–63): every item's sheet covers all of them
  • Just putting the item's sheet on top fixes all 34 batch windows, and only the staircase windows need toggling
  • But we need a way to show the holes of pair sheets at batch windows (windows where the two items differ)

Filler sheets: showing the holes at batch windows

  • Goal: fill the batch-window holes of pair sheets using only a few sheets
  • Place filler sheets above all tiers, and change the colors of the holes just by reordering them
    • For each batch window, pick 2 filler sheets, one white and one black. The other filler sheets have a hole there
    • Window color = the color of the upper of the two (windows without a hole are covered by the item's sheet)
  • 7 sheets were enough
    • Assign the 21 pairs out of 7 sheets to the 34 batch windows in turn (13 windows repeat from the first pair)
    • With 6, too few pairs can be displayed in many cases

Pairs that filler sheets can display

  • The color at a hole of the pair sheet becomes an above/below requirement on filler sheets
    • e.g., window 3 black → "filler sheet 2 above 0"
    • Without a cycle, sorting them in that order displays it
  • The two items of a pair have exactly opposite requirements
    • Only pairs without a cycle are pair candidates
    • Choose greedily from the cheapest by cost (staircase-window mismatches × 3 + batch-window mismatches)
    • A rough weight: holes at staircase windows cost toggles at the tiers
  • For each order, find the ordering with the fewest operations by breadth-first search (7! = 5,040 orderings)
  • operations per order ( of them on filler sheets). from solution 3
points
Arrow A → B: filler sheet A above B

Visualizer: solution 4

  • 7 filler sheets right below the item's sheet
  • Toggles only at staircase windows 0–29

seed=0, all 1,024 orders. Enter an order number to jump
Keys: p play / pause, . / , one operation forward / back,
n / b next order / start of the current order

Solution 5: Spreading pair sheets over the tiers

  • Solution 4 packed pair sheets from the deepest tier they can go in
    • Pair sheets are used by two items, so when they gather in one tier, they bury each other and digging costs more
  • Put each pair sheet in the allowed tier with the fewest pair sheets (the deeper one on ties)
  • Choose the remaining single sheets to balance white and black in each tier (if skewed, toggling finds the target color deeper)
points
Single sheets (gray) and pair sheets (blue) in each tier, seed=0

Visualizer: solution 5

  • Pair sheets are spread over the tiers, so digging takes fewer operations

seed=0, all 1,024 orders. Enter an order number to jump
Keys: p play / pause, . / , one operation forward / back,
n / b next order / start of the current order

Solution 6: Lifting

  • Move sheet y in tier k to just above the head of tier l (l ≤ k)
    • y comes above the heads of tiers l to k → among windows l to k, those where y is opaque take y's color
  • Fixes several mismatched windows in one operation
    • Do it if (windows fixed) − (operations to dig) ≥ 2 (2 operations: lifting, and returning it at the next order)
  • → operations per order
points

Visualizer: solution 6

  • Moves a sheet of a deep tier above the head of a shallower tier, fixing several windows at once

seed=0, all 1,024 orders. Enter an order number to jump
Keys: p play / pause, . / , one operation forward / back,
n / b next order / start of the current order

Solution 7: Rebuilding the operation sequence

  • Doing return → filler sheets → dig → lift → toggle one after another can move the same sheet more than once
  • Decide only the final order of the sheets, and rebuild a sequence that moves each moved sheet once
    • Take visible sheets one by one, and move each just above the already placed sheet that is below it in the final order
    • If no visible sheet is left midway, use the original sequence
  • → operations per order
points
Before (5 operations)After (4 operations)
Sheet A (previous order's) to its tier head—
Reorder the filler sheetsReorder the filler sheets
Sheet C below sheet D (dig)Sheet C below sheet D
Sheet D to the topSheet D to the top
Sheet A below sheet B (toggle)Sheet A below sheet B

An example where sheet A of the previous order, just returned, is moved further down by a toggle. After rebuilding, sheet A goes from the top straight to below sheet B

Visualizer: solution 7

  • Within one order, each sheet is moved at most once

seed=0, all 1,024 orders. Enter an order number to jump
Keys: p play / pause, . / , one operation forward / back,
n / b next order / start of the current order

Solution 8: The order within tiers

  • Up to solution 7, a used sheet was returned to the head of its tier (recently used sheets on top)
    • Orders are independent of each other, so recent use means nothing
  • Sort by the number of items that use the sheet (use count), most first → pair sheets are shallower, so digging costs less
    • Sheets moved down from the head by toggles, and sheets returned to tiers whose windows this order's sheet covers, go to their place in use-count order
  • But keep the top two sheets of different colors → changing that window's color next time takes 1 operation
  • → operations per order
points

The final solution

  • From the top: 7 filler sheets, then tiers 0–29 (185 sheets)
    • 71 pair sheets, 114 single sheets
    • 30 staircase windows, 34 batch windows
  • Sheet design: choose pairs greedily → spread pair sheets over the tiers, balancing each tier's colors with single sheets
  • For each order: return → filler sheets → dig → lift → toggle → rebuild
  • operations per order. About 21 KB of code

Visualizer: solution 8

  • Within a tier, sheets with larger use counts are on top

seed=0, all 1,024 orders. Enter an order number to jump
Keys: p play / pause, . / , one operation forward / back,
n / b next order / start of the current order

How far AI gets on its own

Solution by AIScore (50 cases)Operations / orderhuman onlywith AICode
ChatGPT / Astra (alone, 4 hours)1,082,84421.217 KB
gpt-6.1-sol + 4 subagents (8 hours)625,01312.293 KB
+ a week of Claude / Codex subscriptions519,33910.1248 KB
The editorial's final solution (solution 8)21 KB

Ranks are equivalent ranks against the final submissions in each division (as of 17:00 on the final day)

Summary