code in cpp
作成日: 2025年7月12日
作成日: 2025年7月12日
pythonfrom PIL import Image import matplotlib.pyplot as plt img1 = Image.open('/mnt/data/a5431805-e290-43b0-87f4-4d0397195695.jpg') img1.size (720, 1280)
pythonplt.imshow(img1) plt.axis('off') (-0.5, 719.5, 1279.5, -0.5)
pythonimg2 = Image.open('/mnt/data/e3f20efe-ea8e-4237-8749-2cb85ed0d1e7.jpg') plt.imshow(img2); plt.axis('off') (-0.5, 719.5, 1279.5, -0.5)
pythonplt.figure(figsize=(6,10)) plt.imshow(img2.crop((0,0,720,640))) plt.axis('off') (-0.5, 719.5, 639.5, -0.5)
pythonimg4 = Image.open('/mnt/data/47e5930e-68a8-4eb2-bf2d-46aa496ca2b7.jpg') plt.imshow(img4); plt.axis('off') (-0.5, 719.5, 1279.5, -0.5)
pythonplt.figure(figsize=(6,10)) plt.imshow(img4.crop((0,0,720,700))) plt.axis('off') (-0.5, 719.5, 699.5, -0.5)
pythonplt.figure(figsize=(6,10)) plt.imshow(img2.crop((0,640,720,1280))) plt.axis('off') (-0.5, 719.5, 639.5, -0.5)
pythonimg3 = Image.open('/mnt/data/9c8c0d8f-a19c-4a6c-8ea4-4de86f257692.jpg') plt.imshow(img3); plt.axis('off') (-0.5, 719.5, 1279.5, -0.5)
pythonplt.figure(figsize=(7,10)) plt.imshow(img3.crop((0,0,720,500))) plt.axis('off') (-0.5, 719.5, 499.5, -0.5)
pythonplt.figure(figsize=(7,10)) plt.imshow(img4.crop((0,0,720,400))) plt.axis('off') (-0.5, 719.5, 399.5, -0.5)
pythonplt.figure(figsize=(7,10)) plt.imshow(img4.crop((0,350,720,750))) plt.axis('off') (-0.5, 719.5, 399.5, -0.5)
pythonplt.figure(figsize=(4,8)) plt.imshow(img3.crop((100,400,400,780))) plt.axis('off') (-0.5, 299.5, 379.5, -0.5)
pythonplt.imshow(img2.crop((100,400,400,780))); plt.axis('off') (-0.5, 299.5, 379.5, -0.5)
Below is a compact O(N) solution that follows exactly what the statement (and the diagrams) describe.
cpp#include <bits/stdc++.h> using namespace std; /* * n – number of logs * x – max steps she can walk **left** on a log * y – max steps she can walk **right** on a log * a – lengths of the logs, in the order she encounters them * * Returns the total number of jumps Geeta makes before she either * • reaches the opposite bank, or * • is forced to turn around and return to the start. */ long long stepsNeeded(int n, long long x, long long y, const vector<long long>& a) { long long jumps = 1; // first jump: start bank → log 0 for (int i = 0; i < n - 1; ++i) { long long diff = a[i + 1] - a[i]; // horizontal walk on log i if (diff > 0) // need to walk to the **right** { if (diff > y) // beyond her capability → must retreat return jumps * 2; // go back the way she came } else if (diff < 0) // need to walk to the **left** { if (-diff > x) // beyond her capability → must retreat return jumps * 2; } ++jumps; // successful jump to log (i+1) } // All logs cleared – one final jump to the far bank return jumps + 1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; long long x, y; if (!(cin >> n >> x >> y)) return 0; vector<long long> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; cout << stepsNeeded(n, x, y, a) << '\n'; return 0; }
Forward traversal
For every pair of consecutive logs we compute how far Geeta has to shuffle horizontally on the current log to align with the next log’s right-end:
diff = a[i+1] - a[i]
diff > 0
→ walk right diff
steps (must be ≤ y
).diff < 0
→ walk left |diff|
steps (must be ≤ x
).If the walk is within her capability she performs the jump (so jumps++
) and continues; otherwise she cannot progress.
Retreat rule
The moment a required horizontal walk exceeds her limit, she turns back.
All jumps she made so far are perfectly reversible (they already satisfied the limits in the opposite direction), so the retreat costs exactly the same number of jumps.
Hence we return 2 × (current forward jumps)
.
Success
If every gap satisfies the limits, she stands on the last log; one extra leap gets her onto the far bank, so we add +1
.
Sample 1
N=5, X=3, Y=5, a=[1,6,5,4,1]
All horizontal walks (5 R, 1 L, 1 L, 3 L) fit the limits ⇒ total jumps = 5 logs + 1
= 6.
Sample 2
N=5, X=1, Y=5, a=[1,6,5,4,1]
Fourth gap needs 3 L > X, so she retreats:
forward jumps = 4 → answer = 2 × 4
= 8.