<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5119792247363719173</id><updated>2012-02-16T22:19:10.124+03:30</updated><category term='Codeforces'/><category term='DP'/><category term='Graph'/><category term='UVA'/><category term='TJU'/><category term='STL'/><category term='Brute Force'/><category term='TopCoder'/><category term='Recursion'/><category term='Math'/><category term='Euler Tour'/><category term='Greedy'/><category term='USACO'/><category term='Beginner'/><category term='DFS'/><title type='text'>ACM Problem Hint</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>70</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-6644094077374659459</id><published>2012-01-06T19:12:00.000+03:30</published><updated>2012-01-06T19:12:45.070+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Recursion'/><category scheme='http://www.blogger.com/atom/ns#' term='Brute Force'/><category scheme='http://www.blogger.com/atom/ns#' term='Codeforces'/><title type='text'>Codeforces Beta Round #91 (Div. 2 Only) - C. Lucky Sum</title><content type='html'>&lt;a href="http://codeforces.com/problemset/problem/121/A" target="_blank"&gt;problem statement&lt;/a&gt;&lt;br /&gt;After reading the problem statement, at first I was shocked by the range of input, 1&amp;lt;=left&amp;lt;=right&amp;lt;=10^9, but after a close look to the problem again, I found that there are not too much lucky numbers in the range, I recursively generate lucky numbers with this function :&lt;br /&gt;&lt;br /&gt;#define ll (long long)&lt;br /&gt;vector &lt;ll&gt; all; &lt;/ll&gt;&lt;br /&gt;void back(ll v, ll p10)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; all.push_back(v);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(v &amp;gt; 1000000000)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; back(v+4*p10, p10*10);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; back(v+7*p10, p10*10);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;then I sort "all", sort(all.begin(), all.end()),&lt;br /&gt;and after that, I iterate through "all", I have a variable x that is the leftmost point that next(x) is not calculated yet, then if x &amp;lt;= all[i] this means all of the points k in range [x, all[i]], next(k) = all[i],&lt;br /&gt;so I add (all[i]-x+1) * all[i] to the output, but if for example input is like this 2 6, I add (4-2+1)*4 + (7-5+1)*7 which is wrong, actual output is (4-2+1)*4 + (6-5+1)*7 so I added this if else to my code :&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(all[i] &amp;lt;= right)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sum += (all[i]-x+1) * all[i];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x = all[i]+1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sum += (right-x+1) * all[i];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x = all[i]+1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-6644094077374659459?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/6644094077374659459/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2012/01/codeforces-beta-round-91-div-2-only-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6644094077374659459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6644094077374659459'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2012/01/codeforces-beta-round-91-div-2-only-c.html' title='Codeforces Beta Round #91 (Div. 2 Only) - C. Lucky Sum'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-5496673246144727591</id><published>2012-01-06T16:33:00.000+03:30</published><updated>2012-01-06T16:34:19.829+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Brute Force'/><category scheme='http://www.blogger.com/atom/ns#' term='Beginner'/><category scheme='http://www.blogger.com/atom/ns#' term='Greedy'/><category scheme='http://www.blogger.com/atom/ns#' term='Codeforces'/><title type='text'>Codeforces Beta Round #91 (Div. 2 Only) - B. Lucky Substring</title><content type='html'>&lt;a href="http://codeforces.com/problemset/problem/122/B" target="_blank"&gt;problem statement&lt;/a&gt;&lt;br /&gt;After reading the problem statement carefully and checking the sample test cases, I noticed this part of the problem "&lt;span style="background-color: white; color: black; display: inline ! important; float: none; font-family: verdana,arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"&gt;needs the lexicographically minimum one&lt;/span&gt;", and thought by myself if it is gonna be a substring of input and lucky and lexicographically minimum one, then it has to be "4" or "7" because other substrings that are lucky contain 4 and 7, and there is no lucky number other than "4" or "7" that is repeated more times than "4" or "7" because at least the lucky number is going to be "44", "77", "47", "74", and at this cases "4" and "7" are repeated more times than these substrings.&lt;br /&gt;So I just iterated through input string and counted number of 4's and number of&amp;nbsp; 7's, if there is no 4 or 7, then output -1, otherwise if cnt7 is less than cnt4 output 4 else output 7.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-5496673246144727591?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/5496673246144727591/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2012/01/codeforces-beta-round-91-div-2-only-b.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/5496673246144727591'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/5496673246144727591'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2012/01/codeforces-beta-round-91-div-2-only-b.html' title='Codeforces Beta Round #91 (Div. 2 Only) - B. Lucky Substring'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-6599854703103785470</id><published>2012-01-06T15:57:00.002+03:30</published><updated>2012-01-06T16:20:42.943+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><category scheme='http://www.blogger.com/atom/ns#' term='Brute Force'/><category scheme='http://www.blogger.com/atom/ns#' term='Beginner'/><category scheme='http://www.blogger.com/atom/ns#' term='Codeforces'/><title type='text'>Codeforces Beta Round #91 (Div. 2 Only) - A. Lucky Division</title><content type='html'>&lt;a href="http://codeforces.com/problemset/problem/122/A" target="_blank"&gt;problem statement&lt;/a&gt;&lt;br /&gt;As you read in the problem statement, you have to find out whether a number has a divisor which is also a lucky number, so iterate from 1 to n, if n is divisible by i, then check whether it is a lucky number or not?&lt;br /&gt;Easily write a function that take a number and tell whether it is lucky or not, in other words the function checks whether all of the digits in the given number are 4 or 7.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;ol class="linenums" style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f8f8f8; color: black; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; list-style-image: initial; list-style-position: initial; list-style-type: none; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; orphans: 2; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: pre; widows: 2; word-spacing: 0px;"&gt;&lt;li class="L3" style="background-attachment: initial; background-clip: initial; background-color: #f8f8f8; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="kwd" style="color: #006699; font-weight: bold;"&gt;bool&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; isLucky&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;(&lt;/span&gt;&lt;span class="kwd" style="color: #006699; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; n&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;)&lt;/span&gt;&lt;/li&gt;&lt;li class="L4" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="pun" style="color: #666600;"&gt;{&lt;/span&gt;&lt;/li&gt;&lt;li class="L5" style="background-attachment: initial; background-clip: initial; background-color: #f8f8f8; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="kwd" style="color: #006699; font-weight: bold;"&gt;int&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; rem&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;;&lt;/span&gt;&lt;/li&gt;&lt;li class="L6" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="kwd" style="color: #006699; font-weight: bold;"&gt;while&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;(&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt;n&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;)&lt;/span&gt;&lt;/li&gt;&lt;li class="L7" style="background-attachment: initial; background-clip: initial; background-color: #f8f8f8; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;{&lt;/span&gt;&lt;/li&gt;&lt;li class="L8" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="pln" style="color: black;"&gt;rem &lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;=&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; n&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;%&lt;/span&gt;&lt;span class="lit" style="color: black;"&gt;10&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;;&lt;/span&gt;&lt;/li&gt;&lt;li class="L9" style="background-attachment: initial; background-clip: initial; background-color: #f8f8f8; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="kwd" style="color: #006699; font-weight: bold;"&gt;if&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;(&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt;rem &lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;!=&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; &lt;/span&gt;&lt;span class="lit" style="color: black;"&gt;7&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; &lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; rem &lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;!=&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; &lt;/span&gt;&lt;span class="lit" style="color: black;"&gt;4&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;)&lt;/span&gt;&lt;/li&gt;&lt;li class="L0" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="kwd" style="color: #006699; font-weight: bold;"&gt;return&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; &lt;/span&gt;&lt;span class="kwd" style="color: #006699; font-weight: bold;"&gt;false&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;;&lt;/span&gt;&lt;/li&gt;&lt;li class="L1" style="background-attachment: initial; background-clip: initial; background-color: #f8f8f8; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="pln" style="color: black;"&gt;n &lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;/=&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; &lt;/span&gt;&lt;span class="lit" style="color: black;"&gt;10&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;;&lt;/span&gt;&lt;/li&gt;&lt;li class="L2" style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;}&lt;/span&gt;&lt;/li&gt;&lt;li class="L3" style="background-attachment: initial; background-clip: initial; background-color: #f8f8f8; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="pln" style="color: black;"&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span class="kwd" style="color: #006699; font-weight: bold;"&gt;return&lt;/span&gt;&lt;span class="pln" style="color: black;"&gt; &lt;/span&gt;&lt;span class="kwd" style="color: #006699; font-weight: bold;"&gt;true&lt;/span&gt;&lt;span class="pun" style="color: #666600;"&gt;;&lt;/span&gt;&lt;/li&gt;&lt;li class="L5" style="background-attachment: initial; background-clip: initial; background-color: #f8f8f8; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span class="pun" style="color: #666600;"&gt;}&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-6599854703103785470?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/6599854703103785470/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2012/01/codeforces-beta-round-91-div-2-only.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6599854703103785470'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6599854703103785470'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2012/01/codeforces-beta-round-91-div-2-only.html' title='Codeforces Beta Round #91 (Div. 2 Only) - A. Lucky Division'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-5724221108425190333</id><published>2011-12-15T01:13:00.001+03:30</published><updated>2011-12-15T01:15:00.882+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><category scheme='http://www.blogger.com/atom/ns#' term='Euler Tour'/><category scheme='http://www.blogger.com/atom/ns#' term='Graph'/><category scheme='http://www.blogger.com/atom/ns#' term='DFS'/><title type='text'>UVA - 10596 - Morning Walk</title><content type='html'>&lt;a href="http://uva.onlinejudge.org/external/105/10596.html" target="_blank"&gt;problem statement&lt;/a&gt; &lt;br /&gt;If you read the problem statement carefully, It's obvious that you have to check whether an undirected graph has an Eulerian tour or not,&lt;span style="font-size: small;"&gt;&lt;span class="mw-headline" id="Necessary_and_sufficient_conditions"&gt; necessary and sufficient conditions for and undirected graph :&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;An undirected graph has a closed Euler tour if it is connected and each vertex has an even degree.&lt;br /&gt;I do this using a simple dfs to count the number of components.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-5724221108425190333?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/5724221108425190333/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2011/12/10596-morning-walk.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/5724221108425190333'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/5724221108425190333'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2011/12/10596-morning-walk.html' title='UVA - 10596 - Morning Walk'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-5843533731138989064</id><published>2011-12-14T22:41:00.004+03:30</published><updated>2011-12-15T00:26:26.331+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><title type='text'>UVA - 11099 - Next Same-Factored</title><content type='html'>&lt;a href="http://uva.onlinejudge.org/external/110/11099.html" target="_blank"&gt;problem statement&lt;/a&gt;&lt;br /&gt;After I read the problem statement, I tried to code the first solution that came to my mind, please do not laugh at me for my bad solution :-D, this solution is because I misunderstood the problem statement, my first approach was this : I tried to find the smallest prime factor of n, and output n*smallest factor, but very soon I found that output of 12 is not 12*2 but it's 18, cause 12 = 2^2*3, 24 = 2^3*3 and 18 = 2*3^2, So after I was disappointed for a moment, I tried to think of a new algorithm, and after some nonsense approaches I found that I have to precalculate a list for each number in range 1-10^6 that shows it's factors, and sort them, then whenever I read a number I can check next number in the sorted list, and check whether it's list is equal to the input list or not, if yes print that, and if not print "Not Exist!", I did this, at first run it took 12 seconds to run, and I know what the problem was, that was because of sorting a list which has vectors, I was sure this solution is gonna end with accepted, but don't know how, thinking and&amp;nbsp; thinking..., I found that I have to remove vector, and put something else instead to reduce running time, at this time I remembered hashing :-D the key to solve the problem in my approach came along, and I was happy ;-).&lt;br /&gt;I have a hash function like this :&lt;br /&gt;int hash(string &amp;amp;s)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; &amp;nbsp; int res = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = 0; i &amp;lt; s.size(); i ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; res = res*127 + s[i];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return res;&lt;br /&gt;}&lt;br /&gt;e.g. hash("abcd") = a*127^3 + b*127^2 + c*127^1 + d&lt;br /&gt;and hash("abc") = a*127^2 + b*127^1 + c&lt;br /&gt;6 =&amp;gt; 2-3 &lt;br /&gt;12 =&amp;gt; 2-3&lt;br /&gt;18 =&amp;gt; 2-3&lt;br /&gt;24 =&amp;gt; 2-3&lt;br /&gt;36 =&amp;gt; 2-3&lt;br /&gt;.....&lt;br /&gt;if look at their list like a string and produce their hash, all of them will have the same hash value and when I sort them they will be neighbor, hashValue( 6 ) = 2*127^2 + 3 = hashValue( 12 ) = hashValue( 18 ) = ...&lt;br /&gt;I tried to build these hash values in a bottom-up manner, like this :&lt;br /&gt;&amp;nbsp; &amp;nbsp; #define ll long long &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(ll i = 0; i &amp;lt; prime.size(); i ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ll curP = prime[i];&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(ll j = curP; j &amp;lt; SIZE; j += curP)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; all[j].hash = all[j].hash*1046527 + curP;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;but at first 1046527 was 127 and I got wrong answer, that was because some numbers hash value was a prime number and that would produce a problem, after I changed 127 I got accepted and running time was about 1 second.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-5843533731138989064?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/5843533731138989064/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2011/12/uva-11099-next-same-factored.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/5843533731138989064'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/5843533731138989064'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2011/12/uva-11099-next-same-factored.html' title='UVA - 11099 - Next Same-Factored'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-8960090928012623876</id><published>2011-12-08T01:53:00.001+03:30</published><updated>2011-12-08T02:32:00.498+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Brute Force'/><title type='text'>TopCoder - SRM 515 DIV 2 - RotatedClock - getEarliest</title><content type='html'>After I read the problem statement, I didn't find out what's going on :-D. I think the most important part of the problem for me was these two sentences : "The clock has a scale &lt;b&gt;marked&lt;/b&gt; in 30 degree increments ... She measured the angles of hands from a certain &lt;b&gt;mark&lt;/b&gt;". Before I pay attention to these two sentences, I was confused of test case 3, I was thinking why 19 19 is impossible? Then I found that measured degrees are from a certain mark, so both of them can not be 19 19, but of course both of them can be 0 0, 30 30, 60 60, 90 90 ...&lt;br /&gt;At first I was trying to solve the problem in this way : Which &lt;b&gt;mark &lt;/b&gt;has she chosen to measure the degrees? 0? 30? 60? 90? 120? 150? 180? 210? 240? 270? 300? 330? I thought that I could map one of the degrees to one of these 12 origins, then map the other one, and check whether the resulted degrees show a true time? then I figured out that it's a little hard to implement. I tried to reverse my approach. I iterated h = 0 to 12 and m = 0 to 60 and each time I made mDegree and hDegree, at this moment I have to verify whether mDegree and hDegree is an answer or not? So I shifted both of them 0 degrees, 30 degrees, 90 degrees, 120 degrees, ... 330 degrees and checked whether they matched input or not, if yes I had found the answer. But at this time I checked though my approach seems true but test case 2 gave me time 07:01 that's 210 degrees and 6 degrees that if I add 30 to both of them it becomes 240 and 36 that matches test case 2, I read the problem statement again and I found that her measurement device supports integer degrees not real numbers, because 07:01 is not 210 degrees and 6 degrees, it is 210.5 and 6. So I added another condition to my program that if minute was odd it is not my answer. And Finally Accepted :-).&lt;br /&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-8960090928012623876?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/8960090928012623876/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2011/12/topcoder-srm-515-div-2-rotatedclock.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8960090928012623876'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8960090928012623876'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2011/12/topcoder-srm-515-div-2-rotatedclock.html' title='TopCoder - SRM 515 DIV 2 - RotatedClock - getEarliest'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-2748157283674399135</id><published>2011-12-08T01:47:00.001+03:30</published><updated>2011-12-08T01:50:57.017+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Brute Force'/><category scheme='http://www.blogger.com/atom/ns#' term='Beginner'/><category scheme='http://www.blogger.com/atom/ns#' term='STL'/><title type='text'>TopCoder - SRM 515 DIV 2 - FortunateNumbers - getFortunate</title><content type='html'>As you can see in the problem statement just a simple brute force is enough to pass the time limit. I produce all combinations of a[i] + b[j] + c[k] for all i, j, k and check whether it is a fortunate number or not, if yes I insert the number in a set and finally I return the size of the set.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-2748157283674399135?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/2748157283674399135/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2011/12/topcoder-srm-515-div-2-fortunatenumbers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2748157283674399135'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2748157283674399135'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2011/12/topcoder-srm-515-div-2-fortunatenumbers.html' title='TopCoder - SRM 515 DIV 2 - FortunateNumbers - getFortunate'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-890503442977020485</id><published>2011-12-04T10:35:00.001+03:30</published><updated>2011-12-04T11:29:31.577+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='DP'/><category scheme='http://www.blogger.com/atom/ns#' term='Math'/><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>TopCoder - SRM 144 DIV 1 - Lottery - sortByOdds</title><content type='html'>&amp;nbsp;If you read the problem statement carefully, you'll find out that we are facing 'blanks' spots that in each spot we can put [1, 'choices'], for simplicity n = blanks, up = choices&lt;br /&gt;I categorize this problem as a counting problem in Combinatorics. &lt;br /&gt;&lt;br /&gt;We can break this problem to 4 independent subproblems &lt;b&gt;:&lt;/b&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;&lt;b&gt;Case 1&lt;/b&gt;&lt;/span&gt; : if sorted = false and unique = false, there is no condition, we want to fill each spot, and each spot has up candidates, so the answer is :&lt;br /&gt;(up * up * ... * up)[n times] = up^n&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Case 2&lt;/b&gt; : if sorted = false &amp;amp;&amp;amp; unique = true, what we put in n spots have to be unique, we have to choose n numbers out of up numbers and put all of the permutations of these numbers in the n spots so the answer is :&lt;br /&gt;combination(up, n) * n! = ( (up!) / ((up-n)! * n!) )* n! = up! / (up-n)! = permutation(up, n)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Case 3&lt;/b&gt; : if sorted = true &amp;amp;&amp;amp; unique = true, what we put in spots have to be unique and sorted, so we have to choose n numbers out of up numbers and put the sorted version of this combination in the n spots, so the answer is :&lt;br /&gt;combination(up, n) = up! / ((up-n)! * n!)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Case 4&lt;/b&gt; : if sorted = true &amp;amp;&amp;amp; unique = false, at this case suppose we have an array of size n and we want to fill the spots with numbers in range [1, up] and they have to be non-decreasing. e. g. n = 3 &amp;amp; up = 5 then [1, 2, 3], [1, 1, 1], [1, 2, 2], [5, 5, 5] and .... have to be counted in this case.&lt;br /&gt;So we want the number of sorted arrays that are filled with numbers in range [1, up]. I tried to find a recurrent formula for this, think of the array like this that from left to right the indices decrease from n to 1, a[n], a[n-1], a[n-2] ... a[2], a[1]&lt;br /&gt;mem(k, low) is the number of sorted arrays that start from index k and what we can put in this spot is at least low and at most up, what can we do to break the problem? Each time we can put a number at spot k and invoke mem(k-1, something), but what is 'something'? Yes, each time we put T = low...up and call mem(k-1, T), in this way we fix spot number k to T and tell the next index that you can be at least as T because I have fixed previous spot T, so mem(k, low) = mem(k-1, low) + mem(k-1, low+1) + mem(k-1, low+2) + ... + mem(k-1, up-1) + mem(k-1, up)&lt;br /&gt;What is the trivial case? if k is 1(when we have just 1 spot) how many ways can we fill this spot? There are (up-low+1) ways to fill this spot. So the answer is :&lt;br /&gt;mem(n, 1)&lt;br /&gt;At this case we have to memoize the answer to prevent from a "Time Limit Exceeded" verdict.&lt;br /&gt;&lt;br /&gt;To calculate combination(n, k) we have a recurrent formula that says combination(n, k) = combination(n-1, k-1) + combination(n-1, k), I also memoized this.&lt;br /&gt;&lt;br /&gt;n! = n * (n-1)! --&amp;gt;&amp;gt; I memoized this too.&lt;br /&gt;&lt;br /&gt;To calculate p^n, I wrote a function to calculate it in O(logn) with the fact that p^n = p^(n/2) * p^(n/2)&lt;br /&gt;&lt;br /&gt;:-D But the problem wants something more than what I described. We have to parse the input and produce output. We need a struct like pair&lt;int, string=""&gt; or something like that.&lt;/int,&gt;&lt;br /&gt;I described a struct by myself :&lt;br /&gt;struct str&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; &amp;nbsp; int p;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string name;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; str(int _p = 0, string _name = "")&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p = _p, name = name;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; bool operator&amp;lt;(const str &amp;amp;two) const&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return p &amp;lt; two.p &amp;amp;&amp;amp; (p == two.p &amp;amp;&amp;amp; name &amp;lt; two.name);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;I parsed input like this&lt;br /&gt;string &amp;amp;s = rules[i];&lt;br /&gt;string name = s.substr(0, s.find(':'));&lt;br /&gt;string rest = s.substr(s.find(':')+1, 1000);&lt;br /&gt;then I stringstream 'rest' and token up, n, sorted, unique.&lt;br /&gt;then calculate probability, sort and produce output.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-890503442977020485?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/890503442977020485/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2011/12/topcoder-srm-144-div-1-lottery.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/890503442977020485'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/890503442977020485'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2011/12/topcoder-srm-144-div-1-lottery.html' title='TopCoder - SRM 144 DIV 1 - Lottery - sortByOdds'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-7806119197809503340</id><published>2011-01-12T11:38:00.002+03:30</published><updated>2011-01-12T11:40:02.398+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>TopCoder - SRM 146 DIV 1 - RectangularGrid - countRectangles</title><content type='html'>&lt;a href="http://www.topcoder.com/stat?c=problem_statement&amp;amp;pm=1589"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;In this problem you have to count the number of none-square rectangles in a given rectangle.&lt;br /&gt;What I do is this, that I try to count all of&lt;br /&gt;1*2, 1*3 ... 1*w&lt;br /&gt;2*3, 2*4.... 2*w&lt;br /&gt;......................&lt;br /&gt;rectangles. &lt;br /&gt;&lt;br /&gt;and I do this once for my rectangle and another time with the new rectangle with width and height swapped.&lt;br /&gt;this is my code:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; long long func(int w, int h)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; long long res = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = 1; i &amp;lt;= h; i ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int j = i+1; j &amp;lt;= w; j ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; res += (w-j+1) * (h-i+1);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return res;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; long long countRectangles(int w, int h) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return func( w, h ) + func( h, w );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;How did I find this solution?&lt;/span&gt; I just tried to check it out on a piece of paper and I saw that there is a template for placing the rectangles with different width and heights in a larger rectangle.&lt;br /&gt;For example if you want to place a 1*2 rectangle in a large rectangle, first you have to count how many of them are placed in a single row, (width-2+1) and multiply this number in height that is the number of 1*2 rectangles that are placed horizontally in a rectangle.&lt;br /&gt;&lt;br /&gt;There are another solutions that I'm thinking about them, using 4 and 3 loops that I think they are trivial, and using 1 loop or even no loop that I'm thinking to understand them completely, then I'm gonna add them here.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-7806119197809503340?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/7806119197809503340/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2011/01/topcoder-srm-146-div-1-rectangulargrid.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7806119197809503340'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7806119197809503340'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2011/01/topcoder-srm-146-div-1-rectangulargrid.html' title='TopCoder - SRM 146 DIV 1 - RectangularGrid - countRectangles'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-402417391782226096</id><published>2011-01-04T18:06:00.000+03:30</published><updated>2011-01-04T18:06:24.042+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>TopCoder - SRM 145 DIV 1 - Bonuses - getDivision</title><content type='html'>&lt;a href="http://www.topcoder.com/stat?c=problem_statement&amp;amp;pm=1677"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;This is a straight forward simulation problem. As described in the problem statement you find each persons percentage amounts and then you have to distribute the left over percentage among those who have greater points and if they have equal points, give extra 1% to who comes first in input and of course keep track of this person not to give extra 1% to him again.&lt;br /&gt;&lt;br /&gt;vector &lt;int&gt; getDivision(vector &lt;int&gt; p) &lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int n = p.size();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; vector &lt;int&gt; out( n );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int sum = accumulate( p.begin(), p.end(), 0 );&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = 0; i &amp;lt; n; i ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; out[i] = p[i]*100 / sum;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int sum2 = accumulate( out.begin(), out.end(), 0 );&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; vector &lt;bool&gt; mark(n, 0);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for( ; sum2 &amp;lt; 100; sum2 ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; int mx = -1, id = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int j = 0; j &amp;lt; n; j ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if( mx &amp;lt; p[j] &amp;amp;&amp;amp; mark[j] == 0 )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; mx = p[j], id = j;&lt;/bool&gt;&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; out[id] ++, mark[id] = 1;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return out;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-402417391782226096?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/402417391782226096/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2011/01/topcoder-srm-145-div-1-bonuses.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/402417391782226096'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/402417391782226096'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2011/01/topcoder-srm-145-div-1-bonuses.html' title='TopCoder - SRM 145 DIV 1 - Bonuses - getDivision'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-2904201511123635555</id><published>2011-01-04T16:34:00.001+03:30</published><updated>2011-01-04T16:35:47.910+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginner'/><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>TopCoder - SRM 145 DIV 2 - DitherCounter - count</title><content type='html'>&lt;a href="http://www.topcoder.com/stat?c=problem_statement&amp;amp;pm=1728&amp;amp;rd=4530&amp;amp;rm=129589"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;In this problem you have to count how many of the characters of &lt;b&gt;screen &lt;/b&gt;exist in &lt;b&gt;dithered, &lt;/b&gt;to do this you can loop over the elements of &lt;b&gt;screen&lt;/b&gt; and then for each element of &lt;b&gt;screen&lt;/b&gt; search in &lt;b&gt;dithered &lt;/b&gt;whether that character is there or not, O( n^3 ). But I do this O( n^2 ), since characters are only uppercase letters, at first I mark which characters are there in &lt;b&gt;dithered, &lt;/b&gt;and then loop over &lt;b&gt;screen&lt;/b&gt; and for each character it takes O( 1 ) to check whether it is in &lt;b&gt;dithered &lt;/b&gt;or not.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-2904201511123635555?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/2904201511123635555/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2011/01/topcoder-srm-145-div-2-dithercounter.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2904201511123635555'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2904201511123635555'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2011/01/topcoder-srm-145-div-2-dithercounter.html' title='TopCoder - SRM 145 DIV 2 - DitherCounter - count'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-1569222000995952545</id><published>2011-01-04T02:23:00.001+03:30</published><updated>2011-01-04T02:29:38.690+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>TopCoder - SRM 144 DIV 1 - BinaryCode - decode</title><content type='html'>&lt;a href="http://www.topcoder.com/stat?c=problem_statement&amp;amp;pm=1704&amp;amp;rd=4515"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The problem sounds a little bit tricky at first but the test cases show you what you have to do. At first, when you assume about the first element of source to be '0' or '1', you can find the rest of them easily. But while doing this you have to take care about what you are pushing back into source, if it is some characters other than '0' or '1' the answer for this assumption is "NONE", and at the end I try to rebuild q from my result and see whether this q is equal to the given q or not? Why? Cause source[q.size()] must be equal to '0' because it's outside the string. There is one other way to do this, that while you are constructing source from given q, check whether source[q.size()] is equal to '0' or not, and there is no need to rebuild q from source.&lt;br /&gt;&lt;br /&gt;This function returns the i-th element of string s, if i is out of range returns 0 &lt;br /&gt;int f( string &amp;amp;s, int i )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(i &amp;lt; 0 || i &amp;gt;= s.size() )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  return 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return s[i]-'0';&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;This function checks whether the source that I've decoded is valid or not, by checking whether it has characters other than '0' and '1' or not, and checking that if I build q from s, is it equal to the given q or not?&lt;br /&gt;bool valid( string &amp;amp;s, string &amp;amp;q )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = 0; i &amp;lt; s.size(); i ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if( s[i] != '0' &amp;amp;&amp;amp; s[i] != '1' )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string t(q.size(), '0');&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = 0; i &amp;lt; s.size(); i ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  t[i] = f(s, i-1) + f(s, i) + f(s, i+1) + '0';&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if( q != t )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return false;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return true;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-1569222000995952545?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/1569222000995952545/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2011/01/topcoder-srm-144-div-1-binarycode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1569222000995952545'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1569222000995952545'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2011/01/topcoder-srm-144-div-1-binarycode.html' title='TopCoder - SRM 144 DIV 1 - BinaryCode - decode'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-136908074079198271</id><published>2011-01-04T00:26:00.003+03:30</published><updated>2011-01-04T02:28:54.941+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Beginner'/><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>TopCoder - SRM 144 DIV 2 - Time - whatTime</title><content type='html'>&lt;a href="http://www.topcoder.com/stat?c=problem_statement&amp;amp;pm=1708&amp;amp;rd=4515"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;In this problem, you have to convert a given second into it's equivalent standard time "h:m:s".&lt;br /&gt;Easily I find how many 3600 are there in it, how many 60 are there left in it, and now I have h, m and s that I have to return a string not integers, so I convert each of them into a string and add them together in one string and return it.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;int h = seconds/3600;&lt;br /&gt;int m = (seconds%3600)/60;&lt;br /&gt;int s = seconds%60;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return f(h) + ":" + f(m) + ":" + f(s);&lt;br /&gt;&lt;br /&gt;Function f(int) just converts the given integer into a string like this:&lt;br /&gt;int f( int n )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp; if( n == 0 )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return "0";&lt;br /&gt;&amp;nbsp;&amp;nbsp; string res = "";&lt;br /&gt;&amp;nbsp;&amp;nbsp; while( n )&lt;br /&gt;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; res += n%10 + '0';&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n /= 10;&lt;br /&gt;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp; reverse( res.begin(), res.end() );&lt;br /&gt;&amp;nbsp;&amp;nbsp; return res;&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-136908074079198271?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/136908074079198271/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2011/01/topcoder-srm-144-div-2-whattime.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/136908074079198271'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/136908074079198271'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2011/01/topcoder-srm-144-div-2-whattime.html' title='TopCoder - SRM 144 DIV 2 - Time - whatTime'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-8771151505035759223</id><published>2010-12-31T19:03:00.000+03:30</published><updated>2010-12-31T19:03:57.495+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>UVA - 10082 - WERTYU</title><content type='html'>&lt;a href="http://uva.onlinejudge.org/external/100/10082.html"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Ad Hoc&lt;br /&gt;&lt;br /&gt;Easily you have to convert each character in the input to its left character on the keyboard.&lt;br /&gt;&lt;br /&gt;string kb = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;\'ZXCVBNM,./";&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while( getline(cin, line) )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = 0; i &amp;lt; line.size(); i ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; (line[i] != ' ' ? kb[ kb.find(line[i]) - 1 ] : ' ');&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; endl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&lt;br /&gt;In this solution I saved all the keyboard in a string named kb, and for each character, I&amp;nbsp; find it in the string kb and print the previous character.&lt;br /&gt;I use a good member function of class string, stringName.find( ch ) that return the position of ch in stringName or string::pos if ch is not in the string.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-8771151505035759223?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/8771151505035759223/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-10082-wertyu.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8771151505035759223'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8771151505035759223'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-10082-wertyu.html' title='UVA - 10082 - WERTYU'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-1863281079493811821</id><published>2010-12-31T18:42:00.000+03:30</published><updated>2010-12-31T18:42:32.159+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>UVA - 572 - Oil Deposits</title><content type='html'>&lt;a href="http://uva.onlinejudge.org/external/5/572.html"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;graph algorithms, finding number of components using DFS or BFS&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If you read the problem statement carefully you can see that you have to find the number of components of a graph, I use DFS to do that.&lt;br /&gt;&lt;br /&gt;in body of main :&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = 0; i &amp;lt; m; i ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int j = 0; j &amp;lt; n; j ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if( grid[i][j] == '@' &amp;amp;&amp;amp; mark[i][j] == false )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; dfs( i, j ), comp ++;&lt;br /&gt;&lt;br /&gt;void dfs( int r, int c )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mark[r][c] = true;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = r-1; i &amp;lt;= r+1; i ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int j = c-1; j &amp;lt;= c+1; j ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if( inRange( i, j ) &amp;amp;&amp;amp; grid[i][j] == '@' &amp;amp;&amp;amp; mark[i][j] == false )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; dfs( i, j );&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-1863281079493811821?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/1863281079493811821/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-572-oil-deposits.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1863281079493811821'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1863281079493811821'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-572-oil-deposits.html' title='UVA - 572 - Oil Deposits'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-1908045432966765656</id><published>2010-12-31T18:35:00.001+03:30</published><updated>2010-12-31T18:35:56.767+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>UVA - 11063 - B2-sequence</title><content type='html'>&lt;a href="http://uva.onlinejudge.org/external/110/11063.html"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Simulation - STL - set&lt;br /&gt;complete search&lt;br /&gt;&lt;br /&gt;take care about negative numbers&lt;br /&gt;take care about repetitive numbers&lt;br /&gt;take care about this numbers have to be in increasing order&lt;br /&gt;1 &amp;lt;= b1 &amp;lt; b2 &amp;lt; b3 ...&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if( a[i] &amp;lt;= 0 )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; isB2 = false;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if( i &amp;gt;= 1 &amp;amp;&amp;amp; a[i] &amp;lt;= a[i-1] )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; isB2 = false;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if( mark.count( a[i]+a[j] ) == true )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; isB2 = false;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-1908045432966765656?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/1908045432966765656/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-11063-b2-sequence.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1908045432966765656'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1908045432966765656'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-11063-b2-sequence.html' title='UVA - 11063 - B2-sequence'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-8942141041208201766</id><published>2010-12-31T18:28:00.000+03:30</published><updated>2010-12-31T18:28:48.547+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>UVA - 11340 - Newspaper</title><content type='html'>&lt;a href="http://uva.onlinejudge.org/external/113/11340.html"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;string - Ad Hoc&lt;br /&gt;&lt;br /&gt;As you can see in the problem description, some characters with their costs are given to you, a passage comes after that, and you have to count each character in that passage and find out how much money publisher must pay for a text, characters that are not given a specific cost have to be considered with cost 0.&lt;br /&gt;After I read and count occurrence of each character, I find the cost of the passage with these two lines of code :&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = 0; i &amp;lt; 400; i ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; cent += occurrence[i] * cost[i];&lt;br /&gt;and print output in this manner :&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; int dol = cent / 100;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; cent %= 100;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; dol &amp;lt;&amp;lt; "." &amp;lt;&amp;lt; (cent &amp;lt; 10 ? "0": "") &amp;lt;&amp;lt; cent &amp;lt;&amp;lt; "$" &amp;lt;&amp;lt; endl;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-8942141041208201766?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/8942141041208201766/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-11340-newspaper.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8942141041208201766'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8942141041208201766'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-11340-newspaper.html' title='UVA - 11340 - Newspaper'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-5878324636004995951</id><published>2010-12-31T18:11:00.001+03:30</published><updated>2010-12-31T18:14:40.505+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>UVA - 11228 - Transportation System</title><content type='html'>&lt;a href="http://uva.onlinejudge.org/external/112/11228.html"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;graph algorithms, minimum spanning tree, Prim - Kruskal&lt;br /&gt;&lt;br /&gt;In this problem you have to find several MSTs. (one MST in each state and one MST between all states)&lt;br /&gt;First you have to find out which cities are in each state and then run MST for that state and&lt;br /&gt;then suppose each state as a node that have railroads to other states(nodes) and run MST for the whole states that now are considered as single nodes.&lt;br /&gt;&lt;br /&gt;I want to describe all my code here.&lt;br /&gt;I have a struct to save edge i--&amp;gt;j and the cost :&lt;br /&gt;struct edge&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int p, q;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; double d;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; edge ( int _p = 0, int _q = 0, double _d = 0 )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; p = _p, q = _q, d = _d;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; const bool operator &amp;lt;( const edge &amp;amp;two ) const&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return d &amp;lt; two.d;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;I save my input in this datastructure :&lt;br /&gt;double p[1001][2];&lt;br /&gt;&lt;br /&gt;This function gives me the distance between point 'i' and point 'j' : &lt;br /&gt;double distF( int i, int j )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return sqrt( (p[i][0]-p[j][0])*(p[i][0]-p[j][0]) + (p[i][1]-p[j][1])*(p[i][1]-p[j][1])&amp;nbsp; );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;I use disjoint set datastructure two time in my code, first to find out whether point A and point B are in the same state, how do I do that? I mark each node with par[i],&lt;br /&gt;that means if for example par[2] = 3 node 2 is in the same state as 3 is and 3 is the representative of the state( or set ), and par[4] = 5 mean that node 4 is in the same state as 5 and the representative of the set is 5, then if I find an edge between node 2 and node 4, I have to merge these two sets, and I do it with function merge(int, int), and whenever I want to know whether two nodes are in the same state or not, I use function find(int) which gives me the representative of the set which my node is in and then compare find( x ) and find( y ) if they are equal they are in the same set, otherwise I have to merge them.&lt;br /&gt;int find( int x )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(x == par[x])&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return x;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return par[x] = find( par[x] );&lt;br /&gt;}&lt;br /&gt;void merge( int x, int y )&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; par[ find(x) ] = find( y );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Now I'm ready to read input, I save input in p[i][0] and p[i][1] and then run distF( int, int ) and set the distance between point 'i' and point 'j' and then run disjoint set datastructure to find out which nodes are in the same state( as mentioned in the problem description two nodes are in the same state if the distance between them is less than 'r' ), now I have some states that I have to run MST in each state and after that I have to look at each state as single node, that have several edges to other states, so now I have to find the complete graph of the states, and then run MST for that, but between each node I have several edges( a multigraph ) so I have to choose the minimum edge between two states( suppose state 'i' has A nodes and state 'j' has B nodes, there are A*B edges between these two states that I just want the edge with minimum cost ), so I construct my graph in this way and run MST for that.&lt;br /&gt;This is my MST body, "all" is a vector of edge, that all the edges of a graph are pushed back into it. &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sort( all.begin(), all.end() );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i = 0; i &amp;lt; n; i ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; par[i] = i;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(int k = 0; k &amp;lt; all.size(); k ++)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if( find( all[k].p ) == find( all[k].q ) )&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; continue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; railRoads += all[k].d;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; merge( all[k].p, all[k].q );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-5878324636004995951?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/5878324636004995951/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-11228-transportation-system.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/5878324636004995951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/5878324636004995951'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-11228-transportation-system.html' title='UVA - 11228 - Transportation System'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-8173645679551351304</id><published>2010-12-12T23:06:00.000+03:30</published><updated>2010-12-12T23:06:23.059+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>UVA - 10077 - The Stern-Brocot Number System</title><content type='html'>&lt;a href="http://uva.onlinejudge.org/external/100/10077.html" target="_blank"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Divide and Conquer, like binary search&lt;br /&gt;&lt;br /&gt;In this problem You have a number a/b that want to construct it as stated in the problem description.&lt;br /&gt;In each step you have to decide whether go left or go right? So you compare a/b with your current number which is (s1+t1)/(s2+t2) and decide to go right or left and when you arrive in a level that you've found the number, write the string that you've constructed.&lt;br /&gt;Initially s1=0, t1=1 and s2=1, t2=0.&lt;br /&gt;I have a function which takes ( s1, s2, a, b, t1, t2 ) and Divides and Conquers. :-D&lt;br /&gt;if( b*(s1+t1) &amp;lt; a*(s2+t2) ) D_and_C( s1+t1, s2+t2, a, b, t1, t2 ), output += 'R';&lt;br /&gt;else D_and_C( s1, s2, a, b, t1+s1, t2+s2 ), output += 'L';&lt;br /&gt;if( a==s1+t1 &amp;amp;&amp;amp; b==s2+t2 ) print output;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-8173645679551351304?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/8173645679551351304/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-10077-stern-brocot-number-system.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8173645679551351304'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8173645679551351304'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-10077-stern-brocot-number-system.html' title='UVA - 10077 - The Stern-Brocot Number System'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-8367314252236843504</id><published>2010-12-12T22:25:00.001+03:30</published><updated>2010-12-12T22:26:40.785+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>UVA - 10167 - Birthday Cake</title><content type='html'>&lt;a href="http://uva.onlinejudge.org/external/101/10167.html"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;computational geometry, brute force, complete search&lt;br /&gt;&lt;br /&gt;In this problem You have a cake with radius 100 and 2N cherries that are placed on the cake.&lt;br /&gt;The center of the cake is Origin, You have to cut the cake in 2 parts in a fairly manner that each part has equal cherries and no cherry is on the beeline of division.&lt;br /&gt;Since There are at most 100 cherries and the beeline must go through the center of the cake and the radius of the cake is 100, You can set (A &amp;gt;= 0 &amp;amp;&amp;amp; A &amp;lt;= 100) and (B &amp;gt;= -100 &amp;amp;&amp;amp; B &amp;lt;= 100) and each time make a beeline with this A and B (Ax+By=0) that goes through the Origin and check whether there are equal cherries&lt;br /&gt;in each side of the beeline and no cherry is on the beeline, if yes print x and y and break.&lt;br /&gt;Check if( A*x[i]+B*y[i] &amp;gt; 0 ) one ++; else if( A*x[i]+B*y[i] &amp;lt; 0 ) two ++;&lt;br /&gt;if( one == n &amp;amp;&amp;amp; two == n ) print A and B.&lt;br /&gt;In this way you will get an accept in the suggested time limit.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-8367314252236843504?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/8367314252236843504/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-10167-birthday-cake.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8367314252236843504'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8367314252236843504'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-10167-birthday-cake.html' title='UVA - 10167 - Birthday Cake'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-2337187820716223798</id><published>2010-12-12T18:52:00.000+03:30</published><updated>2010-12-12T18:52:49.566+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>UVA - 11792 - Krochanska is Here!</title><content type='html'>&lt;a href="http://uva.onlinejudge.org/external/117/11792.html"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;graph search algorithm - single source unweighted shortest path - BFS from some sources&lt;br /&gt;&lt;br /&gt;You are facing a multi BFS problem that you have to run BFS from multiple sources, that are called important stations here.&lt;br /&gt;First I have a boolean 2d array "mark" that I can figure out that in each line which stations are used and find out which stations are "important stations".&lt;br /&gt;Important stations are stations that mark is true for that station in more than one line.&lt;br /&gt;Then easily I run BFS for each important station and find the sum of the distances from&lt;br /&gt;the current important station that I've run BFS from to the other important stations and save that in a vector &lt;int&gt; ave, and finally I find the minimum sum of distances from vector &lt;int&gt; ave and print output.&lt;br /&gt;And how I save my graph : I use a vector &lt;int&gt; adj[10001] and try to save each vertex's&lt;br /&gt;neighbours in that.&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-2337187820716223798?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/2337187820716223798/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-11792-krochanska-is-here.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2337187820716223798'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2337187820716223798'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/uva-11792-krochanska-is-here.html' title='UVA - 11792 - Krochanska is Here!'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-5071958580034911259</id><published>2010-12-05T23:17:00.002+03:30</published><updated>2010-12-06T02:15:33.392+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='USACO'/><title type='text'>USACO - Calf Flac</title><content type='html'>&lt;center&gt; &lt;span style="font-size: large;"&gt;&lt;b&gt;Calf Flac&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/center&gt;   It is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world's great palindromes. Your job will be to detect these bovine beauties.  &lt;br /&gt;Ignore punctuation, whitespace, numbers, and case when testing for palindromes, but keep these extra characters around so that you can print them out as the answer; just consider the letters `A-Z' and `a-z'.  &lt;br /&gt;Find the largest palindrome in a string no more than 20,000 characters long. The largest palindrome is guaranteed to be at most 2,000 characters long before whitespace and punctuation are removed.  &lt;br /&gt;&lt;h3&gt;PROGRAM NAME: calfflac&lt;/h3&gt;&lt;h3&gt;INPUT FORMAT&lt;/h3&gt;A file with no more than 20,000 characters. The file has one or more lines. No line is longer than 80 characters (not counting the newline at the end).  &lt;br /&gt;&lt;h3&gt;SAMPLE INPUT (file calfflac.in) &lt;/h3&gt;&lt;pre&gt;Confucius say: Madam, I'm Adam.&lt;/pre&gt;&lt;h3&gt;OUTPUT FORMAT&lt;/h3&gt;The first line of the output should be the length of the longest palindrome found. The next line or lines should be the actual text of the palindrome (without any surrounding white space or punctuation but with all other characters) printed on a line (or more than one line if newlines are included in the palindromic text). If there are multiple palindromes of longest length, output the one that appears first.  &lt;br /&gt;&lt;h3&gt;SAMPLE OUTPUT (file calfflac.out)&lt;/h3&gt;&lt;pre&gt;11&lt;br /&gt;Madam, I'm Adam&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre&gt;&lt;/pre&gt;Greedy - longest palindrome with size less than 2000 - brute force&lt;br /&gt;&lt;br /&gt;In this problem you have a string with size 20,000 and want to find the longest palindrome with size less than 2000, but there are non-alphabetical characters in the input file. So I first read input, I have two arrays for input, one is the input and the other is the transformed input and indexing of the alphabets that I have saved in lower case.&lt;br /&gt;Then I iterate from 0 to size of input and each time I check two things: First : if current character is the middle character, how much can we extend the palindrome from left and right, So I iterate 1000 to left and 1000 to right and wherever it's not yet a palindrome I stop continuing to expand the palindrome,this is when the size of the palindrome is odd, when the size of the palindrome is even I set the current and next character as the core of my palindrome with size even, and again expand the current palindrome. In this approach time complexity is O(20,000 * 2000).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-5071958580034911259?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/5071958580034911259/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-calf-flac.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/5071958580034911259'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/5071958580034911259'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-calf-flac.html' title='USACO - Calf Flac'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-8612421891866717182</id><published>2010-12-05T23:13:00.002+03:30</published><updated>2010-12-06T02:15:35.978+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='USACO'/><title type='text'>USACO - Prime Cryptarithm</title><content type='html'>&lt;center&gt; &lt;span style="font-size: large;"&gt;&lt;b&gt;Prime Cryptarithm&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/center&gt;   The following cryptarithm is a multiplication problem that can be solved by substituting digits from a specified set of N digits into the positions marked with *. If the set of prime digits {2,3,5,7} is selected, the cryptarithm is called a PRIME CRYPTARITHM. &lt;br /&gt;&lt;pre&gt;* * *&lt;br /&gt;   x    * *&lt;br /&gt;    -------&lt;br /&gt;      * * *         &amp;lt;-- partial product 1&lt;br /&gt;    * * *           &amp;lt;-- partial product 2&lt;br /&gt;    -------&lt;br /&gt;    * * * *&lt;/pre&gt;Digits can appear only in places marked by `*'.  Of course, leading zeroes are not allowed.  Note that the 'partial products' are as taught in USA schools. The first partial product is the product of the final digit of the second number and the top number.  The second partial product is the product of the first digit of the second number and the top number.  &lt;br /&gt;Write a program that will find all solutions to the cryptarithm above for any subset of digits from the set {1,2,3,4,5,6,7,8,9}.  &lt;br /&gt;&lt;h3&gt;PROGRAM NAME: crypt1&lt;/h3&gt;&lt;h3&gt;INPUT FORMAT&lt;/h3&gt;&lt;table border="1"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;Line 1: &lt;/td&gt; &lt;td&gt;N, the number of digits that will be used&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;Line 2: &lt;/td&gt; &lt;td&gt;N space separated digits with which to solve the cryptarithm &lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h3&gt;SAMPLE INPUT (file crypt1.in) &lt;/h3&gt;&lt;pre&gt;5&lt;br /&gt;2 3 4 6 8&lt;/pre&gt;&lt;h3&gt;OUTPUT FORMAT&lt;/h3&gt;A single line with the total number of unique solutions.  Here is the single solution for the sample input: &lt;br /&gt;&lt;pre&gt;2 2 2&lt;br /&gt;    x   2 2&lt;br /&gt;     ------&lt;br /&gt;      4 4 4&lt;br /&gt;    4 4 4&lt;br /&gt;  ---------&lt;br /&gt;    4 8 8 4&lt;/pre&gt;&lt;h3&gt;SAMPLE OUTPUT (file crypt1.out)&lt;/h3&gt;&lt;pre&gt;1&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre&gt;&lt;/pre&gt;Ad hoc - iteration - simulation&lt;br /&gt;&lt;br /&gt;There are two ways to solve this problem. &lt;br /&gt;First you can easily iterate from a=100 to a=999 and from b=10 to b=99 in nested for loops, then you can produce the product of a and b and check whether the digits of a, b and a*b are valid or not, if yes increment your output. This approach runs in O(1000 * 100).&lt;br /&gt;Second you can set the digits of a and b, a[0], a[1], a[2] and b[0], b[1] from your input, in this way you don't have to check the validity of a[i] and b[i] and then produce the product of a and b, and now check whether the digits of a*b are valid or not. This approach runs in O(9^5) that is a little better but harder to implement.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-8612421891866717182?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/8612421891866717182/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-prime-cryptarithm.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8612421891866717182'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8612421891866717182'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-prime-cryptarithm.html' title='USACO - Prime Cryptarithm'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-4218710529258425044</id><published>2010-12-05T23:08:00.001+03:30</published><updated>2010-12-06T02:15:22.225+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='USACO'/><title type='text'>USACO - Barn Repair</title><content type='html'>&lt;center&gt; &lt;h1&gt;&lt;span style="font-size: large;"&gt;Barn Repair&lt;/span&gt;&lt;/h1&gt;&lt;/center&gt;   It was a dark and stormy night that ripped the roof and gates off the stalls that hold Farmer John's cows.  Happily, many of the cows were on vacation, so the barn was not completely full.  &lt;br /&gt;The cows spend the night in stalls that are arranged adjacent to each other in a long line.  Some stalls have cows in them; some do not. All stalls are the same width.  &lt;br /&gt;Farmer John must quickly erect new boards in front of the stalls, since the doors were lost.  His new lumber supplier will supply him boards of any length he wishes, but the supplier can only deliver a small number of total boards.  Farmer John wishes to minimize the total length of the boards he must purchase.  &lt;br /&gt;Given M (1 &amp;lt;= M &amp;lt;= 50), the maximum number of boards that can be purchased; S (1 &amp;lt;= S &amp;lt;= 200), the total number of stalls; C (1 &amp;lt;= C &amp;lt;= S) the number of cows in the stalls, and the C occupied stall numbers (1 &amp;lt;= stall_number &amp;lt;= S), calculate the minimum number of stalls that must be blocked in order to block all the stalls that have cows in them.  &lt;br /&gt;Print your answer as the total number of stalls blocked.   &lt;br /&gt;&lt;h3&gt;PROGRAM NAME: barn1&lt;/h3&gt;&lt;h3&gt;INPUT FORMAT&lt;/h3&gt;&lt;table border="1"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;Line 1: &lt;/td&gt; &lt;td&gt;M, S, and C (space separated) &lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;Lines 2-C+1: &lt;/td&gt; &lt;td&gt;Each line contains one integer, the number of an occupied stall. &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h3&gt;SAMPLE INPUT (file barn1.in) &lt;/h3&gt;&lt;pre&gt;4 50 18&lt;br /&gt;3&lt;br /&gt;4&lt;br /&gt;6&lt;br /&gt;8&lt;br /&gt;14&lt;br /&gt;15&lt;br /&gt;16&lt;br /&gt;17&lt;br /&gt;21&lt;br /&gt;25&lt;br /&gt;26&lt;br /&gt;27&lt;br /&gt;30&lt;br /&gt;31&lt;br /&gt;40&lt;br /&gt;41&lt;br /&gt;42&lt;br /&gt;43&lt;/pre&gt;&lt;h3&gt;OUTPUT FORMAT&lt;/h3&gt;A single line with one integer that represents the total number of stalls blocked.  &lt;br /&gt;&lt;h3&gt;SAMPLE OUTPUT (file barn1.out)&lt;/h3&gt;&lt;pre&gt;25&lt;/pre&gt;[One minimum arrangement is one board covering stalls 3-8, one covering 14-21, one covering 25-31, and one covering 40-43.]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;Greedy - sorting - finding maximum difference each time&lt;br /&gt;&lt;br /&gt;Here you have a problem that you have C numbers and you want to split them in M-1 places so remains M sequences that the sum of the difference between the first and last number is each sequence becomes minimum.&lt;br /&gt;For example something like this :&lt;br /&gt;a1 a2 a3 ..... aC&lt;br /&gt;you have to split this sequence in M-1 places so that :&lt;br /&gt;aK1-a1+1&amp;nbsp;&amp;nbsp;&amp;nbsp; +&amp;nbsp;&amp;nbsp;&amp;nbsp; aK3-aK2+1&amp;nbsp;&amp;nbsp; +&amp;nbsp;&amp;nbsp;&amp;nbsp; aK4-aK3+1&amp;nbsp;&amp;nbsp; + ..... +&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; aC-aKM-1 +1&amp;nbsp;&amp;nbsp; is minimized.&lt;br /&gt;First you have to sort the numbers. Then each time you have to find the maximum difference between two adjacent numbers and choose it to be the index you want to split your sequence. Do this M-1 times and then you have M sequences that the sum of the differences between the first and last number in each sequence is minimum.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-4218710529258425044?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/4218710529258425044/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-barn-repair.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/4218710529258425044'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/4218710529258425044'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-barn-repair.html' title='USACO - Barn Repair'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-333036854718145706</id><published>2010-12-05T23:05:00.001+03:30</published><updated>2010-12-06T02:15:16.662+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='USACO'/><title type='text'>USACO - Mixing Milk</title><content type='html'>&lt;center&gt; &lt;h1&gt;&lt;span style="font-size: large;"&gt;Mixing Milk&lt;/span&gt;&lt;/h1&gt;&lt;/center&gt;  Since milk packaging is such a low margin business, it is important to keep the price of the raw product (milk) as low as possible. Help Merry Milk Makers get the milk they need in the cheapest possible manner.  &lt;br /&gt;The Merry Milk Makers company has several farmers from which they may buy milk, and each one has a (potentially) different price at which they sell to the milk packing plant. Moreover, as a cow can only produce so much milk a day, the farmers only have so much milk to sell per day. Each day, Merry Milk Makers can purchase an integral amount of milk from each farmer, less than or equal to the farmer's limit.   &lt;br /&gt;Given the Merry Milk Makers' daily requirement of milk, along with the cost per gallon and amount of available milk for each farmer, calculate the minimum amount of money that it takes to fulfill the Merry Milk Makers' requirements.   &lt;br /&gt;Note: The total milk produced per day by the farmers will be sufficient to meet the demands of the Merry Milk Makers.  &lt;br /&gt;&lt;h3&gt;PROGRAM NAME: milk&lt;/h3&gt;&lt;h3&gt;INPUT FORMAT&lt;/h3&gt;&lt;table border="1"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;Line 1: &lt;/td&gt; &lt;td&gt;Two integers, N and M. &lt;br /&gt;The first value, N,  (0 &amp;lt;= N &amp;lt;= 2,000,000) is the amount of milk that Merry Milk Makers' want per day. The second, M, (0 &amp;lt;= M &amp;lt;= 5,000) is the number of farmers that they may buy from. &lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;Lines 2 through M+1: &lt;/td&gt; &lt;td&gt;The next M lines each contain two integers, P&lt;sub&gt;i&lt;/sub&gt; and A&lt;sub&gt;i&lt;/sub&gt;. &lt;br /&gt;P&lt;sub&gt;i&lt;/sub&gt; (0 &amp;lt;= P&lt;sub&gt;i&lt;/sub&gt; &amp;lt;= 1,000) is price in cents that farmer i charges.&lt;br /&gt;A&lt;sub&gt;i&lt;/sub&gt; (0 &amp;lt;= Ai &amp;lt;= 2,000,000) is the amount of milk that farmer i can sell to Merry Milk Makers per day. &lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;h3&gt;SAMPLE INPUT (file milk.in) &lt;/h3&gt;&lt;pre&gt;100 5&lt;br /&gt;5 20&lt;br /&gt;9 40&lt;br /&gt;3 10&lt;br /&gt;8 80&lt;br /&gt;6 30&lt;/pre&gt;&lt;h3&gt;OUTPUT FORMAT&lt;/h3&gt;A single line with a single integer that is the minimum price that Merry Milk Makers can get their milk at for one day.  &lt;br /&gt;&lt;h3&gt;SAMPLE OUTPUT (file milk.out)&lt;/h3&gt;&lt;pre&gt;630&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre&gt;&lt;/pre&gt;Greedy - sorting&lt;br /&gt;&lt;br /&gt;This problem is solved using a straight forward greedy algorithm, you can easily see that if you sort the input by prices and buy milk from those who sell milk cheaper, you will result in the optimum answer.&lt;br /&gt;For example in the sample input you can see that : you need 100 gallons of milk and there are 5 farmers available for you to buy milk from. First you buy&lt;br /&gt;10 gallons from the farmer who sells milk 3 cents per gallon, then&lt;br /&gt;20 gallons from the farmer who sells milk 5 cents per gallon, then&lt;br /&gt;30 gallons from the farmer who sells milk 6 cents per gallon, then&lt;br /&gt;40 gallons from the farmer who sells milk 8 cents per gallon ( this farmer has 80 gallons available but you just need 40 gallons to reach 100 gallons each day )&lt;br /&gt;So the answer is 10*3 + 20*5 + 30*6 + 40*8 = 30 + 100 + 180 + 320 = 630&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-333036854718145706?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/333036854718145706/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-mixing-milk.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/333036854718145706'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/333036854718145706'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-mixing-milk.html' title='USACO - Mixing Milk'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-6129258176678003379</id><published>2010-12-05T23:01:00.001+03:30</published><updated>2010-12-06T02:15:11.041+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='USACO'/><title type='text'>USACO - Transformations</title><content type='html'>&lt;center&gt; &lt;span style="font-size: large;"&gt;&lt;b&gt;Transformations&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/center&gt;   A square pattern of size N x N (1 &amp;lt;= N &amp;lt;= 10) black and white square tiles is transformed into another square pattern.  Write a program that will recognize the minimum transformation that has been applied to the original pattern given the following list of possible transformations:  &lt;br /&gt;&lt;ul&gt;&lt;li&gt;#1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees. &lt;/li&gt;&lt;li&gt;#2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees. &lt;/li&gt;&lt;li&gt;#3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees.&lt;/li&gt;&lt;li&gt;#4: Reflection: The pattern was reflected horizontally (turned into a mirror image of itself by reflecting around a vertical line in the middle of the image).&lt;/li&gt;&lt;li&gt;#5: Combination: The pattern was reflected horizontally and then subjected to one of the rotations (#1-#3). &lt;/li&gt;&lt;li&gt;#6: No Change: The original pattern was not changed. &lt;/li&gt;&lt;li&gt;#7: Invalid Transformation: The new pattern was not obtained by any of the  above methods. &lt;/li&gt;&lt;/ul&gt;In the case that more than one transform could have been used, choose the one with the minimum number above.  &lt;br /&gt;&lt;h3&gt;PROGRAM NAME: transform&lt;/h3&gt;&lt;h3&gt;INPUT FORMAT&lt;/h3&gt;&lt;table border="1"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;Line 1: &lt;/td&gt; &lt;td&gt;A single integer, N &lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;Line 2..N+1: &lt;/td&gt; &lt;td&gt;N lines of N characters (each either   `@' or `-'); this is the square before   transformation&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;Line N+2..2*N+1: &lt;/td&gt; &lt;td&gt;N lines of N characters (each either   `@' or `-'); this is the square after transformation&lt;/td&gt;   &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h3&gt;SAMPLE INPUT (file transform.in) &lt;/h3&gt;&lt;pre&gt;3&lt;br /&gt;@-@&lt;br /&gt;---&lt;br /&gt;@@-&lt;br /&gt;@-@&lt;br /&gt;@--&lt;br /&gt;--@&lt;/pre&gt;&lt;h3&gt;OUTPUT FORMAT&lt;/h3&gt;A single line containing the the number from 1 through 7 (described above) that categorizes the transformation required to change from the `before' representation to the `after' representation.  &lt;br /&gt;&lt;h3&gt;SAMPLE OUTPUT (file transform.out)&lt;/h3&gt;&lt;pre&gt;1&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre&gt;&lt;/pre&gt;Ad hoc - string manipulation&lt;br /&gt;&lt;br /&gt;In my solution I have 3 funcions f90(), cmp() and reflect()&lt;br /&gt;f90( string [], string [], int ); takes two arrays of string and an integer -the size of arrays- and rotates the grid 90 degrees clockwise and put it into the second array of string&lt;br /&gt;cmp( string [], string [], int ); takes two arrays of string and an integer -the size of arrays- and compares them whether they are equal or not&lt;br /&gt;reflect( string [], string [], int ); takes two arrays of string and an integer -the size of arrays- and reflects the first one round a vertical line and put it into the second one&lt;br /&gt;&lt;br /&gt;for rotating 180 and 270 degrees I used 2 or 3 times of f90(), respectively.&lt;br /&gt;read the problem description carefully as it says : "In the case that more than one transform could have been used, choose the one with the minimum number above."&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-6129258176678003379?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/6129258176678003379/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-transformations.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6129258176678003379'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6129258176678003379'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-transformations.html' title='USACO - Transformations'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-7022039317998235872</id><published>2010-12-05T22:55:00.001+03:30</published><updated>2010-12-06T02:15:05.586+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='USACO'/><title type='text'>USACO - Palindromic Squares</title><content type='html'>&lt;center&gt; &lt;span style="font-size: large;"&gt;&lt;b&gt;Palindromic Squares&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;Rob Kolstad &lt;/center&gt;   Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome.  &lt;br /&gt;Given a number base B (2 &amp;lt;= B &amp;lt;= 20 base 10), print all the integers N (1 &amp;lt;= N &amp;lt;= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square.  Use the letters 'A', 'B', and so on to represent the digits 10, 11, and so on.  &lt;br /&gt;Print both the number and its square in base B.  &lt;br /&gt;&lt;h3&gt;PROGRAM NAME: palsquare&lt;/h3&gt;&lt;h3&gt;INPUT FORMAT&lt;/h3&gt;A single line with B, the base (specified in base 10).  &lt;br /&gt;&lt;h3&gt;SAMPLE INPUT (file palsquare.in) &lt;/h3&gt;&lt;pre&gt;10&lt;/pre&gt;&lt;h3&gt;OUTPUT FORMAT&lt;/h3&gt;Lines with two integers represented in base B.  The first integer is the number whose square is palindromic; the second integer is the square itself.  &lt;br /&gt;&lt;h3&gt;SAMPLE OUTPUT (file palsquare.out)&lt;/h3&gt;&lt;pre&gt;1 1&lt;br /&gt;2 4&lt;br /&gt;3 9&lt;br /&gt;11 121&lt;br /&gt;22 484&lt;br /&gt;26 676&lt;br /&gt;101 10201&lt;br /&gt;111 12321&lt;br /&gt;121 14641&lt;br /&gt;202 40804&lt;br /&gt;212 44944&lt;br /&gt;264 69696&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre&gt;&lt;/pre&gt;Brute force - base conversion - palindrome checking&lt;br /&gt;&lt;br /&gt;Produce all the squares of numbers from 1 to 300, convert them to base b and check whether they are palindrome or not, if they are, convert the number to base b too and print both the number and it's square that has been converted to base b.&lt;br /&gt;I have two functions in my solution, toBase( int, int ) and isPal( string ). In toBase() function I convert the given number to base b, and in function isPal() I check whether the given string is palindrome or not with two iterators to the beginning and to the end of the string.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-7022039317998235872?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/7022039317998235872/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-palindromic-squares.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7022039317998235872'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7022039317998235872'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-palindromic-squares.html' title='USACO - Palindromic Squares'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-6045687486235901619</id><published>2010-12-05T22:46:00.002+03:30</published><updated>2010-12-06T02:14:59.411+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='USACO'/><title type='text'>USACO - Dual Palindromes</title><content type='html'>&lt;center&gt; &lt;span style="font-size: large;"&gt;&lt;b&gt;Dual Palindromes&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;b&gt;Mario Cruz (Colombia) &amp;amp; Hugo Rickeboer (Argentina)&lt;/b&gt; &lt;/center&gt;   A number that reads the same from right to left as when read from left to right is called a palindrome.  The number 12321 is a palindrome; the number 77778 is not.  Of course, palindromes have neither leading nor trailing zeroes, so 0220 is not a palindrome.  &lt;br /&gt;The number 21 (base 10) is not palindrome in base 10, but the number 21 (base 10) is, in fact, a palindrome in base 2 (10101).  &lt;br /&gt;Write a program that reads two numbers (expressed in base 10): &lt;br /&gt;&lt;ul&gt;&lt;li&gt; N (1 &amp;lt;= N &amp;lt;= 15)          &lt;/li&gt;&lt;li&gt; S (0 &amp;lt; S &amp;lt; 10000) &lt;/li&gt;&lt;/ul&gt;and then finds and prints (in base 10) the first N numbers strictly greater than S that are palindromic when written in two or more number bases (2 &amp;lt;= base &amp;lt;= 10).   Solutions to this problem do not require manipulating integers larger than the standard 32 bits.  &lt;br /&gt;&lt;h3&gt;PROGRAM NAME: dualpal&lt;/h3&gt;&lt;h3&gt;INPUT FORMAT&lt;/h3&gt;A single line with space separated integers N and S.  &lt;br /&gt;&lt;h3&gt;SAMPLE INPUT (file dualpal.in) &lt;/h3&gt;&lt;pre&gt;3 25&lt;/pre&gt;&lt;h3&gt;OUTPUT FORMAT&lt;/h3&gt;N lines, each with a base 10 number that is palindromic when expressed in at least two of the bases 2..10.  The numbers should be listed in order from smallest to largest.  &lt;br /&gt;&lt;h3&gt;SAMPLE OUTPUT (file dualpal.out)&lt;/h3&gt;&lt;pre&gt;26&lt;br /&gt;27&lt;br /&gt;28&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;/pre&gt;Brute force search - base conversion - palindrome checking&lt;br /&gt;&lt;br /&gt;Easily iterate from s+1 and each time convert the number to all the bases and check whether they are palindrome or not, if the number is palindrome in more than 2 bases, print that number and decrement n.&lt;br /&gt;when I want to convert a number to base b, I used strings, it's simpler to check whether it is palindrome or not, and for checking whether the given string is palindrome or not I use two pointers to the beginning and to the end of the string and check whether they are equal or not, whenever a position is found that they are not equal I return false, otherwise I return true at the end.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-6045687486235901619?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/6045687486235901619/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-dual-palindromes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6045687486235901619'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6045687486235901619'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-dual-palindromes.html' title='USACO - Dual Palindromes'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-6478876872377535751</id><published>2010-12-05T01:10:00.015+03:30</published><updated>2010-12-06T02:14:54.206+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='USACO'/><title type='text'>USACO - Name That Number</title><content type='html'>&lt;center&gt; &lt;b&gt;Name That Number&lt;/b&gt;&lt;br /&gt;&lt;/center&gt;   Among the large Wisconsin cattle ranchers, it is customary to brand cows with serial numbers to please the Accounting Department. The cow hands don't appreciate the advantage of this filing system, though, and wish to call the members of their herd by a pleasing name rather than saying, "C'mon, #4734, get along."  &lt;br /&gt;Help the poor cowhands out by writing a program that will translate the brand serial number of a cow into possible names uniquely associated with that serial number. Since the cow hands all have cellular saddle phones these days, use the standard Touch-Tone(R) telephone keypad mapping to get from numbers to letters (except for "Q" and "Z"):  &lt;br /&gt;&lt;pre&gt;2: A,B,C     5: J,K,L    8: T,U,V&lt;br /&gt;3: D,E,F     6: M,N,O    9: W,X,Y&lt;br /&gt;4: G,H,I     7: P,R,S&lt;/pre&gt;Acceptable names for cattle are provided to you in a file named "dict.txt", which contains a list of fewer than 5,000 acceptable cattle names (all letters capitalized). Take a cow's brand number and report which of all the possible words to which that number maps are in the &lt;a href="http://ace.delos.com/usaco/namenumdict.txt"&gt;given dictionary&lt;/a&gt; which is supplied as dict.txt in the grading environment (and is sorted into ascending order).  &lt;br /&gt;For instance, the brand number 4734 produces all the following names: &lt;br /&gt;&lt;pre&gt;GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDI&lt;br /&gt;GREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEI&lt;br /&gt;GSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFI&lt;br /&gt;HRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDI&lt;br /&gt;HSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEI&lt;br /&gt;IPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFI&lt;br /&gt;ISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI&lt;/pre&gt;As it happens, the only one of these 81 names that is in the list of valid names is "GREG".  &lt;br /&gt;Write a program that is given the brand number of a cow and prints all the valid names that can be generated from that brand number or ``NONE'' if there are no valid names.  Serial numbers can be as many as a dozen digits long.  &lt;br /&gt;&lt;h3&gt;PROGRAM NAME: namenum&lt;/h3&gt;&lt;h3&gt;INPUT FORMAT&lt;/h3&gt;A single line with a number from 1 through 12 digits in length.  &lt;br /&gt;&lt;h3&gt;SAMPLE INPUT (file namenum.in) &lt;/h3&gt;&lt;pre&gt;4734&lt;/pre&gt;&lt;h3&gt;OUTPUT FORMAT&lt;/h3&gt;A list of valid names that can be generated from the input, one per line, in ascending alphabetical order.  &lt;br /&gt;&lt;h3&gt;SAMPLE OUTPUT (file namenum.out)&lt;/h3&gt;&lt;pre&gt;GREG&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;/pre&gt;String - complete search - using STL map, string &amp;amp; vector&lt;br /&gt;&lt;pre style="font-family: inherit;"&gt;&lt;/pre&gt;There are two approaches to this problem:&lt;br /&gt;First : since the input number has at most 12 digits and for each digit there are 3 candidates if you build all the possibilities and then search them in the dictionary the order of your program will become&lt;br /&gt;O(3^12 * nlog(n)) n = 5000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;   531441 * 42585 that I think is a little too high to run in the time limit.&lt;br /&gt;Second : you can map all the strings in the dictionary to The explained number, and then easily when you read the input number search that whether you have that number in your map data structure and print all the strings mapped to that number, print "NONE" otherwise.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-6478876872377535751?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/6478876872377535751/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-name-that-number.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6478876872377535751'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6478876872377535751'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-name-that-number.html' title='USACO - Name That Number'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-1531864047071482560</id><published>2010-12-05T00:56:00.004+03:30</published><updated>2010-12-06T02:14:48.984+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='USACO'/><title type='text'>USACO - Milking Cows</title><content type='html'>&lt;center&gt; &lt;b&gt;Milking Cows&lt;/b&gt;&lt;br /&gt;&lt;/center&gt;  Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100.  The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).  &lt;br /&gt;Your job is to write a program that will examine a list of beginning and ending times for N (1 &amp;lt;=  N &amp;lt;= 5000) farmers milking N cows and compute (in seconds): &lt;br /&gt;&lt;ul&gt;&lt;li&gt; The longest time interval at least one cow was milked.  &lt;/li&gt;&lt;li&gt; The longest time interval (after milking starts) during which no cows were being milked. &lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;PROGRAM NAME: milk2&lt;/h3&gt;&lt;h3&gt;INPUT FORMAT&lt;/h3&gt;&lt;table border="1"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;td&gt;Line 1: &lt;/td&gt; &lt;td&gt;The single integer  &lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;Lines 2..N+1: &lt;/td&gt; &lt;td&gt;Two non-negative integers less than 1000000, the starting and ending time in seconds after 0500&lt;/td&gt;  &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h3&gt;SAMPLE INPUT (file milk2.in) &lt;/h3&gt;&lt;pre&gt;3&lt;br /&gt;300 1000&lt;br /&gt;700 1200&lt;br /&gt;1500 2100&lt;br /&gt;&lt;/pre&gt;&lt;h3&gt;OUTPUT FORMAT&lt;/h3&gt;A single line with two integers that represent the longest continuous time of milking and the longest idle time.  &lt;br /&gt;&lt;h3&gt;SAMPLE OUTPUT (file milk2.out)&lt;/h3&gt;&lt;pre&gt;900 300&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre&gt;&lt;/pre&gt;&lt;pre style="font-family: inherit;"&gt;Sorting - Ad hoc - iteration with high precision&lt;/pre&gt;&lt;pre style="font-family: inherit;"&gt;&lt;/pre&gt;First sort the elements by the start time of milking then iterate and keep track of maxEnd and minStart till now, if the current times have intersection with the minStart and maxEnd, then update your maxEnd time, else change the minStart and maxEnd to the current times (start[i], end[i]), this is for the longest time that at list one cow is being milked.&lt;br /&gt;For the longest time that no cow is being milked, easily do the same, if the current start[cur] &amp;gt; maxEnd, you have a gap here, so keep track of start[cur]-maxEnd and update minStart and maxEnd.&lt;br /&gt;At the end choose the maximum between your values.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-1531864047071482560?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/1531864047071482560/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-milking-cows.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1531864047071482560'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1531864047071482560'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/usaco-milking-cows.html' title='USACO - Milking Cows'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-7243470579180857277</id><published>2010-12-05T00:49:00.001+03:30</published><updated>2010-12-06T02:14:42.671+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TJU'/><title type='text'>TJU - 1415 - Ferry Loading II</title><content type='html'>&lt;a href="http://acm.tju.edu.cn/toj/showp.php?pid=1415" target="_blank"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;greedy - ad hoc - simulation&lt;br /&gt;&lt;br /&gt;you have to check whether you can load each time n cars or not, if not the first load must be m%n and the rest loads n cars.&lt;br /&gt;then you have to iterate to see when a load comes back are there n other cars ready or not and then decide what to do next.&lt;br /&gt;if they are ready ferry loads the cars and leaves soon, otherwise ferry  has to wait for the n-th car to come and this time is calculated.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-7243470579180857277?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/7243470579180857277/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/tju-1415-ferry-loading-ii.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7243470579180857277'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7243470579180857277'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/tju-1415-ferry-loading-ii.html' title='TJU - 1415 - Ferry Loading II'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-6109041502932578997</id><published>2010-12-02T04:01:00.002+03:30</published><updated>2010-12-06T02:14:37.561+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TJU'/><title type='text'>TJU - 1477 - binary numbers</title><content type='html'>&lt;a href="http://acm.tju.edu.cn/toj/showp1477.html"&gt;problem statement&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;bits - binary presentation of N&lt;br /&gt;&lt;br /&gt;Easily in this problem you have to find out the locations of 1's in the binary presentation of a number N.&lt;br /&gt;I iterate from i=0 to 2^i &amp;lt;= 1000000 and each time check whether ( N &amp;amp; 2^i != 0 ) or not and print i if result is true.&lt;br /&gt;1 &amp;lt;&amp;lt; i&lt;br /&gt;00000001&lt;br /&gt;00000010&lt;br /&gt;00000100&lt;br /&gt;00001000&lt;br /&gt;00010000&lt;br /&gt;00100000&lt;br /&gt;01000000&lt;br /&gt;10000000&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-6109041502932578997?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/6109041502932578997/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/12/tju-1477-binary-numbers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6109041502932578997'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6109041502932578997'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/12/tju-1477-binary-numbers.html' title='TJU - 1477 - binary numbers'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-2563193112304297697</id><published>2010-10-18T01:45:00.001+03:30</published><updated>2010-12-06T02:11:10.661+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>11000 - Bee - UVA</title><content type='html'>Fibonacci numbers&lt;br /&gt;&lt;br /&gt;If you write the sequence resulted from the description, you'll see that you have to find sum of the first n Fibonacci numbers + sum of the first (n-1) Fibonacci numbers.&lt;br /&gt;Sum Fib( i ) = Fib(n+2)-1&lt;br /&gt;----- i = 0...n&lt;br /&gt;&lt;br /&gt;Use long long int and you just need to calculate the first 46 sentences of the Fibonacci numbers.&lt;br /&gt;For more information about Fibonacci numbers read this :&lt;br /&gt;http://en.wikipedia.org/wiki/Fibonacci_number&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-2563193112304297697?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/2563193112304297697/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/10/11000-bee-uva.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2563193112304297697'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2563193112304297697'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/10/11000-bee-uva.html' title='11000 - Bee - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-4574019734536030455</id><published>2010-10-17T11:47:00.001+03:30</published><updated>2010-12-06T02:11:16.078+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>11239 - Open Source - UVA</title><content type='html'>Data Structures - STL map or set - sorting&lt;br /&gt;&lt;br /&gt;As shown in the problem description, you have to count the number of students who have signed up for each project. There are some important hints in this problem that you have to concern :&lt;br /&gt;If someone has signed up for more than one project do not count him for any of them.&lt;br /&gt;If someone has written his name for a project more than one time, It's OK, just count him once in that project.&lt;br /&gt;&lt;br /&gt;Data structure I use in this problem :&lt;br /&gt;&lt;string,&gt;map (string, set(string) ) === to save for each project which students want to attend&lt;br /&gt;&lt;string,&gt;map (string, string) === to save this student is singed for which project previously&lt;br /&gt;set (string) ===&lt;string&gt; to save which student has signed up and can not attend any other project, if a student want to sign up for more than one project I find the previous project he/she has signed up for and delete him/her from that list&lt;br /&gt;vector (int, string) === to produce output and sort it by number of students attending each project and then alphabetically by the name of the project&lt;/string&gt;&lt;/string,&gt;&lt;/string,&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-4574019734536030455?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/4574019734536030455/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/10/11239-open-source-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/4574019734536030455'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/4574019734536030455'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/10/11239-open-source-uva.html' title='11239 - Open Source - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-6055076698287659875</id><published>2010-10-17T10:04:00.001+03:30</published><updated>2010-12-06T02:11:21.029+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>10226 - Hardwood Species - UVA</title><content type='html'>Data Structures - STL map or set&lt;br /&gt;&lt;br /&gt;In this problem you will read some tree names from input and must count the number of trees and at the end print the percentage of trees.&lt;br /&gt;What can we do? We read tree names one by one, when reading a tree name, you have to increment the number of that tree counted till now, so you have to find the index of that tree to increment it. If you use linear search to find the index you will get a TLE (Time Limit Exceeded), so you have to use a binary search. using set or map data structures that have a binary search with them is appropriate. MAPs and SETs are always sorted.&lt;br /&gt;I use a map data structure from string to int and each time I read a tree name I increment the counter of that tree.&lt;br /&gt;&lt;br /&gt;map &lt;string,&gt; mp;&lt;br /&gt;while( getline( cin, tree ), tree != "" )&lt;br /&gt;{&lt;br /&gt;mp[tree] ++;&lt;br /&gt;total ++;&lt;br /&gt;}&lt;br /&gt;and at the end I print (each counter/total).&lt;/string,&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-6055076698287659875?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/6055076698287659875/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/10/10226-hardwood-species-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6055076698287659875'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6055076698287659875'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/10/10226-hardwood-species-uva.html' title='10226 - Hardwood Species - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-9177835251784039632</id><published>2010-10-17T09:14:00.001+03:30</published><updated>2010-12-06T02:11:26.197+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>10928 - My Dear Neighbours - UVA</title><content type='html'>graph representation or Ad Hoc&lt;br /&gt;&lt;br /&gt;In this problem you have to count the OUT-DEGREE of each node and find the minimum OUT-DEGREE and print nodes with the minimum OUT-DEGREE sequentially.&lt;br /&gt;What you have to concern is type of input. I get input using stringstream and getline and count the neighbors of each node.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-9177835251784039632?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/9177835251784039632/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/10/10928-my-dear-neighbours-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/9177835251784039632'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/9177835251784039632'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/10/10928-my-dear-neighbours-uva.html' title='10928 - My Dear Neighbours - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-1525930701434534350</id><published>2010-10-17T09:03:00.001+03:30</published><updated>2010-12-06T02:11:32.320+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>599 - The Forrest for the Trees - UVA</title><content type='html'>graph search algorithm - DFS&lt;br /&gt;&lt;br /&gt;In this problem you have to find the number of components in a graph and you have to distinguish which components are isolated vertices. Easily run DFS for each unvisited node and count the number of components, set a counter before running DFS to find the number of vertices in a component, increment it at the beginnig and if this number was 1, it is an isolated vertex.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-1525930701434534350?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/1525930701434534350/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/10/599-forrest-for-trees-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1525930701434534350'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1525930701434534350'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/10/599-forrest-for-trees-uva.html' title='599 - The Forrest for the Trees - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-7597385791816987095</id><published>2010-10-17T07:56:00.002+03:30</published><updated>2010-12-06T02:11:38.658+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>924 - Spreading The News - UVA</title><content type='html'>graph search algorithm - BFS&lt;br /&gt;&lt;br /&gt;In this problem you have to run BFS from a source, and find out that in the BFS-tree in which level you have the most nodes and in which level the most nodes come to BFS-tree.&lt;br /&gt;I run BFS from the given source and find out the depth of each node from the source and then calculate which depth has the most nodes and what the level is.&lt;br /&gt;if d[i] is the depth for node i from source then&lt;br /&gt;for(int i = 0; i &amp;lt; n; i ++)&lt;br /&gt;output[ d[i] ] ++;&lt;br /&gt;find max from output and print the index of maximum as depth and output[index] as size of the maximum.&lt;br /&gt;problem sample test case :&lt;br /&gt;&lt;a href="http://www.4shared.com/photo/va73oUQD/924_spreading_the_news.html"&gt;http://www.4shared.com/photo/va73oUQD/924_spreading_the_news.html&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-7597385791816987095?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/7597385791816987095/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/10/924-spreading-news-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7597385791816987095'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7597385791816987095'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/10/924-spreading-news-uva.html' title='924 - Spreading The News - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-7761648889884842941</id><published>2010-10-17T07:48:00.001+03:30</published><updated>2010-12-06T02:11:44.602+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>439 - Knight Moves - UVA</title><content type='html'>graph search algorithm - BFS&lt;br /&gt;&lt;br /&gt;In this problem you are going to find out the shortest path between two cells of a chess board for a knight. I don't know how, but you have to prove that in a 8x8 chess board if you place a knight in a cell then it can reach any cell you want. So the shortest path problem works here.&lt;br /&gt;The neighbors of a cell are these cells, if the cell is (x, y):&lt;br /&gt;(x+1, y+2)&lt;br /&gt;(x+1, y-2)&lt;br /&gt;(x-1, y+2)&lt;br /&gt;(x-1, y-2)&lt;br /&gt;(x+2, y+1)&lt;br /&gt;(x+2, y-1)&lt;br /&gt;(x-2, y+1)&lt;br /&gt;(x-2, y-1)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-7761648889884842941?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/7761648889884842941/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/10/439-knight-moves-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7761648889884842941'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7761648889884842941'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/10/439-knight-moves-uva.html' title='439 - Knight Moves - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-6024900052886138665</id><published>2010-10-10T17:42:00.001+03:30</published><updated>2010-12-06T02:11:49.930+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>10610 - Gopher and Hawks - UVA</title><content type='html'>graph search algorithm - BFS&lt;br /&gt;&lt;br /&gt;You are given some points in a grid and have to calculate whether you can reach a  end point from a start point or not. When you leave a point to reach another point you can just run for m*60 seconds, if you run more you will disappear ;-). So "distance between pi and pk / V &amp;lt; m*60" must satisfy to be sure that you can go from pi to pk, in this way I create adjacency matrix and then easily run a BFS on the graph to find the shortest path from the start point to the end point.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-6024900052886138665?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/6024900052886138665/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/10/10610-gopher-and-hawks-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6024900052886138665'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6024900052886138665'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/10/10610-gopher-and-hawks-uva.html' title='10610 - Gopher and Hawks - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-1206204611018950914</id><published>2010-10-10T16:48:00.001+03:30</published><updated>2010-12-06T02:11:58.133+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>10926 - How Many Dependencies? - UVA</title><content type='html'>graph search algorithm - DFS&lt;br /&gt;&lt;br /&gt;In this problem you have to find that if you start DFS from a node how many nodes are under this node in the DFS-tree and choose the maximum value of these for output.&lt;br /&gt;Cause the input size is small and you are facing a DAG, don't think of complex algorithms to solve this problem, easily run DFS for each node and count how many nodes are invoked from this DFS and don't worry about cycles(there are no cycles in this problem). O(n^3)&lt;br /&gt;data structures:&lt;br /&gt;mat[101][101] = adjacency matrix&lt;br /&gt;mark[101] = to mark which nodes are visited&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-1206204611018950914?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/1206204611018950914/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/10/10926-how-many-dependencies-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1206204611018950914'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1206204611018950914'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/10/10926-how-many-dependencies-uva.html' title='10926 - How Many Dependencies? - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-951694081109975941</id><published>2010-10-10T16:10:00.001+03:30</published><updated>2010-12-06T02:12:03.989+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>928 - Eternal Truths - UVA</title><content type='html'>graph search algorithm - BFS&lt;br /&gt;&lt;br /&gt;According to the problem description, you have to find the shortest path between two points in a grid, 'S' &amp;amp; 'E'.&lt;br /&gt;data structures :&lt;br /&gt;triple = a structure with 3 elements( x, y, length ), 'length' is the length of the last jump that has entered this cell&lt;br /&gt;grid[301][301] = an array of strings with size of more than 300 for input grid&lt;br /&gt;mark[301][301][4] = a 3d array to mark and save the distance from the start cell in a jump with size of 1, 2 or 3&lt;br /&gt;After I get input, I find the position of the start and end point, then set all the members of mark to -1, and then create a queue of 'triple', push start to queue and set it's length to 1, because the next jump I can do from this point is a jump with length 1.&lt;br /&gt;Then in a while loop I retrieve the last triple that I have pushed to queue and continue jumping from this point to the neighbor points, before I push the new triple into the queue, I set the length of it to "1 + the current triple's length" to save that how I can continue jumping from this point.&lt;br /&gt;What you have to be careful of is that you can't jump through obstacles, for example assume that you can make jumps with size of 3 at this level.     S.#E     but you can't reach from 'S' to 'E', so check whether there are any obstacles in your jump or not.&lt;br /&gt;At the end check mark[end.first][end.second][1,2,3], any of them that is not -1 and is the smallest one is the answer otherwise the answer is 'NO'.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-951694081109975941?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/951694081109975941/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2010/10/928-eternal-truths-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/951694081109975941'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/951694081109975941'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2010/10/928-eternal-truths-uva.html' title='928 - Eternal Truths - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-4603276950047919880</id><published>2009-12-25T12:59:00.002+03:30</published><updated>2010-12-06T02:26:10.182+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>11388 - GCD LCM - UVA</title><content type='html'>Mathematics&lt;br /&gt;&lt;br /&gt;As you can see in the problem description, you have to find numbers a &amp;amp; b such that (a, b) = G and [a, b] = L and also it has mentioned that if there is more than one pair   satisfying the condition, output the pair for which number a is minimized. It's a very easy problem, you have to figure out whether the remainder of L / G is equal to zero or not, if so, your answer is G &amp;amp; L otherwise you have to output -1. Note that in the worst case (a, b) = a and these inequalities always satisfy.&lt;br /&gt;(a, b) ---&amp;gt;&amp;gt;&amp;gt; GCD of a &amp;amp; b&lt;br /&gt;[a, b] ---&amp;gt;&amp;gt;&amp;gt; LCM of a &amp;amp; b&lt;br /&gt;(a, b) &amp;lt;= a (a, b) &amp;lt;= b  [a, b] &amp;gt;= a&lt;br /&gt;[a, b] &amp;gt;= b&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-4603276950047919880?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/4603276950047919880/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/12/11388-gcd-lcm-uva.html#comment-form' title='38 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/4603276950047919880'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/4603276950047919880'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/12/11388-gcd-lcm-uva.html' title='11388 - GCD LCM - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>38</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-2986106150669711416</id><published>2009-09-06T16:43:00.001+04:30</published><updated>2010-12-06T02:03:54.998+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>UserName - SRM 203 Div 2 - TopCoder</title><content type='html'>string&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Given a list of existing usernames, we are to determine whether a newly requested username already figures among them. If so, we must append to it the lowest possible number to make a new username.&lt;/div&gt;&lt;div&gt;I first checked whether the requested username exists among them, if not I return it, otherwise I iterate starting at 1 and at each step I convert the number to string and add it to the requested username, then check if it doesn't exist I return it.&lt;/div&gt;&lt;div&gt;In this approach I have to added two functions, 1 - search the vector for a string 2 - convert integer to string.&lt;/div&gt;&lt;div&gt;But another solutions exists, at first I implement a set of string and insert all the vector elements to it, I have a char candidate[1024] too.&lt;/div&gt;&lt;div&gt;I use sprintf( candidate, "%s%d", userName.c_str(), number ), then I don't have to implement two extra functions.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-2986106150669711416?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/2986106150669711416/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/username-srm-203-div-2-topcoder.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2986106150669711416'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2986106150669711416'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/username-srm-203-div-2-topcoder.html' title='UserName - SRM 203 Div 2 - TopCoder'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-1358599576856931906</id><published>2009-09-06T15:38:00.001+04:30</published><updated>2010-12-06T02:04:03.476+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>423 - MPI Maelstrom - UVA</title><content type='html'>graph algorithms - single source shortest path Dijkstra&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In this problem you face a communication system, that in this system messages are sent from station 1 to all other n-1 stations, there are costs to send messages from station 'a' to station 'b' if it's possible to send directly otherwise you have to find a route from station 1 to that station.&lt;/div&gt;&lt;div&gt;Since there are communication costs, sometimes it's better to send a message from station 1 to station 'b' not directly to minimize the cost to send message from station 1 to station 'b'.&lt;/div&gt;&lt;div&gt;In this manner each station will have a minimum time required to receive the message from station 1. This problem wants the maximum of these minimum times.&lt;/div&gt;&lt;div&gt;I use dijkstra to solve this problem. Let me explain what I do in my solution.&lt;/div&gt;&lt;div&gt;I have an adjacency matrix[101][101] and an array MIN[101] and a boolean array mark[101]&lt;/div&gt;&lt;div&gt;I construct adjacency matrix from input.&lt;/div&gt;&lt;div&gt;I initialize MIN[i] = infinity for i = 1 to n and MIN[1] = 0&lt;/div&gt;&lt;div&gt;and initialize mark[i] = false for i = 1 to n&lt;/div&gt;&lt;div&gt;I am going to construct a shortest path tree starting at node 1 using dijkstra. I have a node "current". Initially I set it to 1.&lt;/div&gt;&lt;div&gt;Then I check the adjacency matrix for node current and set&amp;nbsp;&lt;/div&gt;&lt;div&gt;MIN[ neighbor[cur] ] = minimum( MIN[neighbor[cur]], MIN[cur] + adjMAT[cur][i]&amp;nbsp;)&lt;/div&gt;&lt;div&gt;after checking all of the current neighbors I set mark[cur] = true and find a new current. how?&lt;/div&gt;&lt;div&gt;I check all of the nodes and from them that haven't been marked yet and has the minimum distance to node start I pick my next "current" node.&lt;/div&gt;&lt;div&gt;Since the graph is connected and has just one component I iterate through MIN[i] and pick the maximum.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-1358599576856931906?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/1358599576856931906/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/423-mpi-maelstrom-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1358599576856931906'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1358599576856931906'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/423-mpi-maelstrom-uva.html' title='423 - MPI Maelstrom - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-1990054851885171849</id><published>2009-09-06T14:11:00.001+04:30</published><updated>2010-12-06T02:04:34.870+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>10301 - Rings and Glue - UVA</title><content type='html'>graph search algorithm, Depth First Search ( DFS ) or BFS &amp;nbsp;+ &amp;nbsp;GEOMETRY&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I first construct adjacency matrix then use DFS to split each component and find the maximum.&lt;/div&gt;&lt;div&gt;d = distance between two centers of the rings&lt;/div&gt;&lt;div&gt;rPlusR = radius of ring 'a' + radius of ring 'b'&lt;/div&gt;&lt;div&gt;if( d &amp;lt;&amp;gt; max(R[a], R[b])&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;then adjMat[a][b] = adjMat[b][a] = true&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;After constructing the adjacency matrix I easily run DFS on the constructed graph, each time DFS is invoked we have a new component so I count the vertices that are being marked during this DFS call and compare to the maximum.&lt;/div&gt;&lt;div&gt;for(int i = 0; i &amp;lt;&amp;gt;&lt;br /&gt;&lt;div&gt;if( mark[i] == false )&lt;/div&gt;&lt;div&gt;{&lt;/div&gt;&lt;div&gt;vertices = 0;&lt;/div&gt;&lt;div&gt;DFS( i );&lt;/div&gt;&lt;div&gt;if( vertices &amp;lt;&amp;gt;&lt;br /&gt;&lt;div&gt;mx = vertices;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I made a mistake in the output.&lt;/div&gt;&lt;div&gt;Note that if maximum is equal to 1 &amp;nbsp;you don't have to print 's' at the end of output.&lt;/div&gt;&lt;div&gt;( thanks Pouria for his hint )&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-1990054851885171849?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/1990054851885171849/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/10301-rings-and-glue-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1990054851885171849'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1990054851885171849'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/10301-rings-and-glue-uva.html' title='10301 - Rings and Glue - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-6923113779104469947</id><published>2009-09-06T00:00:00.001+04:30</published><updated>2010-12-06T02:04:56.021+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>LetterStrings - SRM 202 DIV 2 - TopCoder</title><content type='html'>Simulation - string&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Just iterate through all the strings in 's' and count the letters.&lt;/div&gt;&lt;div&gt;I used function isalpha( s[i][j] ). Since there are just letters and dashes in the input strings you can use this condition too if( s[i][j] != '-' ) sum ++;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-6923113779104469947?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/6923113779104469947/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/letterstrings-srm-202-div-2-topcoder.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6923113779104469947'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6923113779104469947'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/letterstrings-srm-202-div-2-topcoder.html' title='LetterStrings - SRM 202 DIV 2 - TopCoder'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-4541542122976400918</id><published>2009-09-05T22:57:00.001+04:30</published><updated>2010-12-06T02:05:04.367+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>ChessMetric - 2003 TCCC Round 4 - TopCoder</title><content type='html'>Dynamic Programming&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I solved it in O( size*size*numMoves ). I implement an array DP[101][101][51] and what's the state of this DP problem? DP[i][j][m] means number of ways you can reach cell[i][j] in 'm' moves.&lt;/div&gt;&lt;div&gt;Each cell is reachable ( at most ) from 16 other cells, that means if you want to come to cell[i][j] you can reach it from other 16 cells and you are going to add the number of ways you can reach each of them and the result is number of ways you can reach cell[i][j].&lt;/div&gt;&lt;div&gt;At first I set DP[start[0]][start[1]][0] = 1, &amp;nbsp; &amp;nbsp; that means I can reach cell[start[0]][start[1]] in zero moves in one way. So from m=0 to numMoves if DP[i][j][m] != 0 that means I can reach it from start in m moves, I choose it and update it's neighbors DP[a][b][m+1] += DP[i][j][m]&lt;/div&gt;&lt;div&gt;That means I can go to cell[a][b] from cell[i][j] plus one move m ---&amp;gt;&amp;gt;&amp;gt; m+1&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;DP[i][j][m] = DP[i-1][j-1][m-1] + DP[i-1][j][m-1] + ..... + DP[i+2][j+1][m-1]&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-4541542122976400918?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/4541542122976400918/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/chessmetric-2003-tccc-round-4-topcoder.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/4541542122976400918'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/4541542122976400918'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/chessmetric-2003-tccc-round-4-topcoder.html' title='ChessMetric - 2003 TCCC Round 4 - TopCoder'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-7442819817568961371</id><published>2009-09-05T19:33:00.001+04:30</published><updated>2010-12-06T02:05:13.633+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>Multiples - SRM 201 DIV 2 - TopCoder</title><content type='html'>&lt;span class="Apple-style-span" style="font-size: medium;"&gt;Ad Hoc - math&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;The simplest solution, which works because the constraints are so low, is best here: loop from min to max and check each number to see if it is devisable by factor.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;If you want to get more efficient, you can increment min until you hit a number divisible by factor, decrement max until you hit a number divisible by factor, then subtract min from max, divide factor and add 1. that's it.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-7442819817568961371?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/7442819817568961371/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/multiples-srm-201-div-2-topcoder.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7442819817568961371'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7442819817568961371'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/multiples-srm-201-div-2-topcoder.html' title='Multiples - SRM 201 DIV 2 - TopCoder'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-7773795437535404125</id><published>2009-09-05T18:54:00.001+04:30</published><updated>2010-12-06T02:06:03.430+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>AvoidRoads - 2003 TCO Semifinals 4 - TopCoder</title><content type='html'>Dynamic Programming&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The problem wants to know in how many ways one can reach destination ( cell[width][height] )&lt;/div&gt;&lt;div&gt;If there were no obstacles, then it was a discrete mathematics problem, but now that we have obstacles in the path we have to calculate it in a dynamic programming approach.&lt;/div&gt;&lt;div&gt;What's the 'state' in this problem? ( 'state' &lt;span class="Apple-style-span" style="color: #333333; font-family: Arial; font-size: 12px; line-height: 16px;"&gt;a way to describe a situation, a sub-solution for the problem&amp;nbsp;&lt;span class="Apple-style-span" style="color: black; font-family: Georgia; font-size: 16px; line-height: normal;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;dp[i][j] = ( i &amp;gt; 0 ? dp[i-1][j] : 0, j &amp;gt; 0 ? dp[i][j-1] : 0 )&lt;/div&gt;&lt;div&gt;This recurrent relation is true if there are no obstacles in the path, but now what? Since we can come from two cells to the current cell, we have to check whether it is possible to come from cell[i-1][j] to cell[i][j] or from cell[i][j-1] to cell[i][j], each of which is not possible we assume it zero, OK but how do we determine whether it is possible or not? I build a set of pair&lt;pair,&gt;&lt;/pair,&gt;&lt;/div&gt;&lt;div&gt;and that tells me that you cannot go from pairONE to pairTWO.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-7773795437535404125?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/7773795437535404125/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/avoidroads-2003-tco-semifinals-4.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7773795437535404125'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7773795437535404125'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/avoidroads-2003-tco-semifinals-4.html' title='AvoidRoads - 2003 TCO Semifinals 4 - TopCoder'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-6826023503981136061</id><published>2009-09-05T16:56:00.001+04:30</published><updated>2010-12-06T02:07:55.527+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>No Order Of Operations - SRM 200 DIV 2 - TopCoder</title><content type='html'>Ad Hoc - String&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Just simulate what problem wants, iterate from left to right and evaluate the expression.&lt;/div&gt;&lt;div&gt;Note that the numbers are just one digit from 0 to 9.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-6826023503981136061?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/6826023503981136061/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/no-order-of-operations-srm-200-div-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6826023503981136061'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6826023503981136061'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/no-order-of-operations-srm-200-div-2.html' title='No Order Of Operations - SRM 200 DIV 2 - TopCoder'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-8059133065841473409</id><published>2009-09-05T16:03:00.001+04:30</published><updated>2010-12-06T02:05:55.214+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>FlowerGarden - 2004 TCCC Round 1 - TopCoder</title><content type='html'>Dynamic Programming&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I first figure out for each flower how many flowers and which flowers have to go first.&lt;/div&gt;&lt;div&gt;It's not difficult to do that. I implemented arr[50][50] if arr[i][j] is true then flower j must go first before flower i. When arr[i][j] is true??? If height[i] &amp;gt; height[j] &amp;amp;&amp;amp; inter( bloom[i], wilt[i], bloom[j], wilt[j] ) then arr[i][j] = true&lt;/div&gt;&lt;div&gt;The function "inter" determines whether the time of bloom and wilt for flower i and j have conflict.&lt;/div&gt;&lt;div&gt;After calculating "arr" I start finding the highest flower that doesn't conflict with others and push it back to the output vector and manipulate "arr" after choosing a flower, I mean if arr[i][chosen] == 1 then arr[i][chosen] = 0 and repeat this procedure n times to choose n flowers.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-8059133065841473409?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/8059133065841473409/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/flowergarden-2004-tccc-round-1-topcoder.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8059133065841473409'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8059133065841473409'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/flowergarden-2004-tccc-round-1-topcoder.html' title='FlowerGarden - 2004 TCCC Round 1 - TopCoder'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-3470370245048685944</id><published>2009-09-05T01:31:00.001+04:30</published><updated>2010-12-06T02:06:34.369+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>Hawaiian - 2004 TCCC Round 4 - TopCoder</title><content type='html'>Ad Hoc + string&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I just implemented what problems described. If you know the function isalpha( ch ),&amp;nbsp;&lt;/div&gt;&lt;div&gt;you know what you have to do, you just need to implement function isHawaiian( ch ) which checks whether a given character is in the Hawaiian alphabet or not. Of course you have to split the words in the input string, you have two ways to do it, do it manually or use stringstream which do it for you.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;stringstream ssin;&lt;/div&gt;&lt;div&gt;ssin.str( input );&lt;/div&gt;&lt;div&gt;while( ssin &amp;gt;&amp;gt; word )&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;check( word ) and push back to the output vector if it's ok&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-3470370245048685944?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/3470370245048685944/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/hawaiian-2004-tccc-round-4-topcoder.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/3470370245048685944'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/3470370245048685944'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/hawaiian-2004-tccc-round-4-topcoder.html' title='Hawaiian - 2004 TCCC Round 4 - TopCoder'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-7851908044328257112</id><published>2009-09-05T01:10:00.001+04:30</published><updated>2010-12-06T02:05:58.242+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>BadNeighbors - 2004 TCCC Round 4 - TopCoder</title><content type='html'>Dynamic Programming&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For simplicity you can calculate two different sets, one with all elements except the first and the other with all elements except the last one, cause the first and the last element can't be in the desired sequence. So now we have a linear sequence which we want to maximize the sum.&lt;/div&gt;&lt;div&gt;Suppose dp[i] is the maximum sum after checking the elements to position i.&lt;/div&gt;&lt;div&gt;dp[i] = max( element[i]+dp[i-2], element[i-1]+dp[i-3] )&lt;/div&gt;&lt;div&gt;What does it mean? The first (element[i]+dp[i-2]) means it's better to include element 'i' in the set and the second means it's better to include element 'i-1' in the set.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-7851908044328257112?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/7851908044328257112/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/badneighbors-2004-tccc-round-4-topcoder.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7851908044328257112'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/7851908044328257112'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/badneighbors-2004-tccc-round-4-topcoder.html' title='BadNeighbors - 2004 TCCC Round 4 - TopCoder'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-3204073944528521307</id><published>2009-09-04T21:25:00.001+04:30</published><updated>2012-01-06T19:16:54.303+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='DP'/><category scheme='http://www.blogger.com/atom/ns#' term='TopCoder'/><title type='text'>ZigZag - 2003 TCCC Semifinals 3 - TopCoder</title><content type='html'>Dynamic Programming - Longest Increasing Subsequence( LIS )&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;It's something like LIS. You have to manipulate the original algorithm to obtain the answer.&lt;/div&gt;&lt;div&gt;I used a two dimensional array ZIGZAG[60][2] such that ZIGZAG[i][0] is the length of the longest ZigZag sequence ending at sequence[i] and ZIGZAG[i][1] is the sign of the last difference ending at sequence[i].&lt;/div&gt;&lt;div&gt;Suppose we have a sequence like this:&lt;/div&gt;&lt;div&gt;1, 8, 7, 6, 9, 10&lt;/div&gt;&lt;div&gt;ZIGZAG[1][0] = 1, &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ZIGZAG[1][1] = 0&lt;/div&gt;&lt;div&gt;ZIGZAG[2][0] = 2, &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;ZIGZAG[2][1] = +1&lt;/div&gt;&lt;div&gt;ZIGZAG[3][0] = 3, &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;ZIGZAG[3][1] = -1&lt;/div&gt;&lt;div&gt;ZIGZAG[4][0] = 3, &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;ZIGZAG[4][1] = -1&lt;/div&gt;&lt;div&gt;ZIGZAG[5][0] = 4, &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;ZIGZAG[5][1] = +1&lt;/div&gt;&lt;div&gt;ZIGZAG[6][0] = 4, &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ZIGZAG[6][1] = +1&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I implemented the solution in O( n^2 ), since I have to check n items and for each item I will check n items to choose the appropriate subsequence.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-3204073944528521307?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/3204073944528521307/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/09/zigzag-2003-tccc-semifinals-3-topcoder.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/3204073944528521307'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/3204073944528521307'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/09/zigzag-2003-tccc-semifinals-3-topcoder.html' title='ZigZag - 2003 TCCC Semifinals 3 - TopCoder'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-601874197511872133</id><published>2009-09-01T05:28:00.002+04:30</published><updated>2010-12-06T02:27:41.409+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>116 - Unidirectional TSP - UVA</title><content type='html'>dynamic programming&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;All you have to do is to find a path that have the minimal weight path.&lt;/div&gt;&lt;div&gt;I first started to construct my table from the first column and in each step I picked the minimum weight till that step that had the minimum row, but that was wrong because if there are several paths, my program might pick the wrong path.&lt;/div&gt;&lt;div&gt;for example ( nimA, one of my friends pointed this test case out )&lt;/div&gt;&lt;div&gt;4 3&lt;/div&gt;&lt;div&gt;2 1 1&lt;/div&gt;&lt;div&gt;2 1 2&lt;/div&gt;&lt;div&gt;1 2 2&lt;/div&gt;&lt;div&gt;1 2 2&lt;/div&gt;&lt;div&gt;my program produced&lt;/div&gt;&lt;div&gt;4 1 1&lt;/div&gt;&lt;div&gt;3&lt;/div&gt;&lt;div&gt;but the correct answer is&lt;/div&gt;&lt;div&gt;3 2 1&lt;/div&gt;&lt;div&gt;3&lt;/div&gt;&lt;div&gt;that is lexicographically smaller.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I first construct the last column of my table&lt;/div&gt;&lt;div&gt;dp[i][n] = a[i][n] for all i&lt;/div&gt;&lt;div&gt;then I want to construct the rest of my table, in each cell [i][j] I can go to next column one row upper, same row, or one row lower, I pick the minimum value of them and when I have several minimum I pick the one with minimum row.&lt;/div&gt;&lt;div&gt;for( int j = n-1; j &amp;gt;= 1; j --)&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;for(int i = 1; i &amp;lt;= m; i ++) &lt;/div&gt;&lt;br /&gt;&lt;div&gt;in these two loops I fill my table and finally I find my minimum in the first column and print the path.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-601874197511872133?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/601874197511872133/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/116-unidirectional-tsp-uva.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/601874197511872133'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/601874197511872133'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/116-unidirectional-tsp-uva.html' title='116 - Unidirectional TSP - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-3238748567508047949</id><published>2009-08-31T18:44:00.002+04:30</published><updated>2010-12-06T01:59:42.930+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>136 - Ugly Numbers - UVA</title><content type='html'>Ad Hoc OR Dynamic Programming&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;You can pre-calculate calculate the answer by checking all the numbers till you find the 1500'th ugly number by checking each number prim factors to see whether they are just 2, 3 and 5.&lt;/div&gt;&lt;div&gt;OR&lt;/div&gt;&lt;div&gt;You can build a list of ugly numbers in a bottom-up dynamic programming approach. How?&lt;/div&gt;&lt;div&gt;I use a set and a vector of integers for simplicity. Both the set and the vector have the same numbers, I can use just a set too without no vectors.&lt;/div&gt;&lt;div&gt;I must build the list of ugly numbers, since my first ugly number in the list is 1, so the next one must be retrieved by multiplying 2, 3 and 5 by 1 ( the first number in the list ). I pick 2*1 ( the minimum ), then 2 must be multiplied by the next number in the list( 2 ). At next step I choose 3 out of ( 2*2, 3*1, 5*1 ), and 3 must be multiplied by 2( the next number in the list for factor 3 ), then I pick 4 out of ( 2*2, 3*2, 5*1 ) and so on.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;2*1 &amp;nbsp; &amp;nbsp; 3*1 &amp;nbsp; &amp;nbsp; 5*1&lt;/div&gt;&lt;div&gt;2*2 &amp;nbsp; &amp;nbsp;3*2 &amp;nbsp; &amp;nbsp;5*2&lt;/div&gt;&lt;div&gt;2*3 &amp;nbsp; &amp;nbsp;3*3 &amp;nbsp; &amp;nbsp;5*3&lt;/div&gt;&lt;div&gt;2*4 &amp;nbsp; &amp;nbsp;3*4&amp;nbsp;&lt;/div&gt;&lt;div&gt;2*5&lt;/div&gt;&lt;div&gt;2*6&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;My function min3 is invoked in this order:&lt;/div&gt;&lt;div&gt;2*1, &amp;nbsp; 3*1, &amp;nbsp; 5*1&lt;/div&gt;&lt;div&gt;2*2, &amp;nbsp; 3*1, &amp;nbsp; 5*1&lt;/div&gt;&lt;div&gt;2*2, &amp;nbsp; 3*2, &amp;nbsp; 5*1&lt;/div&gt;&lt;div&gt;2*3, &amp;nbsp; 3*2, &amp;nbsp; 5*1&lt;/div&gt;&lt;div&gt;2*3, &amp;nbsp; 3*2, &amp;nbsp; 5*2&lt;/div&gt;&lt;div&gt;2*4, &amp;nbsp; 3*2, &amp;nbsp; 5*2&lt;/div&gt;&lt;div&gt;2*4, &amp;nbsp; 3*3, &amp;nbsp; 5*2&lt;/div&gt;&lt;div&gt;2*5, &amp;nbsp; 3*3, &amp;nbsp; 5*2&lt;/div&gt;&lt;div&gt;2*5, &amp;nbsp; 3*4, &amp;nbsp; 5*2&lt;/div&gt;&lt;div&gt;2*6, &amp;nbsp; 3*4, &amp;nbsp; 5*2&lt;/div&gt;&lt;div&gt;2*6, &amp;nbsp; 3*4, &amp;nbsp; 5*3&lt;/div&gt;&lt;div&gt;2*8, &amp;nbsp; 3*4, &amp;nbsp; 5*3&lt;/div&gt;&lt;div&gt;2*8, &amp;nbsp; 3*5, &amp;nbsp; 5*3&lt;/div&gt;&lt;div&gt;........&lt;/div&gt;&lt;div&gt;........&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-3238748567508047949?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/3238748567508047949/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/136-ugly-numbers-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/3238748567508047949'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/3238748567508047949'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/136-ugly-numbers-uva.html' title='136 - Ugly Numbers - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-4136248875664031809</id><published>2009-08-30T13:58:00.001+04:30</published><updated>2010-12-06T01:59:49.366+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>497 - Strategic Defense Initiative - UVA</title><content type='html'>dynamic programming - Longest Increasing Subsequence ( LIS ) - O( n^2 )...O( nLOG(n) )&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This problem is a classic dynamic programming problem that if you are familiar with the type of the problem, you will solve it in less than 10 minutes.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I use O( nLOG(n) ) implementation of LIS, you can find an implementation of it at&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Lucida Grande'; font-size: 12px; white-space: pre;"&gt;http://www.algorithmist.com/index.php/Longest_Increasing_Subsequence&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-4136248875664031809?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/4136248875664031809/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/497-strategic-defense-initiative-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/4136248875664031809'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/4136248875664031809'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/497-strategic-defense-initiative-uva.html' title='497 - Strategic Defense Initiative - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-2987993024045531057</id><published>2009-08-30T01:30:00.001+04:30</published><updated>2010-12-06T01:59:58.530+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>762 - We Ship Cheap - UVA</title><content type='html'>graph search algorithm - single source shortest path - BFS&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The problem takes as input the description of a city (graph) and &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;prints &lt;/span&gt;the shortest path from source city to destination city.&lt;/div&gt;&lt;div&gt;The challenging part of this problem is that how you want to represent the graph?&lt;/div&gt;&lt;div&gt;Since there are just 26*26 distinct city names you can use adjacency matrix too, but I use adjacency list by using a map of "string" to "vector&lt;string&gt;". I also use a map of "string" to "string" to save the precedence (father) node of another node, so I can print the path easily, and I use a set of "string" to mark nodes that I have visited recently.&lt;/string&gt;&lt;/div&gt;&lt;div&gt;And the rest and main part of the problem is to find the shortest path from source city to destination city that I do it using a queue of "string".&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-2987993024045531057?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/2987993024045531057/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/762-we-ship-cheap-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2987993024045531057'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2987993024045531057'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/762-we-ship-cheap-uva.html' title='762 - We Ship Cheap - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-2709861916460176715</id><published>2009-08-28T19:24:00.001+04:30</published><updated>2010-12-06T01:58:24.086+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>10102 - The path in the colored field - UVA</title><content type='html'>graph search algorithm - BFS   OR   Ad Hoc&lt;br /&gt;&lt;br /&gt;At first I was confused by the problem description, and I thought that the sample output doesn't match the sample input. The problem wants to know that if you are standing in a cell occupied with digit '1', at most how many steps does it take to get to a cell (just one) occupied with digit 3.&lt;br /&gt;So first I find the minimum distance from each '1' to each '3' and take the minimum of these values, then I just iterate through the minimum values I took for each '1' and print the maximum one.&lt;br /&gt;To find the minimum distance from each '1' to each '3' you can run a BFS from that '1' and then pick the minimum of the distances from the '1', but I don't suggest to do that, because in grids BFS is used when you have some special adjacency for cells or you have some obstacles in the path between two cells, but here you can easily calculate the minimum distance between two cells.&lt;br /&gt;the Distance from cell A to cell B = abs( A.x-B.x ) + abs( A.y-B.y )&lt;br /&gt;This is true if you are allowed to move one cell up, down, left or right, but not diagonally.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-2709861916460176715?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/2709861916460176715/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/10102-path-in-colored-field-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2709861916460176715'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/2709861916460176715'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/10102-path-in-colored-field-uva.html' title='10102 - The path in the colored field - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-538643508024236466</id><published>2009-08-27T03:49:00.001+04:30</published><updated>2010-12-06T01:58:32.872+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>11448 - Who said crisis? - UVA</title><content type='html'>big integer - subtraction&lt;br /&gt;&lt;br /&gt;The problem wants you just to subtract two positive integers that can be up to 10000 ( I think ) digits.&lt;br /&gt;If you have a big integer library, that's a simple work, otherwise you have to implement subtraction yourself, as I did.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-538643508024236466?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/538643508024236466/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/11448-who-said-crisis-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/538643508024236466'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/538643508024236466'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/11448-who-said-crisis-uva.html' title='11448 - Who said crisis? - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-3930987838228956957</id><published>2009-08-27T02:22:00.001+04:30</published><updated>2010-12-06T01:58:40.664+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>11474 - Dying Tree - UVA</title><content type='html'>&lt;i&gt;&lt;span style="font-style: italic;"&gt;graph search algorithm - DFS( Depth-First Search )&lt;br /&gt;&lt;br /&gt;The problem wants to know whether there is a tree to call a doctor to save the sick tree or not?&lt;br /&gt;This tree can also be the sick tree.&lt;br /&gt;&lt;/span&gt;I first construct an adjacency matrix from the trees,( excluding doctors ), HOW? Two trees can talk to each other if the minimum distance between them is less than or equal to k. Then I run DFS from node 1, in the DFS function I check whether there is a doctor to whom the current tree in the DFS function can communicate? HOW? An ordinary tree can communicate to a doctor if the minimum distance between them is less than or equal to d. I have a global boolean variable for checking the answer. If in DFS function I find a connection between a tree and a doctor I set it to true and return, otherwise I continue searching from the adjacent trees to the current tree( adjacency matrix ).&lt;br /&gt;&lt;/i&gt;&lt;i&gt;&lt;/i&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-3930987838228956957?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/3930987838228956957/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/11474-dying-tree-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/3930987838228956957'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/3930987838228956957'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/11474-dying-tree-uva.html' title='11474 - Dying Tree - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-1892092393481069251</id><published>2009-08-26T13:02:00.001+04:30</published><updated>2010-12-06T01:58:47.214+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>10793 - The Orc Attack - UVA</title><content type='html'>graph algorithms - all pairs shortest paths - floyd warshall&lt;br /&gt;&lt;br /&gt;Another shortest path problem. As you can see in the problem description you have to find the minimum distance from locations 1-5 to all other locations other than 1-5. Then you have to pick the candidates, which are equidistant from these 5 locations. That location must be reachable from all  the locations in the graph. If such a location exist, then pick the one which has the minimum farthest distance, that means this location( rally point ) is reachable from all other locations and has a minimum distance to all of them, there is a location that has the maximum minimum ( :-D )  distance from rally point, you have to pick a rally point that this value for that is minimum( distance to the farthest location ).&lt;br /&gt;I simply construct the graph from input, run floyd-warshall on it, and the rest of the problem that has described and I just simulate it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-1892092393481069251?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/1892092393481069251/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/10793-orc-attack-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1892092393481069251'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1892092393481069251'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/10793-orc-attack-uva.html' title='10793 - The Orc Attack - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-8956283791393564821</id><published>2009-08-26T02:14:00.001+04:30</published><updated>2010-12-06T01:58:54.561+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>291 - The House Of Santa Claus - UVA</title><content type='html'>backtracking&lt;br /&gt;&lt;br /&gt;Another backtracking problem. As you can see in the problem description you are not allowed to pass each edge twice. ( Euler tour in lexicographical order )&lt;br /&gt;I make an adjacency matrix, and a boolean matrix for marking the edges I have visited till now.&lt;br /&gt;int mat[6][6];&lt;br /&gt;bool mark[6][6];&lt;br /&gt;&lt;br /&gt;Then I start backtracking from node 1 and each time checking whether to continue from current node or not I check ( mark[cur][i] == false &amp;amp;&amp;amp; mat[cur][i] == 1 ) and then mark this edge to true and keep on backtracking. I have a variable d which denotes the depth of the backtracking whenever it reaches 9 I completely print the tour I have saved.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-8956283791393564821?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/8956283791393564821/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/291-house-of-santa-claus-uva.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8956283791393564821'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8956283791393564821'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/291-house-of-santa-claus-uva.html' title='291 - The House Of Santa Claus - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-537623580722287726</id><published>2009-08-26T01:16:00.001+04:30</published><updated>2010-12-06T01:59:02.619+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>10171 - Meeting Prof. Miguel - UVA</title><content type='html'>graph algorithms - all pairs shortest path - floyd warshall&lt;br /&gt;&lt;br /&gt;This is a double all pairs shortest path problem. As you see in the problem description you are facing two distinct graphs which at most each one has 26 nodes( upper case letters ), one of them is for Manzoor and the other is for Prof. Miguel.&lt;br /&gt;I first construct two graphs and initialize the adjacency matrices to  100000, then read input and finally run floyd warshall one both of them, and find the minimum cost, how? The places which both of them can go, I add the cost for both of them and find the minimum of these.&lt;br /&gt;Don't forget if there are several places with minimum cost, print them in lexicographical order like this.&lt;br /&gt;10 A G N&lt;br /&gt;Be careful about zero costs, the problem has pointed that the costs are non-negative integers, so it can be zero.&lt;br /&gt;My program first got WA because of this test case.&lt;br /&gt;&lt;br /&gt;input :&lt;br /&gt;2&lt;br /&gt;Y U B D 0&lt;br /&gt;M U B D 0&lt;br /&gt;B B&lt;br /&gt;&lt;br /&gt;output :&lt;br /&gt;0 B D&lt;br /&gt;&lt;br /&gt;because first I checked if they are in the same place and separated this test case from the others.&lt;br /&gt;0 B&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-537623580722287726?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/537623580722287726/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/10171-meeting-prof-miguel-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/537623580722287726'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/537623580722287726'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/10171-meeting-prof-miguel-uva.html' title='10171 - Meeting Prof. Miguel - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-8271072817518574594</id><published>2009-08-25T22:47:00.001+04:30</published><updated>2010-12-06T01:59:10.257+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>10637 - Coprimes - UVA</title><content type='html'>backtracking&lt;br /&gt;&lt;br /&gt;As the problem says, you should print  all possible partitions of &lt;b&gt;S&lt;/b&gt; into &lt;b&gt;t&lt;/b&gt; co-prime numbers.&lt;br /&gt;The order doesn't matter, so If you have to choose 1 &amp;amp; 2 &amp;amp; 5, you just need to print 1 2 5 not 5 2 1 or 2 5 1. This fact prunes backtracking and helped me pass time limit.&lt;br /&gt;I implemented a function called solve( start, m, d ). In this function I start checking from start, and each time I find a number that is coprime to the selected items, I put it in the list and keep on backtracking with ( number, m-number, d+1 ), when the function reaches at ( d == numberOfPartitions ) and m is equal to zero( that means there are no values to choose and the partitioning is complete ), I completely print the selected list.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-8271072817518574594?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/8271072817518574594/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/10637-coprimes-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8271072817518574594'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/8271072817518574594'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/10637-coprimes-uva.html' title='10637 - Coprimes - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-1619193635702109644</id><published>2009-08-25T20:17:00.001+04:30</published><updated>2010-12-06T01:59:18.471+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>10422 - Knights in FEN - UVA</title><content type='html'>backtracking&lt;br /&gt;&lt;br /&gt;This problem gives you a 5x5 chess board with 24 knights ( 12 of them are white and the other 12 are black ) and wants you to turn the board to the initial board that is described in the problem with minimum moves.&lt;br /&gt;I think there are better approaches, because in the rank list the first 20 have solved this problem with time 0.000, but if you implement a careful backtracking you will pass time limit.&lt;br /&gt;Since the depth is just 10, backtracking is sufficient.&lt;br /&gt;I just replaced the empty cell with every possible replacement, except the cell that in previous move was empty.&lt;br /&gt;&lt;br /&gt;for(int p = 0; p &amp;lt; 8; p ++)     // 8 directions to move from space {                                            // x &amp;amp; y are the previous cell that was empty i = x+dir[p][0]; j = y+dir[p][1]; if( i &amp;gt;= 0 &amp;amp;&amp;amp; j &amp;gt;= 0 &amp;amp;&amp;amp; i &amp;lt; 5 &amp;amp;&amp;amp; j &amp;lt; 5 &amp;amp;&amp;amp; ( i != a || j != b ) )&lt;br /&gt;{&lt;br /&gt;swap( input[i][j], input[x][y] );&lt;br /&gt;solve( d+1, x, y );&lt;br /&gt;swap( input[i][j], input[x][y] );&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;If the problem is solvable in less than 11 moves, maybe it's possible to solve it in several paths, so each time the puzzle was solved, I saved the length of the shortest path.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-1619193635702109644?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/1619193635702109644/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/10422-knights-in-fen-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1619193635702109644'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1619193635702109644'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/10422-knights-in-fen-uva.html' title='10422 - Knights in FEN - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-254714853323759145</id><published>2009-08-25T02:01:00.001+04:30</published><updated>2010-12-06T01:59:27.403+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>11569 - Lovely Hint - UVA</title><content type='html'>backtracking&lt;br /&gt;&lt;br /&gt;The problem wants you to count all the strings that you can construct under the restriction in the problem and print the length of the longest and the ways you can achieve that length.&lt;br /&gt;&lt;br /&gt;Simply I start backtracking from all the characters and in the function you just backtrack with the characters that can be placed next to the current character under the restriction.&lt;br /&gt;Each time I invoke the function for a character, I mark it, then I just pick it once.&lt;br /&gt;Be careful! Don't invoke the function for 'L' in "HELLO" twice.&lt;br /&gt;&lt;br /&gt;if( line[i] != line[i-1] &amp;amp;&amp;amp; !mark[i] &amp;amp;&amp;amp; 5 * (line[cur]-'A'+1) &amp;lt;= 4 * (line[i]-'A'+1) )&lt;br /&gt;{&lt;br /&gt;mark[i] = true;&lt;br /&gt;solve( i, d+1 );&lt;br /&gt;mark[i] = false;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;I sort the input string, something like this is the main condition in the function.&lt;br /&gt;'i' is the current character I'm checking, 'd' is the length of the string I've made till now.&lt;br /&gt;&lt;br /&gt;length[d ++]&lt;br /&gt;This array tracks how many strings up to 'd' characters I can  construct.&lt;br /&gt;At last I find the maximum length and print length[maximum_length].&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-254714853323759145?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/254714853323759145/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/11569-lovely-hint-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/254714853323759145'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/254714853323759145'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/11569-lovely-hint-uva.html' title='11569 - Lovely Hint - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-1857880299067512115</id><published>2009-08-24T23:51:00.002+04:30</published><updated>2010-12-06T01:59:31.972+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>775 - Hamiltonian Cycle - UVA</title><content type='html'>backtracking - graph representation - finding the hamiltonian path&lt;br /&gt;&lt;br /&gt;The problem gives you the adjacency matrix and wants you to find the hamiltonian path.&lt;br /&gt;First, since the graph is dense the hamiltonian path exists( I think so ), so for all test cases you have an answer to this problem, and since the order is not important your path can start from node 1 and end at node 1.&lt;br /&gt;What you have to do is to construct just a path from node 1 that ends up in a node which is adjacent to node 1, so you can print the path at that time.&lt;br /&gt;I use a 2-dimensional  array for adjacency matrix, a boolean array for marking nodes, because each node can be traversed once except node 1 which is the start and the end of the path, and an array of integers for saving the order of the nodes I'm visiting.&lt;br /&gt;I start backtracking from node 1 and each time in the function I check whether there exist another node which is not visited and continue backtracking with this node.&lt;br /&gt;As backtracking goes on, sometime I find out that I have visited all nodes once and that's the time I print the path and that's it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-1857880299067512115?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/1857880299067512115/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/775-hamiltonian-cycle-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1857880299067512115'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/1857880299067512115'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/775-hamiltonian-cycle-uva.html' title='775 - Hamiltonian Cycle - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5119792247363719173.post-6607935371883939498</id><published>2009-08-24T23:20:00.002+04:30</published><updated>2010-12-06T01:59:35.051+03:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='UVA'/><title type='text'>423 - MPI Maelstram - UVA</title><content type='html'>graph algorithms - all pairs shortest path - floyd warshall O( n^3)&lt;br /&gt;&lt;br /&gt;In a system they want to send a message from a source called station 1 to all other n-1 stations.&lt;br /&gt;What question want is the minimum time to do that, so we have to find the minimum time from station 1 to all other stations and then find the maximum between these minimum times and that's the longest time in which all the other stations have gotten the message.&lt;br /&gt;We don't have to send a message directly because of the expensive costs and so we can use intermediate stations to convey the message from station 1 to station k for example.&lt;br /&gt;'n' is not so large so I used floyd-warshall to do this.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5119792247363719173-6607935371883939498?l=acmph.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://acmph.blogspot.com/feeds/6607935371883939498/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://acmph.blogspot.com/2009/08/423-mpi-maelstram-uva.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6607935371883939498'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5119792247363719173/posts/default/6607935371883939498'/><link rel='alternate' type='text/html' href='http://acmph.blogspot.com/2009/08/423-mpi-maelstram-uva.html' title='423 - MPI Maelstram - UVA'/><author><name>MeHdi KaZemI</name><uri>http://www.blogger.com/profile/04212150469849085941</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
