Topcoder SRM 479 "TheCoffeeTimeDivOne"

問題:http://www.topcoder.com/stat?c=problem_statement&pm=11028 (要ログイン)

とりあえず貼っとく.
冗長なのは気にしない.
DivTwoのテキトーなやつと比べると,最初の配列確保でメモリが確保できないので
配列を確保せずにやる方法をテキトーに実装.

import java.util.Arrays;

public class TheCoffeeTimeDivOne {

	public long find(int n, int[] tea) {
		Arrays.sort(tea);
		long time = 0;
		int i = n, c=0,tmp=0,p=tea.length-1;
		for(; i > 0; --i){
			if(tea[p<0?0:p]!=i){
				if(++c==1){
					tmp = i;
				}else if(c==7){
					time+=47+2*tmp+c*4;
					c=0;
				}
			}else{
				--p;
			}
		}
		if(0<c&&c<7){
			time+=47+2*tmp+c*4;
		}
		return f(tea)+time;
	}

	private long f(int[] tea) {
		long time = 0;
		for(int i = tea.length-1; i >= 0;){
			int j = Math.max(0, i-7+1);
			time += 47+2*(tea[i])+(i-j+1)*4;
			i = j-1;
		}
		return time;
	}

}