USACO 2015 December Contest Bronze Division - Fence Painting
Problem link: here
Solution Author: Stefan Dascalescu
Problem Solution
This problem only requires simple casework where we need to consider interval intersections.
Source codes
The source codes in C++ and Python can be seen below.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ifstream cin("paint.in");
ofstream cout("paint.out");
int aL, aR, bL, bR;
cin >> aL >> aR;
cin >> bL >> bR;
if(aL > bL)
swap(aL, bL), swap(aR, bR);
if(aR < bL)
cout << aR - aL + bR - bL;
else
cout << max(aR, bR) - aL;
return 0;
}
with open("paint.in", "r") as fin:
aL, aR = map(int, fin.readline().split())
bL, bR = map(int, fin.readline().split())
if aL > bL:
aL, bL = bL, aL
aR, bR = bR, aR
if aR < bL:
ans = (aR - aL) + (bR - bL)
else:
ans = max(aR, bR) - aL
with open("paint.out", "w") as fout:
fout.write(str(ans))