## Problem Summary:

**Problem Number:** 1223

**Problem Name:** Toboggan of Marbles

**Author’s Name:** Maratona de Programação da SBC Brazil

**Timelimit:** 1

**Problem Category:** AD-HOC

**Problem Source:** https://www.urionlinejudge.com.br/judge/en/problems/view/1223

## Some Talks about Contest Programming:

An incredible method to enhance your abilities when figuring out how to code is by solving coding problems. Solving different kinds of challenges and riddles can enable you to improve as a problem solver, take in the complexities of a programming dialect, get ready for prospective job interviews, learn new algorithms and more.

An online judge is an online platform to test programs in focused programming challenges. They are likewise used to practice for such challenges. A considerable amount of these platforms also arrange their own programming contests.

## 10 Steps to Solve Any Problems:

- Read the problem completely at least two or three times (or however many makes you feel comfortable)
- Identify the subject, the problem belongs to. Is it a sorting or pattern matching problem? Can I use graph theory? Is it related to number theory? etc.
- Try to solve the problem manually by considering 3 or 4 sample data sets.
- After concentrate on optimizing the manual steps. Try to make it as simple as possible.
- Write to write pseudo-code and comments besides the code from the manual steps. One thing you can do is to check after every function is written. Use a good IDE with a debugger, if possible. Don’t need to think much about the syntax. Just focus on the logic and steps.
- Replace the comments or pseudo-code with real code. Always check if the values and code are behaving as expected before moving to the new line of pseudo-code.
- Then optimize the real code.
- Take care of boundary conditions as well.
- Get feedback from your teammates, professors, and other developers and also ask your question on Stack Overflow if possible. Try to learn from others’ guidelines and what they are handling those problems. A problem may be solved in several ways. So, don’t get disappointed if you can’t think like an expert. You need to stick to the problem and you will gradually become better and quicker in solving problems like others.
- Practice, Practice, and Practice.

**N.B: Try to follow the above steps always. If you still can’t get the problem solved, take a look at the solution below. Don’t just copy paste the code. It will kill your creativity. Try to enjoy contest programming and develop your skills.**

## Solution:

#include <bits/stdc++.h> #define EPS 0.000000001 #define INF 2000000000.000000 using namespace std; struct point { int x, y; point() {x = y = 0.0;} point(int _x, int _y) : x(_x), y(_y) {} bool operator < (const point &other) const { if (x != other.x) return x < other.x; return y < other.y; } bool operator == (const point &other) const { return x == other.x && y == other.y; } bool operator <= (const point &other) const { return ((*this < other) || (*this == other)); } int operator %(const point& q) { return x * q.y - y * q.x; } }; struct segment { point first; point second; segment() {first = point(); second = point();} segment(point a, point b) {first = a; second = b;} }; inline int cmp(int x, int y=0) { return (x <= y) ? (x < y) ? -1 : 0 : 1; } double dtos(point r, point p, point q) { point A = point(r.x - q.x, r.y - q.y), B = point(r.x - p.x, r.y - p.y), C = point(q.x - p.x, q.y - p.y); int a = A.x * A.x + A.y * A.y, b = B.x * B.x + B.y * B.y, c = C.x * C.x + C.y * C.y; if (cmp(b, a+c) >= 0) return sqrt(a); if (cmp(a, b+c) >= 0) return sqrt(b); return abs(A%B) / sqrt(c); } int main() { vector<segment> v; int xf, yf, yi; int n; int l, h; point parede1c, parede2c, parede1f, parede2f; while (cin >> n) { cin >> l >> h; parede1c = point(0, h); parede1f = point(0, 0); parede2c = point(l, h); parede2f = point(l, 0); for (int i = 0 ; i < n ; ++i) { cin >> yi >> xf >> yf; if (i & 1) v.push_back(segment(point(l, yi),point(xf, yf))); else v.push_back(segment(point(0, yi),point(xf, yf))); } int n1 = v.size(); double ans; if (n & 1) ans = dtos(v[n1 - 1].second, parede2c, parede2f); else ans = dtos(v[n - 1].second, parede1c, parede1f); cout << "Ate agora tenho " << ans << 'n'; int cont = 0; for (int i = 0 ; i < n - 1 ; ++i, ++cont) { ans = min(ans, dtos(v[i].second, v[i + 1].first, v[i + 1].second)); if (cont & 1) ans = min(dtos(v[i].second, parede2c, parede2f), ans); else ans = min(dtos(v[i].second, parede1c, parede1f), ans); cout << "Ate agora tenho " << ans << 'n'; } cout << setprecision(2) << fixed << ans << 'n'; v.clear(); } }

**N.B.: Code is Collected from Different Sources**

- How to Create a RGB Color Picker for Images using OpenCV Python - 22 April, 2022
- Combine Several Images Vertically with Padding using OpenCV Python - 21 April, 2022
- Combine Several Images Horizontally with Padding using OpenCV Python - 21 April, 2022