If every black triangular tile were isolated, the answer would be immediate: each triangle has three sides, so we would multiply the number of black tiles by three. The only complication is that two adjacent black triangles may share a side. That shared side is counted twice in the initial total, but it needs no warning tape at all.
Trianglane is therefore a counting problem rather than a difficult geometry problem. Start with the full perimeter of every black triangle, then subtract two for every shared edge between black triangles.
Where This Fits in the CCC DSA Map
- Level: Junior J4 / Senior S1
- Pattern: Grid Adjacency, Perimeter Counting, Local Contribution
- Prerequisite: arrays/lists, loops, parity, boundary checks, running totals
- What this teaches: Break a global geometric quantity into local contributions, then remove the duplicated contribution caused by adjacent cells.
- Where it appears again: island perimeter, grid edge counting, local contribution methods, graph degree counting
What to Study Next
- Related CP Weekly problems: CCC 2021 J5/S2 Modern Art for parity counting, CCC 2024 J5 Harvest Waterloo for grid connected components, and CCC 2026 J4 Snail Path for grid simulation.
- Related Python / C++ foundations: one-dimensional lists/vectors, loops,
% 2parity checks, boundary checks, and running totals. - Related DSA roadmap article: CCC Grid Problems: from local adjacency to graph search (planned).
What the Problem Is Asking
The laneway contains two rows of equilateral triangular tiles, with C tiles in each row. Every side is one metre long. Tile orientation alternates: the first tile points upward, and every adjacent tile points in the opposite direction.
The input uses 1 for a black wet tile and 0 for a white dry tile. Bocchi needs warning tape around the outer perimeter of every wet area.
If two black triangles share a complete side, that side is inside a wet area and needs no tape. If two triangles meet only at a vertex, they are not adjacent and their perimeters do not change.
The largest official bound is C <= 200000, so we need a linear-time solution.
Where Beginners Often Get Stuck
The first trap is subtracting only one for a shared edge. Two isolated triangles contribute six sides in total. Their shared side appears once in each triangle’s contribution, so it was counted twice. Since neither copy belongs to the outside perimeter, one shared edge reduces the answer by two.
The second trap is cross-row adjacency. Horizontally adjacent tiles in the same row can share a side. Between the two rows, however, a shared side is possible only at zero-based even columns: 0, 2, 4, .... At odd columns, the two tiles meet only at a point.
The third trap is double counting. If every tile checks both its left and right neighbor, the same horizontal shared edge is found twice. Checking only each pair (i, i + 1) counts every horizontal edge exactly once.
Core Algorithm Idea
Let:
B = number of black triangles
E = number of shared edges between two black triangles
If all black triangles were separate, their total perimeter would be 3B. Every shared edge is included twice in that total but contributes nothing to the outside perimeter. Therefore:
answer = 3B - 2E
This is a local-contribution method: add the complete contribution of each unit first, then remove the internal duplication created by adjacency.
We count E in three places:
- Adjacent black pairs in the top row.
- Adjacent black pairs in the bottom row.
- Black pairs across the two rows at zero-based even columns.
Algorithm Steps
- Read
C,top, andbottom. - Compute
B = sum(top) + sum(bottom). - Initialize
tape = 3 * B. - For every
ifrom0toC - 2, subtract two iftop[i]andtop[i+1]are both black. - Do the same for the bottom row.
- For
i = 0, 2, 4, ..., subtract two iftop[i]andbottom[i]are both black. - Print
tape.
In the second official sample, there are five black triangles, so the initial perimeter is 15. There are two shared black edges, giving 15 - 2 * 2 = 11.
Python Solution
C = int(input())
top = list(map(int, input().split()))
bottom = list(map(int, input().split()))
black_tiles = sum(top) + sum(bottom)
tape = 3 * black_tiles
for i in range(C - 1):
if top[i] == 1 and top[i + 1] == 1:
tape -= 2
if bottom[i] == 1 and bottom[i + 1] == 1:
tape -= 2
for i in range(0, C, 2):
if top[i] == 1 and bottom[i] == 1:
tape -= 2
print(tape)
The code first adds the full perimeter of every black triangle. It then removes the two types of shared edges: horizontal pairs within a row and vertical pairs at even columns. Each shared edge is examined exactly once.
C++ Solution
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int C;
cin >> C;
vector<int> top(C), bottom(C);
int blackTiles = 0;
for (int i = 0; i < C; i++) {
cin >> top[i];
blackTiles += top[i];
}
for (int i = 0; i < C; i++) {
cin >> bottom[i];
blackTiles += bottom[i];
}
int tape = 3 * blackTiles;
for (int i = 0; i + 1 < C; i++) {
if (top[i] == 1 && top[i + 1] == 1) {
tape -= 2;
}
if (bottom[i] == 1 && bottom[i + 1] == 1) {
tape -= 2;
}
}
for (int i = 0; i < C; i += 2) {
if (top[i] == 1 && bottom[i] == 1) {
tape -= 2;
}
}
cout << tape << '\n';
return 0;
}
The C++ version uses the same counting logic. The condition i + 1 < C prevents an out-of-bounds access, and i += 2 visits exactly the columns where cross-row shared edges are possible.
Complexity Analysis
The input and all counting loops scan the two rows a constant number of times:
Time: O(C)
Space: O(C)
This easily handles the official maximum of 200,000 columns.
Common Mistakes
- Subtracting one instead of two for a shared edge.
- Checking cross-row adjacency in every column instead of only zero-based even columns.
- Treating triangles that meet at a vertex as if they shared a side.
- Checking both left and right neighbors and subtracting the same horizontal edge twice.
- Reading
row[i + 1]wheniis already the last column. - Guessing the perimeter from the picture without defining a countable invariant.
Review Questions
- Why does every black triangle initially contribute three?
- Why does one shared black edge reduce the answer by two?
- Why can cross-row shared edges occur only at zero-based even columns?
- How does the loop count every horizontal shared edge exactly once?
- What other grid-perimeter problems can use the same “full contribution minus internal edges” idea?
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us an email.
