== Got TLE on OJ? Here is the solution! ==
2014-02-13 14:28
357 查看
As a solo warrior in OJ, I spent about nearly 50% of my time on tackling TLE - that is innumerous hours. I almost lost my courage to OJ.But thanks to this post: http://www.spoj.com/forum/viewtopic.php?f=3&t=7968. I didn't use all of the hints, but getchar_unlocked works like charms.
As advised, cin\cout is too slow. For example, my Dijkstra using cin\cout took 25+ seconds, but after using the below code piece to get ints:
The same code ran only 11.34 seconds. Please note that, _unlocked API are not available on every platform - never mind, SPOJ has it.
And by the way, std::sync_with_stdio(false) gave segment error. I don't know why.
As advised, cin\cout is too slow. For example, my Dijkstra using cin\cout took 25+ seconds, but after using the below code piece to get ints:
#define gc getchar_unlocked int read_int() { char c = gc(); while(c<'0' || c>'9') c = gc(); int ret = 0; while(c>='0' && c<='9') { ret = 10 * ret + c - 48; c = gc(); } return ret; }
void print_fast(char *p) { fwrite_unlocked(p, 1, strlen(p), stdout); }
The same code ran only 11.34 seconds. Please note that, _unlocked API are not available on every platform - never mind, SPOJ has it.
And by the way, std::sync_with_stdio(false) gave segment error. I don't know why.
相关文章推荐
- Oracle FAQ: 未在本地计算机上注册"OraOLEDB.Oracle"提供程序 (The 'OraOLEDB.Oracle' provider is not registered on the local machine) solution
- Solution: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
- Opensack Issue & Solution: "HypervisorUnavailable: Connection to the hypervisor is broken on host"
- here is the code for MJPG video capture on ip camera
- O-MG, the Developer Preview of Android O is here!
- relying on packaging to define the extension of the main artifact has been deprecated and is schedul
- Access restriction: The type Resource is not accessible due to restriction on required library
- VMWare的The network bridge on device VMnet0 is not running故障解决
- The method encode(byte[]) from the type CharacterEncoder is not accessible due to restriction on req
- What is the difference between “setup” and “install” based on computer programs?
- 报错: The type ByteInputStream is not accessible due to restriction on required library
- The Secret to 10 Million Concurrent Connections -The Kernel is the Problem, Not the Solution
- PHP Warning: date(): It is not safe to rely on the system’s timezone settings
- Windows Mobile开发启动模拟器时,得到错误 0x80070070: There is not enough space on the disk
- Error: The project file ' ' has been renamed or is no longer in the solution
- SharePoint2007安装出现“The language is not supported on the server”
- /dev/sdb1 is apparently in use by the system; will not make a filesystem here!
- interfaceX is on channel Y, but the AP uses channel Z
- MVC project on VS2010 error : The project type is not supported by this installation
- The resource is not on the build path of a java project解决方法