== Got TLE on OJ? Here is the solution! ==
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! ==的更多相关文章
- Leetcode OJ : Triangle 动态规划 python solution
Total Accepted: 31557 Total Submissions: 116793 Given a triangle, find the minimum path sum from ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【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 ...
- 【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 ...
- 【LeetCode OJ】Surrounded Regions
Problem Link: http://oj.leetcode.com/problems/surrounded-regions/ We can do follows in the 2D board. ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LeetCode OJ】Palindrome Partitioning
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...
- 【LeetCode OJ】Valid Palindrome
Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...
- 【LEETCODE OJ】Candy
Problem link: http://oj.leetcode.com/problems/candy/ Suppose we are given an array R[1..N] that are ...
随机推荐
- android内嵌入webview导致闪退
这里碰到的是各种闪退情况之一,webview退出后,程序里立马需要申请内存空间做别的事情,这时内存不够就会闪退,做法就是延时个几百毫秒,在这段时间内让java把该回收的内存都回收,然后延时到了再做接下 ...
- cocos2d-html5如何更改预加载时的默认的logo图片和删除loading小圈圈
找到框架目录(2.1.4)里的cocos2d目录里有个CCLoader.js文件,找到LoaderScene的构造方法ctor,可以看到有一行: this._logoTexture.src= &quo ...
- 转 SQL Server中关于的checkpoint使用说明
在SQL Server中有一个非常重要的命令就是CheckPoint,它主要作用是把缓存中的数据写入mdf文件中. 其实在我们进行insert, update, delete时,数据并没有直接写入数据 ...
- iOS学习笔记---oc语言第八天
属性 能在一定程度上简化代码,并且增强实例变量的访问安全性 属性的声明:使用@property声明属性 eg:@property NSSstring *name;相当于@interface中声明了两 ...
- Apache CXF 102 CXF with REST
前言 续上篇Apache CXF 101,摘抄部分REST概念性知识,以运行实例考察CXF对REST的支持. 目录 1 REST简介 2 工具 3 运行实例 内容 本Spike记录中内容,如无特别指出 ...
- clientX,screenX,pageX,offsetX的异同 【转载】
首先说明一下以上对象都是指javascript中的,不包含其他语言. pageX/pageY: 鼠标相对于整个页面的X/Y坐标.注意,整个页面的意思就是你整个网页的全部,比如说网页很宽很长,宽2000 ...
- activiti工作流数据库表详细介绍 (23张表)
Activiti的后台是有数据库的支持,所有的表的表名都以ACT_开头,表名的第二部分是用来表示表的用途的两个字母标识. 用途也和服务的API对应. ACT_RE_*: 'RE'表示repositor ...
- 查看PHP的配置信息
查看PHP的配置信息其实一个函数就搞定了. 首先在服务器的根目录建立phpinfo.php文件. 然后打开此文件输入以下内容 <?php phpinfo(); ?> 保存此文件之后在浏览器 ...
- 共享内存+互斥量实现linux进程间通信 分类: Linux C/C++ 2015-03-26 17:14 67人阅读 评论(0) 收藏
一.共享内存简介 共享内存是进程间通信中高效方便的方式之一.共享内存允许两个或更多进程访问同一块内存,就如同 malloc() 函数向不同进程返回了指向同一个物理内存区域的指针,两个进程可以对一块共享 ...
- 【python2.7】raw_input()和input()区别及用法
版权声明:本文为博主原创文章,未经博主允许不得转载. 一.函数介绍1. input([prompt]) 等同于eval(raw_input([prompt])),这个函数不会捕捉用户输入上的错误,如果 ...