Posts

Showing posts with the label C

How do online judges identify Memory Limit Exceeded?

Image
Question: What is the simplest and most accurate way to measure the memory used by a program in a programming contest environment? Answer I assume you want to know how online judges determine ' Memory Limit Exceeded ' for a user-supplied program. First you may want to read the general architecture of online judges at  TopCoder: How do online judges identify Time Limit Exceeded? where the functioning of online judges has been explained in a very basic way and how they determine  TLE - Time Limit Exceeded  for a program. Here again the the resource limits come to rescue. But the more useful thing that stand by our side is  proc - a file system that contains process information. proc is the key to determining memory usage by a program. It is an interface to kernel data structures and is usually mounted at /proc in a UNIX-based machine. See `man proc` for more detail. For almost every process running in the system, there is a directory created in /p...

How do online judges identify Time Limit Exceeded?

Question :   I have to design C++ code that calls a function. But I must not run that function for more than 2 seconds.   Answer Slow down young man, and let's proceed steadily. Shall we? Now what exactly you want to achieve is unclear here. Do you simply want to measure the execution time of the function? Do you want to terminate the program after the time limit is exceeded? Are you calling the function in a child process? Is your process running as a privileged user?The implementation obviously depends on what you want to do and what environments you are initializing the process with. Nevertheless, there are few different ways. I will get to them one by one with simplest first. 1. Measuring time You can easily measure the time (clock time & CPU time) taken to execute a function using clock() and time() functions available in the header <time.h>. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <stdio.h>...