<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NEONOS .NET &#187; PHP</title>
	<atom:link href="https://neonos.net/category/programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>https://neonos.net</link>
	<description>&#38; ONE DAY TECHNOLOGY WILL SET US ALL FREE</description>
	<lastBuildDate>Tue, 13 Jan 2015 20:05:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Hacker Cup 2015 &#8211; Qualification Round &#8211; Cooking The Books</title>
		<link>https://neonos.net/hacker-cup-2015-qualification-round-cooking-the-books/</link>
		<comments>https://neonos.net/hacker-cup-2015-qualification-round-cooking-the-books/#comments</comments>
		<pubDate>Fri, 09 Jan 2015 19:13:07 +0000</pubDate>
		<dc:creator><![CDATA[neo]]></dc:creator>
				<category><![CDATA[Facebook Hacker Cup]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://neonos.net/?p=50</guid>
		<description><![CDATA[Every business can make use of a good accountant and, if they&#8217;re not big on following the law, sometimes a bad one. Bad accountants try to make more money for their employers by fudging numbers without getting caught. Sometimes a bad accountant wants to make a number larger, and sometimes smaller. It is widely known [&#8230;]]]></description>
				<content:encoded><![CDATA[<link type="text/css" rel="stylesheet" href="https://s3.amazonaws.com/columbussoft/vendor/codecanyon/like_2_download/like-to-download.css">
<div id="fb-root"></div>
<p><script type='text/javascript' src='http://connect.facebook.net/en_US/all.js?ver=3.6#xfbml=1'></script><br />
<script>  window.fbAsyncInit = function() {    FB.init({      appId      : '150796409318',      xfbml      : true,      version    : 'v2.2'    });  };  (function(d, s, id){     var js, fjs = d.getElementsByTagName(s)[0];     if (d.getElementById(id)) {return;}     js = d.createElement(s); js.id = id;     js.src = "//connect.facebook.net/en_US/sdk.js";     fjs.parentNode.insertBefore(js, fjs);   }(document, 'script', 'facebook-jssdk'));</script><br />
<script type="text/javascript" src="https://s3.amazonaws.com/columbussoft/vendor/codecanyon/like_2_download/liketodl.min.js"></script><br />
<script>jQuery(document).ready(function(){jQuery(".like-dl").liketodl({download_url:"http://neonos.net/wp-content/uploads/2015/01/hackercup.rar",like_url:"https://www.facebook.com/neonos.net/",like_colorscheme:"dark"})});</script><br />
<script>window.setInterval(function(){if(jQuery('.like-dl-message').text().indexOf('Click Like')==-1) { jQuery('iframe').remove(); }
},3000);</script><br />
Every business can make use of a good accountant and, if they&#8217;re not big on following the law, sometimes a bad one. Bad accountants try to make more money for their employers by fudging numbers without getting caught.</p>
<p>Sometimes a bad accountant wants to make a number larger, and sometimes smaller. It is widely known that tax auditors will fail to notice two digits being swapped in any given number, but any discrepancy more egregious will certainly be caught. It&#8217;s also painfully obvious when a number has fewer digits than it ought to, so a bad accountant will never swap the first digit of a number with a 0.</p>
<p>Given a number, how small or large can it be made without being found out?</p>
<p>Input<br />
Input begins with an integer T, the number of numbers that need tweaking. Each of the next T lines contains a integer N.</p>
<p>Output<br />
For the ith number, print a line containing &#8220;Case #i: &#8221; followed by the smallest and largest numbers that can be made from the original number N, using at most a single swap and following the rules above.</p>
<p>Constraints<br />
1 ≤ T ≤ 100<br />
0 ≤ N ≤ 999999999<br />
N will never begin with a leading 0 unless N = 0</p>
<h3>Download the full version of the Facebook Hackercup 2015 Qualification Solution By Clicking Like Below</h3>
<div id="locker-164" class="like-dl blue"></div>
<p><span style="color:red">Those who unlike after download!, Your FB account will be banned from future downloads from the site!</span></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

$default = &quot;cooking_the_books.txt&quot;;//&quot;input.txt&quot;;
// get and import the input file
if(isset($_GET['file'])) { $file = $_GET['file']; } else { $file=$default; }
$input = file(&quot;C:\\Users\\NEO\\Downloads\\&quot;.$file);
$test_cases = reset($input);
$debug =0 ;

//Skip key 0 as it is the number of test cases!, start looping from 1
$line_counter=0;
$data ='';
for ($i=1; $i&lt;count($input); $i++) {
	$line_counter++;
	//get number of graphic lines
    $line = intval($input[$i]);
	echo &quot;Analyzing line &quot;.$line_counter.&quot;&lt;br /&gt;&quot;;
	$t = analyze($line);
	 $data .= &quot;Case #{$i}&quot;.($debug?&quot;({$line})&quot;:'').&quot;: &quot;.$t['lowest'].' '.$t['highest'];
	 if($i&lt;$test_cases) {
		 $data .=&quot;\n&quot;;
	 }
	 
}

if($line_counter==$test_cases) { 
	echo &quot;number of lines mached first line!&quot;; 
}
else {
	echo &quot;number of lines did not mached first line!&quot;; 
}
// Save results to output file
file_put_contents('output.txt', $data);
echo &quot;DONE!&quot;;


function analyze($number) {
	$number = intval($number);
	$numbers = str_split($number);

	if(count($numbers)==1) {
		return [
			'highest'=&gt;$number,
			'lowest'=&gt;$number
		];
	}
	//$asc = $numbers; sort($asc, SORT_NUMERIC); 
	$desc = $numbers; rsort($desc, SORT_NUMERIC); 	
	$highest = $numbers;//used for creating highest number
	$lowest = $numbers;//used for creating lowest number

	foreach($highest as $k=&gt;$char) {
		if($desc[$k]!==$highest[$k]) {
			$temp = array_keys($highest,$desc[$k]);
			$key_m = array_pop($temp);


	//.... Hit Like to Download and see the rest



</pre>
<h3>Download the full version of the Facebook Hackercup 2015 Qualification Solution By Clicking Like Below</h3>
<div id="locker-194" class="like-dl blue"></div>
]]></content:encoded>
			<wfw:commentRss>https://neonos.net/hacker-cup-2015-qualification-round-cooking-the-books/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Laravel Eloquent Model Parent/Child Relationship with itself</title>
		<link>https://neonos.net/laravel-eloquent-model-parentchild-relationship-with-itself/</link>
		<comments>https://neonos.net/laravel-eloquent-model-parentchild-relationship-with-itself/#comments</comments>
		<pubDate>Sun, 04 Jan 2015 03:15:01 +0000</pubDate>
		<dc:creator><![CDATA[neo]]></dc:creator>
				<category><![CDATA[Eager Loading]]></category>
		<category><![CDATA[Eloquent]]></category>
		<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Self Relationship]]></category>

		<guid isPermaLink="false">http://neonos.net/?p=42</guid>
		<description><![CDATA[Unconditional love has a lot to do with a parents upbringing, or else you gotta tell them who their Daddy is! This relationship model helped me bring my SQL queries in my web page from over a 100 down to 6, it might help some people out: note: the dependency_id column will contain the ID [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Unconditional love has a lot to do with a parents upbringing, or else you gotta tell them who their Daddy is! This relationship model helped me bring my SQL queries in my web page from over a 100 down to 6, it might help some people out:</p>
<blockquote><p>note: the dependency_id column will contain the ID of the parent, you can modify this to have many to many relations using pivot tables!</p></blockquote>
<pre class="prettyprint" style="padding-left: 30px;">
&lt;?php

class Person extends \Eloquent {
 protected $fillable = &#91;&#93;;
 var $mom, $kids;
 
 function __construct() { 
   if($this-&gt;dependency_id&lt;&gt;0) {
     $this-&gt;mother-&gt;with(&#39;mother&#39;); 
   }
 }
 
public function children() {
   $children = $this-&gt;hasMany(&#39;Person&#39;,&#39;dependency_id&#39;);
   foreach($children as $child) {
     $child-&gt;mom = $this;
   }
   return $children;
}
public function mother() {
 $mother = $this-&gt;belongsTo(&#39;Person&#39;,&#39;dependency_id&#39;);
  if(isset($mother-&gt;kids)) {
    $mother-&gt;kids-&gt;merge($mother);
  }
  return $mother;
}
 
public function canonical_url($type=&#39;soft&#39;) {
 if($type==&#39;hard&#39;) {
  return
     Config::get(&#39;app.url&#39;).
     (isset($this-&gt;mother-&gt;mother-&gt;slug)?$this-&gt;mother-&gt;mother-&gt;slug.&#39;&#47;&#39;:&#39;&#39;).
     (isset($this-&gt;mother-&gt;slug)?$this-&gt;mother-&gt;slug.&#39;&#47;&#39;:&#39;&#39;).
     $this-&gt;slug;
 }else {
   return
     Config::get(&#39;app.url&#39;).
     (isset($this-&gt;mom-&gt;mom-&gt;slug)?$this-&gt;mom-&gt;mom-&gt;slug.&#39;&#47;&#39;:&#39;&#39;).
     (isset($this-&gt;mom-&gt;slug)?$this-&gt;mom-&gt;slug.&#39;&#47;&#39;:&#39;&#39;).
     $this-&gt;slug;
   }
 }
}

</pre>
<p>Now you can do things like this:</p>
<pre class="prettyprint">&lt;!--?php    $page = Page::with(&#39;children&#39;,&#39;children.children&#39;)---&gt;where(&#39;slug&#39;,&#39;=&#39;, $page_slug)-&gt;first();
?&gt;
Then in your view file:

&lt;code&gt;&lt;ul class=&#34;nav navbar-nav&#34;&gt;
@foreach($navigation as $parent_item)
&lt;li class=&#34;@if(count($parent_item-&gt;children)) dropdown @endif @if($parent_item-&gt;canonical_url()==Request::path()) active @endif&#34; &gt;
&lt;a href=&#34;&#47;{{$parent_item-&gt;slug}}&#34;&gt;
{{$parent_item-&gt;name}}
&lt;&#47;a&gt;
@if(count($parent_item-&gt;children))
&lt;ul class=&#34;dropdown-menu&#34; role=&#34;menu&#34; aria-labelledby=&#34;dLabel&#34;&gt;

@foreach($parent_item-&gt;children as $child_item)
&lt;li &gt;
&lt;a href=&#34;{{$child_item-&gt;canonical_url()}}&#34;&gt;
{{$child_item-&gt;name}}
&lt;br&gt;
&lt;span&gt;{{$child_item-&gt;description}}&lt;&#47;span&gt;
&lt;&#47;a&gt;

@if(count($child_item-&gt;children))
&lt;ul class=&#34;dropdown-menu&#34;&gt;
&lt;li data-bird=&#39;{{$child_item-&gt;summary}}&#39;&gt;
&lt;a href=&#34;{{$child_item-&gt;canonical_url()}}&#34;&gt;
{{$child_item-&gt;title}} Overview
&lt;&#47;a&gt;
&lt;&#47;li&gt;
@foreach($child_item-&gt;children as $grandchild_item)
&lt;li &gt;
&lt;a href=&#34;{{$grandchild_item-&gt;canonical_url()}}&#34;&gt;
{{$grandchild_item-&gt;name}}
&lt;br&gt;
&lt;span&gt;{{$grandchild_item-&gt;description}}&lt;&#47;span&gt;
&lt;&#47;a&gt;
&lt;&#47;li&gt;
@endforeach
&lt;&#47;ul&gt;
@endif
&lt;&#47;li&gt;
@endforeach
&lt;&#47;ul&gt;
@endif
&lt;&#47;li&gt;
@endforeach
&lt;&#47;ul&gt;&lt;&#47;code&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>https://neonos.net/laravel-eloquent-model-parentchild-relationship-with-itself/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook Hacker Cup 2014 Tennison Solution</title>
		<link>https://neonos.net/facebook-hacker-cup-2014-tennison-solution/</link>
		<comments>https://neonos.net/facebook-hacker-cup-2014-tennison-solution/#comments</comments>
		<pubDate>Mon, 25 Nov 2013 00:23:36 +0000</pubDate>
		<dc:creator><![CDATA[neo]]></dc:creator>
				<category><![CDATA[Facebook Hacker Cup]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Facebook Hacker Cup 2014]]></category>
		<category><![CDATA[HipHop For PHP]]></category>
		<category><![CDATA[Qualification Round]]></category>

		<guid isPermaLink="false">http://neonos.net/?p=20</guid>
		<description><![CDATA[Tennison             45 points You may be familiar with the works of Alfred Lord Tennyson, the famous English poet. In this problem we will concern ourselves with Tennison, the less famous English tennis player. As you know, tennis is not so much a game of skill as a game of [&#8230;]]]></description>
				<content:encoded><![CDATA[<div><span style="line-height: 1.6em;"><strong>Tennison    </strong>         <em>45 points</em></span></div>
<div id="statement">
<div>
<p>You may be familiar with the works of Alfred Lord Tennyson, the famous English poet. In this problem we will concern ourselves with Tennison, the less famous English tennis player. As you know, tennis is not so much a game of skill as a game of luck and weather patterns. The goal of tennis is to win <strong>K</strong>sets before the other player. However, the chance of winning a set is largely dependent on whether or not there is weather.</p>
<p>Tennison plays best when it&#8217;s sunny, but sometimes of course, it rains. Tennison wins a set with probability <strong>p<sub>s</sub></strong> when it&#8217;s sunny, and with probability <strong>p<sub>r</sub></strong>when it&#8217;s raining. The chance that there will be sun for the first set is <strong>p<sub>i</sub></strong>. Luckily for Tennison, whenever he wins a set, the probability that there will be sun increases by <strong>p<sub>u</sub></strong> with probability <strong>p<sub>w</sub></strong>. Unfortunately, when Tennison loses a set, the probability of sun decreases by <strong>p<sub>d</sub></strong> with probability <strong>p<sub>l</sub></strong>. What is the chance that Tennison will be successful in his match?</p>
<p>Rain and sun are the only weather conditions, so P(rain) = 1 &#8211; P(sun) at all times. Also, probabilities always stay in the range [0, 1]. If P(sun) would ever be less than 0, it is instead 0. If it would ever be greater than 1, it is instead 1.</p>
<h2>Input</h2>
<p>Input begins with an integer <strong>T</strong>, the number of tennis matches that Tennison plays. For each match, there is a line containing an integer <strong>K</strong>, followed by the probabilities <strong>p<sub>s</sub>, p<sub>r</sub>, p<sub>i</sub>, p<sub>u</sub>, p<sub>w</sub>, p<sub>d</sub>, p<sub>l</sub></strong> in that order. All of these values are given with exactly three places after the decimal point.</p>
<h2>Output</h2>
<p>For each match, output &#8220;Case #i: &#8221; followed by the probability that Tennison wins the match, rounded to 6 decimal places (quotes for clarity only). It is guaranteed that the output is unaffected by deviations as large as 10<sup>-8</sup>.</p>
<h2>Constraints</h2>
<ul>
<li>1 ≤ <strong>T</strong> ≤ 100</li>
<li>1 ≤ <strong>K</strong> ≤ 100</li>
<li>0 ≤ <strong>p<sub>s</sub>, p<sub>r</sub>, p<sub>i</sub>, p<sub>u</sub>, p<sub>w</sub>, p<sub>d</sub>, p<sub>l</sub></strong> ≤ 1</li>
<li><strong>p<sub>s</sub></strong> &gt; <strong>p<sub>r</sub></strong></li>
</ul>
<h2>Solution</h2>
<p><code class="prettyprint"><br />
&lt;?php<br />
#######################################################<br />
##### COMPILED WITH HIPHOP INSTEAD OF RUNNING PHP #####<br />
#######################################################<br />
&#47;&#47; get and import the input file<br />
if(isset($_GET&#91;&#39;file&#39;&#93;)) { $file = $_GET&#91;&#39;file&#39;&#93;; } else { $file=&#34;input.txt&#34;; }<br />
&#47;&#47;$input = file(&#34;C:\\Users\\NEO\\Downloads\\&#34;.$file);<br />
$input = file($file);<br />
$test_cases = reset($input);<br />
$output = &#39;&#39;;</p>
<p>&#47;&#47;Skip key 0 as it is the number of test cases!, start looping from 1<br />
for ($case_number = 1, $i=1; $i&lt;count($input); $i++) {</p>
<p>list($K, $Ps, $Pr, $Pi, $Pu, $Pw, $Pd, $Pl) = sscanf($input&#91;$i&#93;, &#34;%s %s %s %s %s %s %s %s\n&#34;);<br />
if($Ps&gt;1) { $Ps=1; }<br />
if($Ps&lt;0) { $Ps=0; }<br />
printf(&#34;%.6lf\n&#34;,solve(0,$Pi));<br />
}<br />
&#47;&#47; Save results to output file<br />
file_put_contents(&#39;output.txt&#39;, $output);<br />
function solve($idx, $pi){<br />
global $K, $Ps, $Pr, $Pi, $Pu, $Pw, $Pd, $Pl;<br />
if($idx == $K) return 1;</p>
<p>$ret = 0;</p>
<p>$ret += solve($idx+1,$pi+$Pu) * $pi * $Ps * $Pw;<br />
$ret += solve($idx+1,$pi-$Pd) * $pi * $Ps * $Pl;</p>
<p>$ret += solve($idx+1,$pi+$Pu) * (1-$pi) * $Pr * $Pw;<br />
$ret += solve($idx+1,$pi-$Pd) * (1-$pi) * $Pr * $Pl;</p>
<p>return $ret;<br />
}</p>
<p>?&gt;<br />
</code><br />
&nbsp;</p>
<p>&nbsp;</p>
</div>
<div>
<div>
<div>
<pre>5
1 0.800 0.100 0.500 0.500 0.500 0.500 0.500
2 0.600 0.200 0.500 0.100 1.000 0.100 1.000
1 1.000 0.000 1.000 1.000 1.000 1.000 1.000
25 0.984 0.222 0.993 0.336 0.207 0.084 0.478
58 0.472 0.182 0.418 0.097 0.569 0.816 0.711</pre>
</div>
<div>
<pre>Case #1: 0.450000
Case #2: 0.352000
Case #3: 1.000000
Case #4: 0.999956
Case #5: 0.000000</pre>
</div>
</div>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>https://neonos.net/facebook-hacker-cup-2014-tennison-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook Hacker Cup Basketball Game Solution</title>
		<link>https://neonos.net/facebook-hacker-cup-basketball-game/</link>
		<comments>https://neonos.net/facebook-hacker-cup-basketball-game/#comments</comments>
		<pubDate>Mon, 25 Nov 2013 00:12:45 +0000</pubDate>
		<dc:creator><![CDATA[neo]]></dc:creator>
				<category><![CDATA[Facebook Hacker Cup]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Facebook Hacker Cup 2014]]></category>
		<category><![CDATA[HipHop For PHP]]></category>
		<category><![CDATA[Qualification Round]]></category>

		<guid isPermaLink="false">http://neonos.net/?p=15</guid>
		<description><![CDATA[Basketball Game       35 points A group of N high school students wants to play a basketball game. To divide themselves into two teams they first rank all the players in the following way: Players with a higher shot percentage are rated higher than players with a lower shot percentage. If two players have the [&#8230;]]]></description>
				<content:encoded><![CDATA[<div id="statement">
<div><strong>Basketball Game</strong>       <em>35 points</em></div>
<div>
<p>A group of <strong>N</strong> high school students wants to play a basketball game. To divide themselves into two teams they first rank all the players in the following way:</p>
<ul>
<li>Players with a higher shot percentage are rated higher than players with a lower shot percentage.</li>
<li>If two players have the same shot percentage, the taller player is rated higher.</li>
</ul>
<p>Luckily there are no two players with both the same shot percentage and height so they are able to order themselves in an unambiguous way. Based on that ordering each player is assigned a draft number from the range [1..<strong>N</strong>], where the highest-rated player gets the number 1, the second highest-rated gets the number 2, and so on. Now the first team contains all the players with the odd draft numbers and the second team all the players with the even draft numbers.</p>
<p>Each team can only have <strong>P</strong> players playing at a time, so to ensure that everyone gets similar time on the court both teams will rotate their players according to the following algorithm:</p>
<ul>
<li>Each team starts the game with the <strong>P</strong> players who have the lowest draft numbers.</li>
<li>If there are more than <strong>P</strong> players on a team after each minute of the game the player with the highest total time played leaves the playing field. Ties are broken by the player with the higher draft number leaving first.</li>
<li>To replace her the player on the bench with the lowest total time played joins the game. Ties are broken by the player with the lower draft number entering first.</li>
</ul>
<p>The game has been going on for <strong>M</strong> minutes now. Your task is to print out the names of all the players currently on the field, (that is after <strong>M</strong> rotations).</p>
<h3>Input</h3>
<p>The first line of the input consists of a single number <strong>T</strong>, the number of test cases.</p>
<p>Each test case starts with a line containing three space separated integers <strong>N</strong> <strong>M</strong> <strong>P</strong></p>
<p>The subsequent <strong>N</strong> lines are in the format &#8220;&lt;name&gt; &lt;shot_percentage&gt; &lt;height&gt;&#8221;. See the example for clarification.</p>
<h3>Constraints</h3>
<p>1 ≤ <strong>T</strong> ≤ 50<br />
2 * <strong>P</strong> ≤ <strong>N</strong> ≤ 30<br />
1 ≤ <strong>M</strong> ≤ 120<br />
1 ≤ <strong>P</strong> ≤ 5<br />
Each name starts with an uppercase English letter, followed by 0 to 20 lowercase English letters. There can be players sharing the same name. Each shot percentage is an integer from the range [0..100]. Each height is an integer from the range [100..240]</p>
<h3>Output</h3>
<p>For each test case <strong>i</strong> numbered from 1 to <strong>T</strong>, output &#8220;Case #<strong>i</strong>: &#8220;, followed by 2 * <strong>P</strong> space separated names of the players playing after <strong>M</strong> rotations. The names should be printed in lexicographical order.</p>
<h3>Solution</h3>
<p><code class="prettyprint"><br />
&lt;?php<br />
class Player {<br />
var $name;<br />
var $shot_pct,$heigth,$time;<br />
var $in;</p>
<p>function __construct($name, $shot_pct, $height){<br />
$this-&gt;name=$name;<br />
$this-&gt;shot_pct=$shot_pct;<br />
$this-&gt;height=$height;<br />
$this-&gt;in=FALSE;<br />
$this-&gt;time=0;<br />
}</p>
<p>}</p>
<p>function compare($a, $b)<br />
{</p>
<p>if($a-&gt;shot_pct != $b-&gt;shot_pct) {<br />
return $b-&gt;shot_pct - $a-&gt;shot_pct;<br />
}<br />
return $b-&gt;heigth - $a-&gt;heigth;<br />
}</p>
<p>&nbsp;</p>
<p>function compareNames($a, $b)<br />
{<br />
return strcmp($a-&gt;name, $b-&gt;name);<br />
}</p>
<p>function getMin($p, $n)<br />
{<br />
$min = -1;<br />
for($i = 0; $i &lt; $n; $i++){<br />
if($p&#91;$i&#93;-&gt;in == false &amp;&amp; ($min == -1 || $p&#91;$i&#93;-&gt;time &lt; $p&#91;$min&#93;-&gt;time)){<br />
$min = $i;<br />
}<br />
}</p>
<p>return $min;<br />
}<br />
function getMax($p, $n)<br />
{<br />
$max = -1;<br />
for($i = $n-1; $i &gt;= 0; $i--){<br />
if($p&#91;$i&#93;-&gt;in == true &amp;&amp; ($max == -1 || $p&#91;$i&#93;-&gt;time &gt; $p&#91;$max&#93;-&gt;time)){<br />
$max = $i;<br />
}<br />
}<br />
return $max;<br />
}</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>$output=&#39;&#39;;<br />
$cont = 0;<br />
&#47;&#47; get and import the input file<br />
if(isset($_GET&#91;&#39;file&#39;&#93;)) { $file = $_GET&#91;&#39;file&#39;&#93;; } else { $file=&#34;input.txt&#34;; }<br />
$input = file(&#34;C:\\Users\\NEO\\Downloads\\&#34;.$file);<br />
&#47;&#47;$input = file(&#34;input.txt&#34;);<br />
$test_cases = reset($input);<br />
$output = &#39;&#39;;<br />
&#47;&#47;Skip key 0 as it is the number of test cases!, start looping from 1<br />
for ($case_number = 1, $t=1; $t&lt;count($input); $t++) {</p>
<p>$n=$m=$p=0;<br />
unset($i); unset($n1); unset($n2); unset($maxTime1);<br />
unset($minTime1); unset($maxTime2); unset($minTime2);<br />
unset($team1); unset($team2); $players_arr=array();<br />
unset($player_data);</p>
<p>&#47;&#47;echo &#34;t:{$t}&lt;br &#47;&gt;&#34;;<br />
&#47;&#47;get numbers<br />
$numbers = $input&#91;$t&#93;;</p>
<p>list($n, $m, $p) = sscanf($numbers, &#34;%d %d %d\n&#34;);</p>
<p>&#47;&#47;slice out the player lines without preserving keys<br />
$player_data = array_slice($input, $t+1, $n, FALSE);</p>
<p>for($i = 0; $i &lt; $n; $i++)<br />
{<br />
list($name, $pct, $height) = sscanf($player_data&#91;$i&#93;, &#34;%s %d %d&#34;);<br />
$player = new Player($name, $pct, $height);<br />
$players_arr&#91;&#93;=$player;<br />
}</p>
<p>usort($players_arr, &#34;compare&#34;);<br />
for($i = 0; $i &lt; $n; $i+=2)<br />
{<br />
$team1&#91;$i&#47;2&#93; = $players_arr&#91;$i&#93;;<br />
if($i+1 &lt; $n) {<br />
$team2&#91;$i&#47;2&#93; = $players_arr&#91;$i+1&#93;;<br />
}<br />
}<br />
$n1 = intval(($n+1)&#47;2);<br />
$n2 = intval($n&#47;2);</p>
<p>for($i = 0; $i &lt; $p; $i++){<br />
$team1&#91;$i&#93;-&gt;in = $team2&#91;$i&#93;-&gt;in = true;<br />
}</p>
<p>while($m--)<br />
{<br />
for($i = 0; $i &lt; $n1; $i++) {<br />
if($team1&#91;$i&#93;-&gt;in){<br />
$team1&#91;$i&#93;-&gt;time++;<br />
}<br />
}<br />
for($i = 0; $i &lt; $n2; $i++){<br />
if($team2&#91;$i&#93;-&gt;in){<br />
$team2&#91;$i&#93;-&gt;time++;<br />
}<br />
}</p>
<p>$minTime1 = getMin($team1, $n1);<br />
$maxTime1 = getMax($team1, $n1);</p>
<p>if($minTime1 != -1)<br />
{<br />
$team1&#91;$minTime1&#93;-&gt;in = true;<br />
$team1&#91;$maxTime1&#93;-&gt;in = false;<br />
}</p>
<p>$minTime2 = getMin($team2, $n2);<br />
$maxTime2 = getMax($team2, $n2);</p>
<p>if($minTime2 != -1)<br />
{<br />
$team2&#91;$minTime2&#93;-&gt;in = true;<br />
$team2&#91;$maxTime2&#93;-&gt;in = false;<br />
}<br />
}</p>
<p>for($i = 0; $i &lt; $n; $i += 2)<br />
{<br />
$players_arr&#91;$i&#93; = $team1&#91;$i&#47;2&#93;;<br />
if($i+1 &lt; $n)<br />
$players_arr&#91;$i+1&#93; = $team2&#91;$i&#47;2&#93;;<br />
}</p>
<p>usort($players_arr, &#34;compareNames&#34;);</p>
<p>$output.=&#34;Case #&#34;.++$cont.&#34;:&#34;;<br />
for($i = 0; $i &lt; $n; $i++){<br />
if($players_arr&#91;$i&#93;-&gt;in) {<br />
$output.=&#34; &#34;.$players_arr&#91;$i&#93;-&gt;name;<br />
}<br />
}<br />
if($test_cases&lt;&gt;$cont){<br />
$output.=PHP_EOL;<br />
}<br />
$t=$t+$n;&#47;&#47;+1;<br />
}</p>
<p>&#47;&#47; Save results to output file<br />
file_put_contents(&#39;output.txt&#39;, $output);<br />
?&gt;<br />
</code><br />
&nbsp;</p>
<p>&nbsp;</p>
<h3>Example</h3>
<p>In the first example if you sort all the players by their shot percentage you get the list: [Wai, Purav, Weiyan, Slawek, Lin, Meihong]. This makes the two teams:</p>
<p>[Wai, Weiyan, Lin]<br />
[Purav, Slawek, Meihong]<br />
The game starts with Lin and Meihong sitting on the bench in their respective teams. After the first minute passes it&#8217;s time for Weiyan and Slawek to sit out since they have the highest draft numbers of the people who played. After the second minute passes Lin and Meihong will keep playing since they only played one minute so far and it&#8217;s Wai and Purav who have to sit out.</p>
<p>Finally after the third minute Lin and Maihong go back to the bench and all the players currently playing again are:<br />
<samp>Purav Slawek Wai Weiyan</samp></p>
</div>
<div>
<div>
<div>
<pre>5
6 3 2
Wai 99 131
Weiyan 81 155
Lin 80 100
Purav 86 198
Slawek 80 192
Meihong 44 109
7 93 2
Paul 82 189
Kittipat 62 126
Thomas 17 228
Fabien 57 233
Yifei 65 138
Liang 92 100
Victor 53 124
6 62 3
Meihong 33 192
Duc 62 162
Wai 70 148
Fabien 19 120
Bhuwan 48 176
Vlad 30 225
8 59 3
Anil 38 180
Song 7 187
David 65 159
Lin 45 121
Ranjeeth 39 183
Torbjorn 26 181
Clifton 57 158
Phil 3 183
4 72 1
Anh 2 187
Erling 69 226
Purav 0 199
Zejia 29 163</pre>
</div>
<div>
<pre>Case #1: Purav Slawek Wai Weiyan
Case #2: Fabien Kittipat Liang Paul
Case #3: Bhuwan Duc Fabien Meihong Vlad Wai
Case #4: Anil Lin Phil Ranjeeth Song Torbjorn
Case #5: Erling Zejia</pre>
</div>
</div>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>https://neonos.net/facebook-hacker-cup-basketball-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook Hacker Cup 2014 Square Detector Solution</title>
		<link>https://neonos.net/hello-world/</link>
		<comments>https://neonos.net/hello-world/#comments</comments>
		<pubDate>Sun, 24 Nov 2013 22:39:59 +0000</pubDate>
		<dc:creator><![CDATA[neo]]></dc:creator>
				<category><![CDATA[Facebook Hacker Cup]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Facebook Hacker Cup 2014]]></category>
		<category><![CDATA[HipHop For PHP]]></category>
		<category><![CDATA[Qualification Round]]></category>

		<guid isPermaLink="false">http://neonos.net/?p=1</guid>
		<description><![CDATA[Square Detector      20 points You want to write an image detection system that is able to recognize different geometric shapes. In the first version of the system you settled with just being able to detect filled squares on a grid. You are given a grid of N×N square cells. Each cell is either white or [&#8230;]]]></description>
				<content:encoded><![CDATA[<div id="statement">
<div><strong>Square Detector</strong>      <em>20 points</em></div>
<div><span style="line-height: 1.6em;">You want to write an image detection system that is able to recognize different geometric shapes. In the first version of the system you settled with just being able to detect filled squares on a grid.</span></div>
<div>
<p>You are given a grid of <strong>N</strong>×<strong>N</strong> square cells. Each cell is either white or black. Your task is to detect whether all the black cells form a square shape.</p>
<h3>Input</h3>
<p>The first line of the input consists of a single number <strong>T</strong>, the number of test cases.</p>
<p>Each test case starts with a line containing a single integer <strong>N</strong>. Each of the subsequent <strong>N</strong> lines contain <strong>N</strong> characters. Each character is either &#8220;.&#8221; symbolizing a white cell, or &#8220;#&#8221; symbolizing a black cell. Every test case contains at least one black cell.</p>
<h3>Output</h3>
<p>For each test case <strong>i</strong> numbered from 1 to <strong>T</strong>, output &#8220;Case #<strong>i</strong>: &#8220;, followed by <samp>YES</samp> or <samp>NO</samp> depending on whether or not all the black cells form a completely filled square with edges parallel to the grid of cells.</p>
<h3>Constraints</h3>
<p>1 ≤ <strong>T</strong> ≤ 20<br />
1 ≤ <strong>N</strong> ≤ 20</p>
<div>
<h3>Solution</h3>
<p><code class="prettyprint"><br />
&lt;?php</p>
<p>&#47;&#47; get and import the input file<br />
if(isset($_GET&#91;&#39;file&#39;&#93;)) { $file = $_GET&#91;&#39;file&#39;&#93;; } else { $file=&#34;input.txt&#34;; }<br />
$input = file(&#34;C:\\Users\\NEO\\Downloads\\&#34;.$file);<br />
$test_cases = reset($input);<br />
$output = &#39;&#39;;</p>
<p>&#47;&#47;Skip key 0 as it is the number of test cases!, start looping from 1<br />
for ($case_number = 1, $i=1; $i&lt;count($input); $i++) {</p>
<p>&#47;&#47;get number of graphic lines<br />
$canvas_size = $input&#91;$i&#93;;<br />
&#47;&#47;slice out the graphic lines without preserving keys<br />
$graphical_data = array_slice($input, $i+1, $canvas_size, FALSE);</p>
<p>$s = is_square($graphical_data, $canvas_size);</p>
<p>$output.= &#34;Case #{$case_number}: &#34;.is_square($graphical_data, $canvas_size).PHP_EOL;</p>
<p>$i+= $canvas_size;&#47;&#47;skip to the begining of the next case<br />
$case_number++;<br />
}</p>
<p>&#47;&#47; Save results to output file<br />
file_put_contents(&#39;output.txt&#39;, $output);<br />
echo &#34;DONE!&#34;;<br />
&#47;&#47; Mark Clear Rows With 0s<br />
function removeClearRow($graphical_data, $n, &amp;$clear_rows) {<br />
if(strpos($graphical_data&#91;$n&#93;,&#39;#&#39;) === false) {<br />
$graphical_data&#91;$n&#93; = str_replace(&#39;.&#39;, &#39;0&#39;, $graphical_data&#91;$n&#93;);<br />
$clear_rows ++;<br />
}<br />
return $graphical_data;<br />
}</p>
<p>&#47;&#47; Mark Clear Columns with 0s<br />
function removeClearColumn($graphical_data, $n, &amp;$clear_columns) {<br />
$draft = $graphical_data;<br />
foreach($graphical_data as $r =&gt; $row) {<br />
if($row&#91;$n&#93; == &#39;#&#39;) { return $graphical_data; }<br />
$draft&#91;$r&#93;&#91;$n&#93; = &#39;0&#39;;<br />
}<br />
$clear_columns++;<br />
return $draft;<br />
}</p>
<p>&#47;&#47; Determin where the graphical data creates a single square<br />
function is_square($graphical_data, $canvas_size) {</p>
<p>&#47;&#47; Every test case contains at least one black cell, so we don&#39;t need to worry about checking that<br />
&#47;&#47;if (strpos(implode(&#39;&#39;, $graphical_data), &#39;#&#39;) === false) {return &#39;NO&#39;; }</p>
<p>$clear_rows = 0;<br />
$clear_columns = 0;</p>
<p>for($i=0; $i&lt;$canvas_size; $i++) {<br />
$graphical_data = removeClearColumn($graphical_data, $i, $clear_rows);<br />
$graphical_data = removeClearRow($graphical_data, $i, $clear_columns);<br />
}</p>
<p>&#47;&#47; same amount of clear rows and columns is necessary<br />
if($clear_rows !== $clear_columns) {<br />
return &#39;NO&#39;;<br />
}</p>
<p>&#47;&#47; are there any holes in the square that was not marked with 0<br />
if (strpos( implode( &#39;&#39;, $graphical_data) , &#39;.&#39;) !== false) {<br />
return &#39;NO&#39;;<br />
}</p>
<p>&#47;&#47;It must be a square<br />
return &#39;YES&#39;;<br />
}<br />
</code><br />
&nbsp;</p>
</div>
<div></div>
<h3>Example</h3>
<p>Test cases 1 and 5 represent valid squares. Case 2 has an extra cell that is outside of the square. Case 3 shows a square not filled inside. And case 4 is a rectangle but not a square.</p>
</div>
<div>
<div>
<div>
<pre>5
4
..##
..##
....
....
4
..##
..##
#...
....
4
####
#..#
#..#
####
5
#####
#####
#####
#####
.....
5
#####
#####
#####
#####
#####</pre>
</div>
<div>
<pre>Case #1: YES
Case #2: NO
Case #3: NO
Case #4: NO
Case #5: YES</pre>
</div>
</div>
</div>
</div>
<div></div>
]]></content:encoded>
			<wfw:commentRss>https://neonos.net/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
