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<'' || c>'') c = gc();
int ret = ;
while(c>='' && c<='') {
ret = * ret + c - ;
c = gc();
}
return ret;
}
void print_fast(char *p)
{
fwrite_unlocked(p, , 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.

== Got TLE on OJ? Here is the solution! ==的更多相关文章

  1. Leetcode OJ : Triangle 动态规划 python solution

    Total Accepted: 31557 Total Submissions: 116793     Given a triangle, find the minimum path sum from ...

  2. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  3. 【LeetCode OJ】Best Time to Buy and Sell Stock III

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...

  4. 【LeetCode OJ】Best Time to Buy and Sell Stock

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ We solve this problem ...

  5. 【LeetCode OJ】Surrounded Regions

    Problem Link: http://oj.leetcode.com/problems/surrounded-regions/ We can do follows in the 2D board. ...

  6. 【LeetCode OJ】Palindrome Partitioning II

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...

  7. 【LeetCode OJ】Palindrome Partitioning

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...

  8. 【LeetCode OJ】Valid Palindrome

    Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...

  9. 【LEETCODE OJ】Candy

    Problem link: http://oj.leetcode.com/problems/candy/ Suppose we are given an array R[1..N] that are ...

随机推荐

  1. PHP 中替换若干字符串字串为数组中的值,不用循环,非常高效

    替换某个字符串中的一个或若干个字串为数组中某些值 php本身有自带的函数,可以不用循环非常高效的实现其效果: 实例代码:   $phrase  = "You should eat fruit ...

  2. 用python处理数学问题

    一, 计算对数: >>> import math        #导入数学模块>>> math.log(8,2)     #计算以2为底 8的对数3.0>&g ...

  3. Indexof

    注释:indexOf() 方法对大小写敏感! 注释:如果要检索的字符串值没有出现,则该方法返回 -1. 实例 在本例中,我们将在 "Hello world!" 字符串内进行不同的检 ...

  4. HDU 5918 KMP/模拟

    Sequence I Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. Servlet 中文乱码问题及解决方案剖析

    转自:http://blog.csdn.net/xiazdong/article/details/7217022/ 一.常识了解 1.GBK包含GB2312,即如果通过GB2312编码后可以通过GBK ...

  6. Android——AutoCompleteTextView、Spinner和消息提示

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  7. MySQL-python模块

    1.  Python 操作 Mysql 模块的安装 linux: pip install MySQL-python 或 yum -y install MySQL-python windows: exe ...

  8. CUDA中的流与事件

    流:CUDA流很像CPU的线程,一个CUDA流中的操作按顺序进行,粗粒度管理多个处理单元的并发执行. 通俗的讲,流用于并行运算,比如处理同一副图,你用一个流处理左边半张图片,再用第二个流处理右边半张图 ...

  9. java_queue

    队列是一种特殊的线性表,先进先出(first in first out)FIFO,它只允许在表的前端(front)进行删除操作,只允许在表的后端(rear)进行插入操作. 实际应用:排队等待公交车,银 ...

  10. 开源App之MyHearts(二)

    前言 小弟技术有限,有的地方也是自己摸索出来的,可能和大神们写的好的代码没法比,但是我会努力的.要对自己说下,加油!! 此次更新 1.集成QQ登录完成 集成QQ登录网上写的介绍已经很多了,这里就不详细 ...