3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

paiza POH ec-campaign #paizahack_01 [JavaScript]

Last updated at Posted at 2014-04-17
問題 https://paiza.jp/poh/ec-campaign
タイム一覧/C++ http://qiita.com/cielavenir/items/a61cfe8390eb16866ae5
Python/Ruby(1) http://qiita.com/cielavenir/items/b10ff4d201150f525062
C#/Java/Python/Ruby http://qiita.com/cielavenir/items/d89e85f069cf570e6786
Perl/PHP http://qiita.com/cielavenir/items/1a650a4c41d7bdd31392
JavaScript http://qiita.com/cielavenir/items/a85b985888fdc15c52b7
Go/CoffeeScript/Scala/R/Bash http://qiita.com/cielavenir/items/79016a0afd30470f440d
VB/F#/D http://qiita.com/cielavenir/items/cb6094bab56253de992c

JavaScript (0.34s)

#!/usr/bin/node
(function(){
	var T=[];
	var stdin = process.openStdin();
	stdin.setEncoding('utf8');

array_binarysearch=function(needle,haystack,size){
    var high = size-1;
    var low = 0;
    var ret = size;
    while( low <= high ){
        var probe = (high + low) / 2^0;
        var comparison = haystack[probe]-needle;
        if( comparison <= 0 ){
            low = probe+1;
        }else{
			ret=high;
            high = probe-1;
        }
    }
	return ret;
}

	var input_fragment="";
	stdin.on('data', function(input) {
		var i=0,len,ref,x=[];
		ref=(input_fragment+input).split("\n");
		input_fragment=ref.pop();
		for(len=ref.length;i<len;i++){
    		if(ref[i]=='')continue;
    		x.push(ref[i]);
		}
		T=T.concat(x);
	});

	stdin.on('end', function(z) {
		if(input_fragment){
			var i=0,len,ref,x=[];
			ref=(input_fragment+"\n").split("\n");
			input_fragment=ref.pop();
			for(len=ref.length;i<len;i++){
    			if(ref[i]=='')continue;
    			x.push(ref[i]);
			}
			T=T.concat(x);
		}

		var arg=T[0].split(' ').map(Number);
		var n=arg[0];
		var d=arg[1];
		var v=T.slice(1,1+n).map(Number).sort(function(a,b){return a-b;});

		for(var i=0;i<d;i++){
			var m=Number(T[1+n+i]);
			var idx=array_binarysearch(m-v[0],v,n);
			var r=0;
			var j=0;
			for(var k=idx-1;j<k&&v[j]+v[j+1]<=m;j++){
				for(;v[j]+v[k]>m;)k--;
				if(r<v[j]+v[k]){
					r=v[j]+v[k];
					if(r==m)break;
				}
			}
			console.log(r);
		}
	});
})();
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?