Editing
2015 Dec Bronze Problem 1 Fence Painting
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Code == === C++ === <pre> #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin("paint.in"); ofstream fout("paint.out"); int segment1Start, segment1End; fin >> segment1Start >> segment1End; int segment2Start, segment2End; fin >> segment2Start >> segment2End; int paintedLength; if(segment2Start >= segment1End) { paintedLength = (segment1End - segment1Start) + (segment2End - segment2Start); } else if(segment2End > segment1End) { paintedLength = segment2End - segment1Start; } else { paintedLength = segment1End - segment1Start; } fout << paintedLength << endl; return 0; } </pre> === Java === <pre> import java.io.*; import java.util.*; public class Paint { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("paint.in")); PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("paint.out"))); StringTokenizer st = new StringTokenizer(br.readLine()); int segment1Start = Integer.parseInt(st.nextToken()); int segment1End = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int segment2Start = Integer.parseInt(st.nextToken()); int segment2End = Integer.parseInt(st.nextToken()); int paintedLength; // Checking overlap scenarios if(segment2Start >= segment1End) { // No overlap paintedLength = (segment1End - segment1Start) + (segment2End - segment2Start); } else if(segment2End > segment1End) { // Partial overlap with segment2 exceeding segment1 paintedLength = segment2End - segment1Start; } else { // Full overlap where segment2 is completely inside segment1 paintedLength = segment1End - segment1Start; } pw.println(paintedLength); pw.close(); } } </pre> === Python === <pre> with open('paint.in', 'r') as fin: a, b = map(int, fin.readline().strip().split()) c, d = map(int, fin.readline().strip().split()) if c < a: a, c = c, a b, d = d, b if c >= b: painted_length = (b - a) + (d - c) elif d > b: painted_length = d - a else: painted_length = b - a with open('paint.out', 'w') as fout: fout.write(str(painted_length) + '\n') </pre> [[Category:Yearly_2015_2016]] [[Category:Bronze]] [[Category:Intervals]] [[Category:Sorting]]
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information