Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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:

#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.

posted on 2014-02-13 14:28  Tonix  阅读(251)  评论(0编辑  收藏  举报