give ans code in cpp
Erstellt am: 12. Juli 2025
Erstellt am: 12. Juli 2025
pythonfrom PIL import Image import matplotlib.pyplot as plt img = Image.open('/mnt/data/0e070c34-1a0c-4c17-ac4a-149f92c38ff7.jpg') plt.imshow(img) plt.axis('off') (-0.5, 719.5, 1279.5, -0.5)
pythonimport numpy as np # crop approximate area around center where text is h,w = img.size[1], img.size[0] # Actually img.size returns width,height w, h = img.size # crop top area where text lines crop = img.crop((0, 120, w, 600)) plt.imshow(crop) plt.axis('off') (-0.5, 719.5, 479.5, -0.5)
pythoncrop2 = img.crop((0, 500, w, 800)) plt.imshow(crop2) plt.axis('off') (-0.5, 719.5, 299.5, -0.5)
python# manually inspect crop2 section coordinates to see text numbers left half crop2_left = img.crop((0, 520, 350, 850)) plt.imshow(crop2_left) plt.axis('off') (-0.5, 349.5, 329.5, -0.5)
pythonimg2 = Image.open('/mnt/data/29e3df6f-326a-4ef0-9000-61a875303c41.jpg') plt.imshow(img2) plt.axis('off') (-0.5, 719.5, 1279.5, -0.5)
pythonplt.imshow(img2.crop((0, 100, 550, 450))) plt.axis('off') (-0.5, 549.5, 349.5, -0.5)
pythonimg3 = Image.open('/mnt/data/4afb3e1d-01ac-4585-b3c5-0d4c0e55dec9.jpg') plt.imshow(img3) plt.axis('off') (-0.5, 719.5, 1279.5, -0.5)
pythonplt.imshow(img3.crop((0, 560, 550, 840))) plt.axis('off') (-0.5, 549.5, 279.5, -0.5)
C++ solution (axis-aligned squares, O(N²)):
cpp#include <bits/stdc++.h> using namespace std; struct PairHash { size_t operator()(const pair<int,int>& p) const noexcept { return (static_cast<uint64_t>(p.first) << 32) ^ static_cast<uint32_t>(p.second); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; if(!(cin >> N) || N < 4) return 0; // no points ⇒ no output vector<pair<int,int>> pts(N); unordered_set<pair<int,int>, PairHash> S; // constant-time lookup for (int i = 0; i < N; ++i) { cin >> pts[i].first >> pts[i].second; S.insert(pts[i]); } int best = INT_MAX; // smallest side found so far /* Enumerate every horizontal segment (two points with the same y). Treat it as the *bottom* edge, then also as the *top* edge (dir = ±1). */ for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { if (pts[i].second != pts[j].second) continue; // not horizontal int side = abs(pts[i].first - pts[j].first); // length of the edge if (side == 0) continue; // same point for (int dir : {+1, -1}) { // square above & below int newY = pts[i].second + dir * side; if (S.count({pts[i].first, newY}) && S.count({pts[j].first, newY})) { best = min(best, side); } } } } /* If at least one square was found, best < INT_MAX */ if (best != INT_MAX) cout << best << '\n'; else cout << -1 << '\n'; // or handle as per problem-setter’s spec return 0; }
unordered_set
for O(1) existence checks.side = |x₂ – x₁|
is a candidate side length.(x₁, y ± side)
and (x₂, y ± side)
.O(1)
with the set; if present,
update the current minimum side.-1
if no square exists).With N ≤ 100 the double loop costs at most 4 995 checks, easily within limits.