heap sort

heap sort, 参考wikipedia

void sift_down(int *a, int start,int end){
	int i=start;
	while(i*2+1<=end) {
		int child=i*2+1;
		if (child<end && a[child]<a[child+1])
			child++;
		if (a[i]<a[child]){
			std::swap(a[i],a[child]);
			i=child;
		}
		else return;
	}
}

void heapify(int *a,int n){
	for (int i=(n-2)/2;i>=0;i--) {
		sift_down(a,i,n-1);
	}
}

void heap_sort(int *a, int n){
	heapify(a,n);
	for (int i=n-1;i>0;i--) {
		std::swap(a[0],a[i]);
		sift_down(a,0,i-1);
	}
}

Comments(0)

程序员的自我修养 preface

演员的自我修养

从今天开始, 我会post一些编程基础, 经典算法, 算法题目, 和编程tricks&tips.

我将这个叫做《程序员的自我修养》.

Comments(0)

Windows 7 disabled script host??

自从装了windows 7, 就没有成功用过我写的windows script. 一些控制iTunes的js. 今天终于忍不住了, 网上搜了一番, 发现一个解决办法,

http://www.winhelponline.com/articles/230/1/Error-There-is-no-script-engine-for-file-extension-when-running-js-files.html

基本思想是, 由于js文件在注册表里缺失了相应的class, 于是windows不知道该如何处理js这个文件. 不知道是不是windows 7出于安全的考虑, 禁止了.

解决办法是,

1. 用管理员模式运行  regsvr32 %systemroot%\system32\jscript.dll
2. 下载这个注册表文件, 并运行.

即可.

Tags:

Comments(0)

sizeof empty struct in C

An interesting question, what is the size of an empty struct in C/C++?

This question was discussed here.

I did a research and found,

Visual Studio 2008 (compile as C) Not allowed. Prompt that C requires Struct or unoin has at least one member.
Visual Studio 2008 (compile as C++) 1
GCC 0 (sizeof should be followed by a variable, instead of a struct name)
G++ 1

Summary: In C++, empty struct is size 1. In C, it is somehow meaningless. 
Actually, struct is treated as class in C++ and so empty struct is also allowed. Without any member or functions, the size of a class/struct is 1.

Tags:

Comments(0)

还招行信用卡美元账单

当信用卡有美元帐的时候, 你可以选择用美元还款或者rmb购汇还款. 倘若用美元还款, 那么有多少钱还多少钱, 谁都不吃亏. 若用rmb还款, 就要选择汇率好的时间点还. 一般账单日到还款日之间还有半个多月时间, 这期间可以选一个汇率划算的时间, 用rmb直接还掉. 还的方法是, 先通过一卡通同城转账到信用卡(不要直接还款),或者到网点存款到信用卡(也是转账类型,不是还款), 这样信用卡账目上就有多余的rmb, 银行管它叫做溢缴款. 然后在网上信用卡大众版或者专业版里的信用卡里, 找到 还款管理->自动购汇设置->本期溢缴款购汇, 有个申请溢缴款购汇. 这样你的溢缴款都会在当天晚上按照当天的汇率购汇还上信用卡的美元部分. 这个操作要在下午2点之前操作, 否则将会按照次日的牌价进行. 汇率要参考招行给出的现汇卖出价, 和中间价差的不多, 还比较划算.

这里顺道bs下新加坡的citibank和dbs, 信用卡直接是以新币记账, 并且汇率极差. 1.40左右的中间价, 记账1.42~14.3多. 同时不让用美元还款. 只能新币还.  citibank有个更恶心的地方. 住酒店时候,酒店要冻结一些资金作为押金. citibank居然把冻结算做消费,解冻时候算还款, 一进一出, 收了两笔手续费, 100美元的押金, 花了近10新币的手续费.

Tags:

Comments(0)

新软件们

曾经订阅cnbeta和其他一些软件论坛, 不断跟进各个软件的更新状态. 可是随着人渐渐老了, 跟进出现颓势. 最近更新了下lightroom和live套装, 感觉不错, 这里随口说两句.

Lightroom 3. 

从2到3的更新挺大的. 添加了很多新功能. 比如noise reduction, lens distortion correction, vegetating correction, 加上flickr的互动等等. 这些都是曾经非常期盼的. 现在加上之后, 只是觉得太舒服了, 好像一直都在一样.  另一个很大的体验是速度的提升, 之前我用的时候一直出现lag, 现在速度已经基本满意了. LR也可以配合一些插件直接上传facebook, picasaweb, flickr了, 那么我也不需要再先将照片的低质量版本导入到iphoto里了.

前一段时间我还尝试着使用了下aperture 3, 用的挺别扭的, 速度也不快. 当时看到他说的很多feature比较吸引我, geotag, face recognition等等, 想着用了它就直接的把iphoto给取代了. 可惜, 它并没有使得我背叛了LR. 当新的LR装上后, 我首先做的事情是, 把aperture里的少量测试的几组照片又倒回了LR, 并决心继续使用LR了.

LR还有一个优点是, Windows 和 MacOS都有. 试想如果家里组建一个media station, 有时候用mac有时候用windows, 都能看和管理照片库.

新的Live Messenger 及 Writer.

在别人blog看到writer的新beta版, 就装了一下试试, 整个界面改成了office 2007的那种样子, 没觉得好或者不好,大家一致我也没意见.

同时我也装了messenger的beta版, 没想到这次革新比较大. 之前对messenger一直也不感冒. 可能是因为没期待, 反而得到了点惊喜. 新版的messenger分为full view和compact view. compact view就是原来的那个样子. full view比较宽, 里面整合了live社区的各个功能: live space(blog)以及状态更新(微薄), 还有skydrive的照片. 用户可以直接在msgr上做comment和reply, 速度快, 使用方便. 此外, MS居然还很大气的把facebook整合进来了(哦, 我忘记了MS之前已经花了一大笔钱买了facebook的9点几百分比的股票呢), 挺不错的. 直接可以看Facebook的照片, 评论, wall, 也可以回复. 算是个facebook的半个客户端了. 我就是很喜欢本地程序, 用户体验好. 现在live msgr作为工作之余网上闲逛挺好用的(闲逛的地方又多了一个-.-) 

下载地址http://explore.live.com/windows-live-essentials-beta

Tags:

Comments(0)

how to tether blackberry to Mac

0, download bb modem script here, http://www.fibble.org/archives/2007/10/updated-modem-s.html
and put it to /Library/Modem scripts/
1. pair your bb with your mb, at the last step, use the info as below to setup the phone modem.
2. create new bluetooth connection in network pannel of system configuration. Fill the blanks on the right with the below info again.
3. go to advanced, Choose the vonder as RIM, and Model as 8800, Dial mode: ignore dial tone while dialing,

T-Mobile:
phone number: wap.voicestream.com
username: guest
password: guest
At&t:
Username: WAP@CINGULARGPRS.COM
Password: CINGULAR1
Phone Number: wap.cingular

refer to this link for more details instruction, http://theappleblog.com/2009/06/24/how-to-tether-your-blackberry-to-your-mac/

Tags: ,

Comments(0)

ice skating @ kallang leisure park

今天心血来潮去溜冰,本想去jurong east的那家,网上搜了下,发现现在大家都去的是kallang的一家,在kallang leisure park。这家是2007年开的,最早只有专业服务,后来对大众开放。因为这家距离我家很近,所以完全没必要去jurongeast那边了。又一查,没想到今天是circle line从paya lebar到dhoby Ghaut开始运行的第一天。 就和mm从paya lebar坐车到stadium站。出了stadium站一转身就是kallang leisure park了。另外一边就是indoor stadium了。 貌似今天是SHE和五月天的演唱会。 我们居然也跑过来凑热闹了。

ice skating在三楼。大小和jurong east的那家差不多,价格和开放时间看下表。

price

我们就用了便宜那种的鞋子,鞋子也还比较新,场馆维护也不错。下次打算试一试那个hockey,可能刃会更利一点把。
我们自己带了手套和袜子,不然需要买他们的。
因为mm不太会,大多时间都在教她,最后她也学的差不多了,基本可以自己滑了。 很有成就感。
我很久没滑了,滑了几圈去复习。基本恢复到以前的水平了。 只是年纪老了,不敢蹦蹦跳跳了。就这,有一次摔的比较结实,脖子被蹲到了,现在仍很痛。
玩了两个小时,也已经累的不行了,而冰场开始铲冰了。就闪人了。比较可笑的是,那个铲冰机和plants vs zombies里的铲冰机长真是一模一样。没带相机,没法上片子了。
走的时候,听到那边stadium里狂吼的声音,仔细辨认,是五月天的。
看到mrt的office里有很多工作人员,大家都在等散场然后疏通管理人流。

几个tips

  1. 可以自带手套和袜子,省点钱,但这两样是必须的。手套是防止手撑地的时候被冰划伤
  2. 要穿长裤,长袖衣服,和手套同理。
  3. 若有护膝可以戴上,摔在冰上,膝盖很受不了(老了?)
  4. 带水,里面没看到买水的。只有投币买杯装饮料的。

Comments(0)

two latex tips

1. how to use symbol (~) in latex?  simple, just use \textasciitilde.  Somebody use $\sim$, that’s not correct. if you  copy it in Acrobat Reader and paste it out, you will find that’s another char.

2. how to use right-alignment and top-alignment in a tabular. Here is how: http://texblog.wordpress.com/2008/05/07/fwd-equal-cell-width-right-and-centre-aligned-content/
to summarize, define:\newcolumntype{x}[1]{>{\raggedleft\hspace{0pt}}p{#1}}  and use it in tabular \begin{tabular}{|l|x{4.5cm}|x{4.5cm}|}. but you cannot use \\ to end lines. Instead, use \tabularnewline.

Tags:

Comments(0)

how to do photo matting in photoshop CS4

There used to be a plugin named Extract. but it is not by default installed. so download optional installation @ http://www.adobe.com/support/downloads/thankyou.jsp?ftpID=4047&fileID=3768

installation is simple , copy the plugin file to the plugin folder->filter of photoshop application.

after that,  open a photo and choose extract from manu under filter.  use highlight tool to draw the boundaries and the use fill tool to fill the foreground, thereafter click ‘Ok’..  job is done. Note: donot draw too wide boundary using highlight tool, the narrow it is, the better your matting is.

Although there is quite a lot research on photo matting and they have much better result. The photoshop one is still very easy to use and fast.

If you cannot get the matting properly, please read this instruction with figure: http://www.photoshopcafe.com/tutorials/cutout/cutout.htm

Tags:

Comments(0)