<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<style type="text/css">
      <!--
        body { font-size: 12pt; font-family: Monospaced }
        pre { font-size: 12pt; font-family: Monospaced }
      -->
    </style>
<style type="text/css">
      <!--
        body { font-size: 12pt; font-family: Monospaced }
        pre { font-size: 12pt; font-family: Monospaced }
      -->
    </style> <style type="text/css">
      <!--
        body { font-size: 12pt; font-family: Monospaced }
        pre { font-size: 12pt; font-family: Monospaced }
      -->
    </style>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>top coder practice room(practice room1-16)(2001 invi-semi)</title>
<style>
<!--
table.MsoTableGrid
	{border:1.0pt solid windowtext;
	text-align:justify;
	text-justify:inter-ideograph;
	font-size:10.0pt;
	font-family:"Times New Roman";
	}
 li.MsoNormal
	{mso-style-parent:"";
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman";
	margin-left:0cm; margin-right:0cm; margin-top:0cm}
-->
</style>
</head>

<body rightmargin="1" bottommargin="1" marginwidth="1" marginheight="1">



<p align="left"><span lang="en-ca"><font size="6" color="#FF0000"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
TopCoder practice room (purely for fun)</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First </span>Edition</font></b></pre>
</div>
<div align="left">
	<pre><span lang="en-ca"><b>These are problems from <a href="http://www.topcoder.com/tc">topCoder practice room</a> and for the &quot;1000&quot; problem sometimes it is not that kind of hard as the problem varies so much depending </b></span></pre>
</div>
<div align="left">
	<pre><span lang="en-ca"><b>on people.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <p>See below.</p>
　</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
	<pre>
　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre>　</pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca"><font size="4" color="#FF0000">1. chooser (problem 250)</font></span></b></pre>
</div>
<div align="left">
  <pre>
</pre>
	<pre>Problem Statement
&nbsp;&nbsp;&nbsp;&nbsp;
A permutation p[0], p[1], ..., p[n-1] is a sequence containing each number from 0 to n-1 exactly once. The result of applying permutation p to an array a of length n is an array b of length n, where b[p[i]] = a[i] (0-based indices). Given an array a, find a permutation which has the effect of sorting the elements of a in non-descending order, i.e., an order where each element is greater than or equal to the previous one. If there are several suitable permutations return the lexicographically smallest one. The permutation p[0], p[1], ..., p[n-1] is considered lexicographically smaller than the permutation q[0], q[1], ..., q[n-1] if there is an index i such that p[i] &lt; q[i] and the equation p[j] = q[j] holds for all j &lt; i.
Definition
&nbsp;&nbsp;&nbsp;&nbsp;
Class:
SortingWithPermutation
Method:
getPermutation
Parameters:
vector &lt;int&gt;
Returns:
vector &lt;int&gt;
Method signature:
vector &lt;int&gt; getPermutation(vector &lt;int&gt; a)
(be sure your method is public)
&nbsp;&nbsp;&nbsp;&nbsp;

Constraints
-
a will contain between 1 and 50 elements, inclusive.
-
Each element of a will be between 1 and 1000, inclusive.
Examples
0)

&nbsp;&nbsp;&nbsp;&nbsp;
{2, 3, 1}
Returns: {1, 2, 0 }
The element that is originally at position 0 goes to position 1. The elements originally at positions 1 and 2 go to positions 2 and 0, respectively.
1)

&nbsp;&nbsp;&nbsp;&nbsp;
{2, 1, 3, 1}
Returns: {2, 0, 3, 1 }
There are two suitable permutations - {2, 0, 3, 1} and {2, 1, 3, 0}. The first one is lexicographically smaller.
2)

&nbsp;&nbsp;&nbsp;&nbsp;
{4, 1, 6, 1, 3, 6, 1, 4}
Returns: {4, 0, 6, 1, 3, 7, 2, 5 }

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.</pre>
	<pre>　</pre>
	<pre>#include &lt;set&gt;
#include &lt;vector&gt;

using namespace std;

struct Node
{
	int value;
	int index;
};

struct SetIntComp
{
	bool operator()(const Node&amp; left, const Node&amp; right) const
	{
		if (left.value != right.value)
		{
			return left.value &lt; right.value;
		}
		else
		{
			return left.index &lt; right.index;
		}
	}
};
typedef set&lt;Node, SetIntComp&gt; MySet;
class SortingWithPermutation
{
public:
	vector&lt;int&gt; getPermutation(vector&lt;int&gt; a)
	{
		MySet m;
		Node node;		
		vector&lt;int&gt; v(a.size());
		int i;
		for ( i = 0; i &lt; a.size(); i ++)
		{
			node.value = a[i];
			node.index = i;
			m.insert(node);
		}
		i = 0;
		for ( MySet::iterator it = m.begin(); it != m.end(); it ++, i ++)
		{			
			v[it-&gt;index] = i;
		}
		return v;
	}
};			
			
		
</pre>
	<pre>/*

<a name="trace32">Problem Statement</a>
You work in a company that produces measuring devices. The software for each device is stored in reprogrammable memory. To program a device, you must connect it to a personal computer and transmit data to the device's reprogrammable memory through a serial interface. Your task is to make this process as efficient as possible.
You are given two vector &lt;int&gt;s offset and size. Each corresponding pair of elements in offset and size describes a piece of data that must be transmitted to the device. The i-th piece of data consists of size[i] consecutive bytes that must be written starting at the address in offset[i]. To successfully program a device, you must write every piece of the given data. Memory addresses that are not referenced in this data are not important - so you can write anything to those addresses, or write nothing at all to them.
Data is transmitted from the computer to the device through packets. Each packet can contain a maximum of maxData bytes of consecutive data that will be written to a specified memory address. Return the minimum possible total number of packets required to transmit all of the given data to the device.
Definition
????
Class:
ProgrammingDevice
Method:
minPackets
Parameters:
vector &lt;int&gt;, vector &lt;int&gt;, int
Returns:
int
Method signature:
int minPackets(vector &lt;int&gt; offset, vector &lt;int&gt; size, int maxData)
(be sure your method is public)
????

Notes
-
Assume that the reprogrammable memory of the measuring device is infinitely large.
Constraints
-
offset will contain between 1 and 50 elements, inclusive.
-
offset and size will contain the same number of elements.
-
Each element of offset will be between 0 and 1,000,000,000, inclusive.
-
Each element of size will be between 1 and 1,000,000,000, inclusive.
-
None of the pieces of data described by offset and size will overlap.
-
maxData will be between 1 and 2,000,000,000, inclusive.
Examples
0)

????
{0, 10, 20, 30}
{8, 5, 3, 11}
6
Returns: 6
Send 15 bytes starting from offset 0 in 3 packets. Only 13 of those 15 bytes are meaningful. There are 2 dummy bytes starting from offset 8. Then, send 3 bytes starting from offset 20 in 1 packet. Finally, send 11 bytes starting from offset 30 in 2 packets. A total of 6 packets are sent.
1)

????
{0, 10, 20, 30}
{8, 2, 3, 11}
6
Returns: 5
Send 12 bytes starting from offset 0 in 2 packets. Then, send 3 bytes starting from offset 20 in 1 packet. Finally, send 11 bytes starting from offset 30 in 2 packets. A total of 5 packets are sent.
2)

????
{15, 95}
{1, 20}
100
Returns: 1

3)

????
{77, 7777, 777}
{700, 70000, 7000}
1
Returns: 77700

4)

????
{0,1000000000}
{1000000000,1000000000}
2000000000
Returns: 1

5)

????
{0,1000000000}
{1000000000,1000000000}
1
Returns: 2000000000

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
*/


#include &lt;vector&gt;
#include &lt;map&gt;

using namespace std;

class ProgrammingDevice
{
public:
	int minPackets(vector&lt;int&gt; offset, vector&lt;int&gt; size, int maxData)
	{
		map&lt;int, int&gt; m;
		vector&lt;int&gt; sortedOffset, sortedSize;
		int i, counter = 0, n;
		unsigned int theStart, theSize;
		if (maxData == 1)
		{
			for (i = 0; i &lt; size.size(); i ++)
			{
				counter += size[i];
			}
			return counter;
		}
		
		for (i = 0; i &lt; offset.size(); i ++)
		{
			m[offset[i]] = size[i];
		}
		i = 0;
		for (map&lt;int, int&gt;::const_iterator it = m.begin(); it != m.end(); it ++, i ++)
		{
			sortedOffset.push_back(it-&gt;first);
			sortedSize.push_back(it-&gt;second);
		}
		theStart = sortedOffset[0];
		theSize = sortedSize[0];
		for (i = 0; i &lt; sortedOffset.size(); i ++)
		{
			if (theStart &lt;= sortedOffset[i])
			{
				theStart = sortedOffset[i];
				theSize = sortedSize[i];
			}
			else
			{
				theSize = sortedSize[i] - (theStart - sortedOffset[i]);
			}
			n = (theSize + maxData - 1)/maxData;
			counter += n;
			theStart += n * maxData;
		}
		return counter;
	}
};
				
			
			
		
			
int main()
{
	vector&lt;int&gt; offset, size;
	ProgrammingDevice p;
	int counter = 0;
	offset.push_back(0);
	offset.push_back(1000000000);
	size.push_back(1000000000);
	size.push_back(1000000000);

	counter = p.minPackets(offset, size, 2000000000);


	return 0;
}</pre>
	<pre>	</pre>
	<pre>/*

Problem Statement
????
An electronic billboard is supposed to display large letters by using several lightbulbs per letter. Given a message, and how each enlarged letter looks as a 5x5 arrangement of lightbulbs, return the enlarged message.
The enlarged representation of the letters will be in a vector &lt;string&gt; with each element formatted as follows (quotes added for clarity):
&quot;&lt;letter&gt;:*****-*****-*****-*****-*****&quot;
Where &lt;letter&gt; is a single uppercase letter [A-Z], and each * is either the character '#' (representing a lit lightbulb) or a period ('.') (representing an unlit lightbulb). Each group of 5 (delimited by a dash, '-') represents a row in the 5x5 representation of the letter. So, &quot;T:#####-..#..-..#..-..#..-..#..&quot; means that the 5x5 representation of 'T' is:
&quot;#####&quot;
&quot;..#..&quot;
&quot;..#..&quot;
&quot;..#..&quot;
&quot;..#..&quot;
Return the enlarged message as a 5-element vector &lt;string&gt;, with each element representing one row of lightbulbs (where element 0 is the top row). Leave 1 (one) column of periods ('.') between each adjacent pair of letters in the enlarged message.
Definition
????
Class:
Billboard
Method:
enlarge
Parameters:
string, vector &lt;string&gt;
Returns:
vector &lt;string&gt;
Method signature:
vector &lt;string&gt; enlarge(string message, vector &lt;string&gt; letters)
(be sure your method is public)
????

Constraints
-
message will contain between 1 and 10 characters, inclusive.
-
each character of message will be an uppercase letter [A-Z].
-
letters will contain between 1 and 10 elements, inclusive.
-
each element of letters will be exactly 31 characters in length.
-
each element of letters will be formatted as (quotes added for clarity): &quot;&lt;letter&gt;:*****-*****-*****-*****-*****&quot;, where &lt;letter&gt; is a single uppercase letter [A-Z] (inclusive) representing the letter being enlarged, and each * is either the character '#' or a period.
-
every letter appearing in message will have an enlarged representation in letters.
-
each letter represented in letters will be unique.
Examples
0)

????
&quot;TOPCODER&quot;
{&quot;T:#####-..#..-..#..-..#..-..#..&quot;
,&quot;O:#####-#...#-#...#-#...#-#####&quot;
,&quot;P:####.-#...#-####.-#....-#....&quot;
,&quot;C:.####-#....-#....-#....-.####&quot;
,&quot;D:####.-#...#-#...#-#...#-####.&quot;
,&quot;E:#####-#....-####.-#....-#####&quot;
,&quot;R:####.-#...#-####.-#.#..-#..##&quot;}
Returns: 
{ &quot;#####.#####.####...####.#####.####..#####.####.&quot;,
  &quot;..#...#...#.#...#.#.....#...#.#...#.#.....#...#&quot;,
  &quot;..#...#...#.####..#.....#...#.#...#.####..####.&quot;,
  &quot;..#...#...#.#.....#.....#...#.#...#.#.....#.#..&quot;,
  &quot;..#...#####.#......####.#####.####..#####.#..##&quot; }

1)

????
&quot;DOK&quot;
{&quot;D:####.-#...#-#...#-#...#-####.&quot;
,&quot;O:#####-#...#-#...#-#...#-#####&quot;
,&quot;K:#...#-#..#.-###..-#..#.-#...#&quot;}
Returns: 
{ &quot;####..#####.#...#&quot;,
  &quot;#...#.#...#.#..#.&quot;,
  &quot;#...#.#...#.###..&quot;,
  &quot;#...#.#...#.#..#.&quot;,
  &quot;####..#####.#...#&quot; }

2)

????
&quot;RANDOMNESS&quot;
{&quot;S:##.##-#####-#.#.#-#.#.#-####.&quot;
,&quot;N:#####-#####-#####-#####-#####&quot;
,&quot;R:#####-#####-##.##-#####-#####&quot;
,&quot;A:.....-.....-.....-.....-.....&quot;
,&quot;D:#.#.#-.#.#.-#.#.#-.#.#.-#.#.#&quot;
,&quot;O:#####-#...#-#.#.#-#...#-#####&quot;
,&quot;E:#....-.#...-..#..-...#.-....#&quot;
,&quot;M:#....-.....-.....-.....-.....&quot;
,&quot;X:#...#-.#.#.-..#..-.#.#.-#...#&quot;}
Returns: 
{ &quot;#####.......#####.#.#.#.#####.#.....#####.#.....##.##.##.##&quot;,
  &quot;#####.......#####..#.#..#...#.......#####..#....#####.#####&quot;,
  &quot;##.##.......#####.#.#.#.#.#.#.......#####...#...#.#.#.#.#.#&quot;,
  &quot;#####.......#####..#.#..#...#.......#####....#..#.#.#.#.#.#&quot;,
  &quot;#####.......#####.#.#.#.#####.......#####.....#.####..####.&quot; }
Note that the letter X is defined but never used.
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
*/


#include &lt;stdio.h&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

using namespace std;


class Billboard
{
public:
	vector&lt;string&gt; enlarge(string message, vector&lt;string&gt; letters)
	{
		vector&lt;string&gt; result;
		string str[5];
		char* ptr;
		int i, j, index;
		char ch = '.';
		for ( i = 0; i &lt; 5; i ++)
		{
			result.push_back(str[i]);
		}
				
		for (i = 0; i &lt; message.size(); i ++)
		{
			for (j = 0; j &lt; letters.size(); j ++)
			{
				if (message[i] == letters[j][0])
				{
					index = j;
					break;
				}
			}
			for (j = 0; j &lt;  5; j ++)
			{
				ptr =&amp;(letters[index][j * 6 + 2]);
				result[j].append(ptr, 5);
				if (i != message.size() - 1)
				{
					result[j].append(1, ch);
				}
			}
		}
		return result;
	}
};
				
				
			
	
			
	
int main()
{
	Billboard b;
	string str = &quot;DOK&quot;;
	vector&lt;string&gt; v, result;
	string s;
	s.assign(&quot;D:####.-#...#-#...#-#...#-####.&quot;, strlen(&quot;D:####.-#...#-#...#-#...#-####.&quot;));
	v.push_back(s);

	s.assign(&quot;O:#####-#...#-#...#-#...#-#####&quot;, strlen(&quot;O:#####-#...#-#...#-#...#-#####&quot;));

	v.push_back(s);

	s.assign(&quot;K:#...#-#..#.-###..-#..#.-#...#&quot;, strlen(&quot;K:#...#-#..#.-###..-#..#.-#...#&quot;));

	v.push_back(s);

	result = b.enlarge(str, v);
	for (int i = 0; i &lt; result.size(); i ++)
	{
		printf(&quot;%s\n&quot;, result[i].c_str());
	}

	return 0;
}</pre>
	<pre>　</pre>
	<pre><span lang="en-ca"><a name="count-friendly"></a>//////////////////////////////////////////////////////////////////////////////////////////////////</span></pre>
	<pre>Problem Statement
&nbsp;&nbsp;&nbsp;&nbsp;
We call two numbers friendly if they have the same digits, ignoring order or repetition. For example 122213 and 312 are friendly while 145 and 2544411 are not. A sequence is friendly if it contains at least two numbers, and all possible pairs of numbers within it are friendly. Two contiguous subsequences are different if they have a different start index, end index or both.
If we are given the sequence 112, 12, 21, 354, 534345, 345, 2221 then the friendly contiguous subsequences are: {112, 12}, {112, 12, 21}, {12, 21}, {354, 534345}, {354, 534345, 345} and {534345, 345}. {112, 12, 21, 354} is not a friendly contiguous subsequence because 112 and 354 are not friendly numbers and {112, 12, 21, 2221} is not a friendly contignuous subsequence because the elements of the sequence aren't in consecutive positions in the original sequence.
Given a vector &lt;int&gt; array, you must return number of different friendly contignuous subsequences of array.
Definition
&nbsp;&nbsp;&nbsp;&nbsp;
Class:
FriendlySequences
Method:
count
Parameters:
vector &lt;int&gt;
Returns:
int
Method signature:
int count(vector &lt;int&gt; array)
(be sure your method is public)
&nbsp;&nbsp;&nbsp;&nbsp;

Constraints
-
array will have between 0 and 50 elements, inclusive.
-
Each element of array will be between 0 and 2000000000, inclusive.
Examples
0)

&nbsp;&nbsp;&nbsp;&nbsp;
{112, 12, 21, 354, 534345, 345, 2221}
Returns: 6
The example in the problem.
1)

&nbsp;&nbsp;&nbsp;&nbsp;
{10, 1100, 10101, 111, 1111, 11111, 11, 1, 111}
Returns: 18

2)

&nbsp;&nbsp;&nbsp;&nbsp;
{0, 0, 0, 0}
Returns: 6
We have a total of 6 possible different pairs of start and end indices for friendly subsequences.
3)

&nbsp;&nbsp;&nbsp;&nbsp;
{123456890, 213456890, 198654320} 
Returns: 3

4)

&nbsp;&nbsp;&nbsp;&nbsp;
{9}
Returns: 0

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.</pre>
	<pre>#include &lt;vector&gt;

using namespace std;


class FriendlySequences
{
private:
	int isFriendly(int left, int right)
	{
		char strLeft[11], strRight[11];
		int lenLeft, lenRight, i, j;
		bool bFound;
		sprintf(strLeft, &quot;%d&quot;, left);
		sprintf(strRight, &quot;%d&quot;, right);
		lenLeft = strlen(strLeft);
		lenRight = strlen(strRight);
		for (i = 0; i &lt; lenLeft; i ++)
		{
			bFound = false;
			for (j = 0; j &lt; lenRight; j ++)
			{
				if (strLeft[i] == strRight[j])
				{
					bFound = true;
					break;
				}
			}
			if (!bFound)
			{
				return false;
			}
		}
		return true;		
	}
	
	int calculate(int n)
	{
		if (n &gt; 0)
		{
			return (n - 1) * n / 2;
		}
		else
		{
			return 0;
		}
	}

public:
	int count(vector &lt;int&gt; array)
	{
		int nStart, nEnd, i, counter = 0;
		nStart = nEnd = 0;
		bool bSame = false;
		for (i = 0; i &lt; array.size(); i ++)
		{
			if (nEnd != i)
			{
				bSame = isFriendly(array[nEnd], array[i]);
			}
			if (!bSame)
			{
				counter += calculate(nEnd - nStart + 1);
				nStart = nEnd = i;
			}
			else
			{
				nEnd ++;
			}
		}
		counter += calculate(nEnd - nStart + 1);
		return counter;
	}
};</pre>
	<pre><span lang="en-ca"><a name="circuit"></a>/////////////////////////////////////////////////////////////////////////////////</span></pre>
	<pre>Problem Statement
&nbsp;&nbsp;&nbsp;&nbsp;
An essential part of circuit design and general system optimization is critical path analysis. On a chip, the critical path represents the longest path any signal would have to travel during execution. In this problem we will be analyzing chip designs to determine their critical path length. The chips in this problem will not contain any cycles, i.e. there exists no path from one component of a chip back to itself.  Given a vector &lt;string&gt; connects representing the wiring scheme, and a vector &lt;string&gt; costs representing the cost of each connection, your method will return the size of the most costly path between any 2 components on the chip. In other words, you are to find the longest path in a directed, acyclic graph. Element j of connects will list the components of the chip that can be reached directly from the jth component (0-based). Element j of costs will list the costs of each connection mentioned in the jth element of connects. As mentioned above, the chip will not contain any cyclic paths. For example:
connects = {&quot;1 2&quot;,
            &quot;2&quot;,
            &quot;&quot;}
costs    = {&quot;5 3&quot;,
            &quot;7&quot;,
            &quot;&quot;}
In this example, component 0 connects to components 1 and 2 with costs 5 and 3 respectively. Component 1 connects to component 2 with a cost of 7. All connections mentioned are directed. This means a connection from component i to component j does not imply a connection from component j to component i. Since we are looking for the longest path between any 2 components, your method would return 12.
Definition
&nbsp;&nbsp;&nbsp;&nbsp;
Class:
Circuits
Method:
howLong
Parameters:
vector &lt;string&gt;, vector &lt;string&gt;
Returns:
int
Method signature:
int howLong(vector &lt;string&gt; connects, vector &lt;string&gt; costs)
(be sure your method is public)
&nbsp;&nbsp;&nbsp;&nbsp;

Constraints
-
connects must contain between 2 and 50 elements inclusive
-
connects must contain the same number of elements as costs
-
Each element of connects must contain between 0 and 50 characters inclusive
-
Each element of costs must contain between 0 and 50 characters inclusive
-
Element i of connects must contain the same number of integers as element i of costs
-
Each integer in each element of connects must be between 0 and the size of connects-1 inclusive
-
Each integer in each element of costs must be between 1 and 1000 inclusive
-
Each element of connects may not contain repeated integers
-
Each element of connects must be a single space delimited list of integers, each of which has no extra leading zeros. There will be no leading or trailing whitespace.
-
Each element of costs must be a single space delimited list of integers, each of which has no extra leading zeros. There will be no leading or trailing whitespace.
-
The circuit may not contain any cycles
-
There will be at least 1 connection.
Examples
0)

&nbsp;&nbsp;&nbsp;&nbsp;
{&quot;1 2&quot;,
 &quot;2&quot;,
 &quot;&quot;}
{&quot;5 3&quot;,
 &quot;7&quot;,
 &quot;&quot;}
Returns: 12
From above
1)

&nbsp;&nbsp;&nbsp;&nbsp;
{&quot;1 2 3 4 5&quot;,&quot;2 3 4 5&quot;,&quot;3 4 5&quot;,&quot;4 5&quot;,&quot;5&quot;,&quot;&quot;}
{&quot;2 2 2 2 2&quot;,&quot;2 2 2 2&quot;,&quot;2 2 2&quot;,&quot;2 2&quot;,&quot;2&quot;,&quot;&quot;}
Returns: 10
The longest path goes from 0-1-2-3-4-5 for a cost of 10.
2)

&nbsp;&nbsp;&nbsp;&nbsp;
{&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;,&quot;&quot;}
{&quot;2&quot;,&quot;2&quot;,&quot;2&quot;,&quot;&quot;,&quot;3&quot;,&quot;3&quot;,&quot;3&quot;,&quot;&quot;}
Returns: 9
The 0-1-2-3 path costs 6 whereas the 4-5-6-7 path costs 9
3)

&nbsp;&nbsp;&nbsp;&nbsp;
{&quot;&quot;,&quot;2 3 5&quot;,&quot;4 5&quot;,&quot;5 6&quot;,&quot;7&quot;,&quot;7 8&quot;,&quot;8 9&quot;,&quot;10&quot;,
 &quot;10 11 12&quot;,&quot;11&quot;,&quot;12&quot;,&quot;12&quot;,&quot;&quot;}
{&quot;&quot;,&quot;3 2 9&quot;,&quot;2 4&quot;,&quot;6 9&quot;,&quot;3&quot;,&quot;1 2&quot;,&quot;1 2&quot;,&quot;5&quot;,
 &quot;5 6 9&quot;,&quot;2&quot;,&quot;5&quot;,&quot;3&quot;,&quot;&quot;}
Returns: 22

4)

&nbsp;&nbsp;&nbsp;&nbsp;
{&quot;&quot;,&quot;2 3&quot;,&quot;3 4 5&quot;,&quot;4 6&quot;,&quot;5 6&quot;,&quot;7&quot;,&quot;5 7&quot;,&quot;&quot;}
{&quot;&quot;,&quot;30 50&quot;,&quot;19 6 40&quot;,&quot;12 10&quot;,&quot;35 23&quot;,&quot;8&quot;,&quot;11 20&quot;,&quot;&quot;}
Returns: 105

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.</pre>
	<pre>#include &lt;vector&gt;
#include &lt;string&gt;</pre>
	<pre>using namespace std;</pre>
	<pre>class Circuits
{
private:
	vector&lt;int&gt; string2vector(string&amp; input)
	{
		char buffer[32];
		vector&lt;int&gt; result;
		int i = 0, number;
		const char* ptr = input.c_str();
		if (ptr == NULL || strlen(ptr) == 0)
		{
			return result;
		}
		</pre>
	<pre>		while (true)
		{			
			if (*ptr == ' ' || *ptr == '\0')
			{
				buffer[i] = '\0';
				number = atoi(buffer);
				result.push_back(number);				
				i = 0;
			}
			else
			{
				buffer[i] = *ptr;
				i ++;
			}
			if (*ptr == '\0')
			{
				break;
			}
			ptr ++;
		}
		return result;
	}
				</pre>
	<pre>	// let's use dynamic programming instead of recursion, NO! it is too complicated now...		
		</pre>
	<pre>	int doHowLong(vector&lt;vector&lt;int&gt; &gt; connects, vector&lt;vector&lt;int&gt; &gt; costs, int start)
	{			
		int longest = 0;
		int i, length;
		for (i = 0; i &lt; connects[start].size(); i ++)
		{
			length = costs[start][i] + doHowLong(connects, costs, connects[start][i]);
			if (length &gt; longest)
			{
				longest = length;
			}
		}
		return longest;
	}	
public:
	int howLong(vector &lt;string&gt; connects, vector &lt;string&gt; costs)
	{
		vector&lt;vector&lt;int&gt; &gt; connect, cost;
		vector&lt;int&gt; node;
		int longest = 0, length;
		int i;
		for (i = 0; i &lt; connects.size(); i ++)
		{
			node = string2vector(connects[i]);
			connect.push_back(node);
		}
		for (i = 0; i &lt; costs.size(); i ++)
		{
			node = string2vector(costs[i]);
			cost.push_back(node);
		}
		</pre>
	<pre>		for (i = 0; i &lt; connect.size(); i ++)
		{
			length = doHowLong(connect, cost, i);
			if (length &gt; longest)
			{
				longest = length;
			}
		}
		</pre>
	<pre>		return longest;
	}
};
		</pre>
	<pre>/*
Problem Statement
Your company controls the flow of oil through a network of pipes. The network is composed of N waypoints, each of which has a number 
between 0 and N-1 inclusive. Some of the waypoints are connected by pipes. You will be given vector &lt;string&gt;s caps and costs
describing these pipes. Each element of caps and costs will be formatted as follows (quotes for clarity):
	&quot;x1,y1 x2,y2 x3,y3 ...&quot;
Each x,y pair is called an indicator. Element i of caps will always contain the same number of indicators as element i of costs.
 Furthermore, the jth indicator of element i of caps will correspond to the jth indicator of element i of costs. If element i 
 of caps contains the indicator x,y it means there is a pipe that allows y units of flow from waypoint i to waypoint x. The 
 corresponding pair x,z in element i of costs gives the cost z attributed to using this pipe.  In this network, one waypoint 
 has been designated as the source, and another as the sink. Your business would like to send oil from the source to the sink 
 but it also wants to save money. For this reason, only 1 path will be activated. Your job is to find the path from the source 
 to the sink that maximizes the ratio capacity/cost. For the purposes of this problem, a path will consist of an ordered sequence 
 of distinct waypoints, from the source to the sink, where each consecutive pair of waypoints are connected by a pipe. The pipes
 on the path must allow oil to flow in the direction of the path (source to sink). No flow of oil may enter the source or leave
 the sink. The cost of a path is the sum of all the costs of the pipes on the path. The capacity is the minimum capacity over 
 all the pipes on the path. Note that the pipes are directed, so you can push oil from waypoint i to waypoint j only if there 
 is a pipe allowing such a flow in that direction. You will return the maximum value of capacity/cost accurate to 1e-9 absolute 
 or relative. If there is no path of pipes from source to finish to push oil through, return 0.
Definition


Class:
PipePath
Method:
capCost
Parameters:
vector &lt;string&gt;, vector &lt;string&gt;, int, int
Returns:
double
Method signature:
double capCost(vector &lt;string&gt; caps, vector &lt;string&gt; costs, int source, int sink)
(be sure your method is public)
????

Notes
-
A pipe from waypoint i to waypoint j does not imply there is a pipe from j to i.
-
There can be multiple pipes carrying oil from waypoint i to waypoint j.
Constraints
-
caps will contain between 2 and 50 elements inclusive.
-
caps and costs will contain the same number of elements.
-
Each element of caps and costs will contain between 0 and 50 characters inclusive.
-
Each element of caps and costs will be a single space delimited list of indicators. Each indicator will take the form (quotes for clarity) &quot;x,y&quot;. x will have no extra leading zeros, and will be between 0 and N-1 inclusive, where N is the number of elements in caps. y will have no extra leading zeros, and will be between 1 and 20000000 inclusive.
-
Element i of caps will not contain the indicator &quot;i,y&quot;.
-
Each element of caps and costs will have no leading or trailing whitespace.
-
Element i of caps will have the same number of indicators as element i of costs.
-
The jth indicator of element i of caps will have the same first value as the jth indicator of element i of costs.
-
source and sink will be between 0 and N-1 inclusive, where N is the number of elements in caps.
-
source and sink will not be equal.
Examples
0)

{&quot;1,10 2,9&quot;,&quot;&quot;,&quot;1,100&quot;}
{&quot;1,100 2,50&quot;,&quot;&quot;,&quot;1,50&quot;}
0
1
Returns: 0.1
The 2 paths from waypoint 0 to waypoint 1 have a cost of 100. Since one has capacity 9, and the other has capacity 10, we choose the capacity 10 path.
1)

{&quot;1,3 3,5&quot;,&quot;3,2 2,6&quot;,&quot;3,2&quot;,&quot;1,1 2,4 4,6&quot;,
 &quot;0,3 2,7&quot;}
{&quot;1,3 3,5&quot;,&quot;3,2 2,6&quot;,&quot;3,2&quot;,&quot;1,1 2,4 4,6&quot;,
 &quot;0,3 2,7&quot;}
0
4
Returns: 0.45454545454545453

2)

????
{&quot;1,15 1,20&quot;,&quot;2,10&quot;,&quot;&quot;}
{&quot;1,10 1,10&quot;,&quot;2,15&quot;,&quot;&quot;}
0
2
Returns: 0.4
There are multiple pipes from waypoint 0 to waypoint 1.
*/

#include &lt;vector&gt;
#include &lt;string&gt;

using namespace std;

class PipePath
{
private:
	vector&lt;vector&lt;pair&lt;int, int&gt; &gt; &gt;capVect;
	vector&lt;vector&lt;pair&lt;int, int&gt; &gt; &gt;costVect;
	int src;
	int dst;
	double doFind(vector&lt;int&gt; path, int minCap,int totalCost, int wayPoint)
	{
		int i;
		int newPoint, newCost, newCap;
		double result = 0, newResult;
		if (wayPoint == dst)
		{
			return (double)minCap/(double)totalCost;
		}
		for (i = 0; i &lt; path.size(); i ++)
		{
			if (wayPoint == path[i])
			{
				return 0.0;
			}
		}
		for (i = 0; i &lt; capVect[wayPoint].size(); i ++)
		{
			newPoint = capVect[wayPoint][i].first;
			newCap = capVect[wayPoint][i].second;
			newCost = costVect[wayPoint][i].second;
			if (i == 0)
			{
				path.push_back(wayPoint);
			}
			else
			{
				path[path.size()-1] = wayPoint;
			}
			if (minCap &lt; newCap)
			{
				newCap = minCap;
			}
			newResult = doFind(path, newCap, totalCost + newCost, newPoint);
			if (newResult &gt; result)
			{
				result = newResult;
			}
		}
		return result;
	}	
		
	vector&lt;pair&lt;int,int&gt; &gt;  string2vector(string&amp; str)
	{
		char buffer[32];
		const char* ptr = str.c_str();
		int number, i;
		
		vector&lt;pair&lt;int, int&gt; &gt; result;
		pair&lt;int, int&gt; node;
		i = 0;
		if (str.size() &gt;  0)
		{
			while (true)
			{
				if (*ptr == ' ' || *ptr == '\0'|| *ptr == ',')
				{
					buffer[i] = '\0';
					number = atoi(buffer);
					i = 0;
					
					if (*ptr == '\0')
					{
						node.second = number;
						result.push_back(node);
						break;
					}
					else
					{
						if (*ptr == ' ')
						{
							node.second = number;
							result.push_back(node);
						}
						else
						{
							if (*ptr == ',')
							{
								node.first = number;
							}
						}
					}
				}
				else
				{
					buffer[i] = *ptr;
					i ++;
				}
				// in any case, we advance
				ptr ++;
			}				
		}			
		return result;	
	}
	
public:
	double capCost(vector &lt;string&gt; caps, vector &lt;string&gt; costs, int source, int sink)
	{
		int i;
		src = source;
		dst = sink;
		vector&lt;int&gt; path;	
		for (i = 0; i &lt; caps.size(); i ++)
		{
			capVect.push_back(string2vector(caps[i]));
			costVect.push_back(string2vector(costs[i]));
		}
		
		return doFind(path, 20000000, 0, source);	
	}
};
	
int main()
{
	vector&lt;string&gt; caps, costs;
	PipePath pipe;
	double result;
	char* capStr[5] = {&quot;1,3 3,5&quot;,&quot;3,2 2,6&quot;,&quot;3,2&quot;,&quot;1,1 2,4 4,6&quot;,  &quot;0,3 2,7&quot;};
	char* costStr[5] = {&quot;1,3 3,5&quot;,&quot;3,2 2,6&quot;,&quot;3,2&quot;,&quot;1,1 2,4 4,6&quot;, &quot;0,3 2,7&quot;};
	for (int i = 0; i &lt; 5; i ++)
	{
		caps.push_back(capStr[i]);
		costs.push_back(costStr[i]);
	}

	result = pipe.capCost(caps, costs, 0, 4);

	return 0;
}

</pre>
	<pre>		
		</pre>
	<pre><span lang="en-ca">///////////////////////////////////////////////////////////////////</span></pre>
	<pre><span lang="en-ca">// the problem</span></pre>
	<pre>Problem Statement
&nbsp;&nbsp;&nbsp;&nbsp;
You have been given a rows-by-cols chessboard, with a list of squares cut out. The list of cutouts will be given in a vector &lt;string&gt; cutouts. Each element of cutouts is a comma-delimited lists of coords. Each coord has the form (quotes for clarity) &quot;r c&quot;. If coord &quot;r c&quot; appears in an element of cutouts, it means that the square at row r column c (0-based) has been removed from the chessboard. This problem will involve placing rooks on a chessboard, so that they cannot attack each other. For a rook to attack a target piece, it must share the same row or column as the target. Your method will return an int that will be the maximum number of rooks that can be placed on the chessboard, such that no pair of rooks can attack each other. Rooks cannot be placed on cut out squares. The cut out squares do not affect where the rooks can attack.
Definition
&nbsp;&nbsp;&nbsp;&nbsp;
Class:
RookAttack
Method:
howMany
Parameters:
int, int, vector &lt;string&gt;
Returns:
int
Method signature:
int howMany(int rows, int cols, vector &lt;string&gt; cutouts)
(be sure your method is public)
&nbsp;&nbsp;&nbsp;&nbsp;

Constraints
-
rows will be between 1 and 300 inclusive.
-
cols will be between 1 and 300 inclusive.
-
cutouts will contain between 0 and 50 elements inclusive.
-
Each element of cutouts will contain between 3 and 50 characters inclusive.
-
Each element of cutouts will be a comma delimited list of coords. Each coord will be of the form &quot;r c&quot;, where
r and c are integers, with no extra leading zeros,
r is between 0 and rows-1 inclusive,
and c is between 0 and cols-1 inclusive.
-
Each element of cutouts will not contain leading or trailing spaces.
Examples
0)

&nbsp;&nbsp;&nbsp;&nbsp;
8
8
{}
Returns: 8
In the example cases the .'s represent squares of the chessboard, and the X's represent cut out squares.
........ 
........ 
........ 
........ 
........ 
........ 
........ 
........
1)

&nbsp;&nbsp;&nbsp;&nbsp;
2
2
{&quot;0 0&quot;,&quot;0 1&quot;,&quot;1 1&quot;,&quot;1 0&quot;}
Returns: 0
XX 
XX  
2)

&nbsp;&nbsp;&nbsp;&nbsp;
3
3
{&quot;0 0&quot;,&quot;1 0&quot;,&quot;1 1&quot;,&quot;2 0&quot;,&quot;2 1&quot;,&quot;2 2&quot;}
Returns: 2
X.. 
XX. 
XXX
3)

&nbsp;&nbsp;&nbsp;&nbsp;
3
3
{&quot;0 0&quot;,&quot;1 2&quot;,&quot;2 2&quot;}
Returns: 3
X.. 
..X 
..X
4)

&nbsp;&nbsp;&nbsp;&nbsp;
200
200
{}
Returns: 200

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.</pre>
	<pre><span lang="en-ca">//////////////////////////////////////////////////</span></pre>
	<pre><span lang="en-ca">// this doesn't work!!! as test case 4 runs more than 2 seconds!!!!</span></pre>
	<pre>#include &lt;vector&gt;
#include &lt;set&gt;
#include &lt;string&gt;

using namespace std;

class RookAttack
{
private:
	int row;
	int col;
	int* checkBoard;
	int* chessBoard;
	pair&lt;int, int&gt; string2pair(string&amp; str)
	{
		pair&lt;int, int&gt; result;
		const char* ptr = str.c_str();
		sscanf(ptr, &quot;%d %d&quot;, &amp;result.first, &amp;result.second);
		return result;
	}
	
	void init(vector&lt;pair&lt;int, int&gt; &gt;&amp; holes)
	{
		int i;
		chessBoard = new int[row*col];
		checkBoard = new int[row*col];
		memset(checkBoard, 0, sizeof(int)*row*col);
		memset(chessBoard, 0, sizeof(int)*row*col);
		for (i = 0;  i &lt; holes.size(); i ++)
		{
			chessBoard[holes[i].first * col + holes[i].second] = -1;
		}	
	}
	void setRook(int r, int c, int level)
	{
		int i, index;
		for (i = 0; i &lt; row; i ++)
		{
			index = i * col + c;
			if (chessBoard[index] == 0)
			{
				chessBoard[index ] = level;
			}
		}
		for (i = 0; i &lt; col; i ++)
		{
			index = r*col + i;
			if (chessBoard[index] == 0)
			{
				chessBoard[index] = level;
			}
		}
	}
	
	void resetRook(int r, int c, int level)
	{
		int i, index;
		for (i = 0; i &lt; row; i ++)
		{
			index = i * col + c;
			if (chessBoard[index] == level)
			{
				chessBoard[index ] = 0;
			}
		}
		for (i = 0; i &lt; col; i ++)
		{
			index = r*col + i;
			if (chessBoard[index] == level)
			{
				chessBoard[index] = 0;
			}
		}
	
	}
	
	int findRook(int level)
	{	
		int result = level;
		int number = 0;
		int r = -1, c = -1;
		int index;
		for (r = 0; r &lt; row; r ++)
		{
			for (c = 0; c &lt; col; c ++)
			{				
				index = r*col + c;
				if (chessBoard[index] == 0 &amp;&amp; checkBoard[index] == 0)
				{					
					setRook(r, c, level);
					checkBoard[index] = 1;
					number = findRook(level + 1);
					if (number &gt; result)
					{
						result = number;
					}
					resetRook(r, c, level);
				}
			}		
		}
		return result;		
	}
	
public:
	int howMany(int rows, int cols, vector &lt;string&gt; cutouts)
	{
		vector&lt;pair&lt;int, int&gt; &gt; holes;
		int i;
		for (i = 0; i &lt; cutouts.size(); i ++)
		{
			holes.push_back(string2pair(cutouts[i]));
		}
		row = rows;
		col = cols;
		init(holes);
		return findRook(1) - 1;
	}
};</pre>
	<pre><span lang="en-ca">///////////////////////////////////////////////////////////////////////////////////////</span></pre>
	Version:0.9 StartHTML:-1 EndHTML:-1 StartFragment:0000000111 
	EndFragment:0000009365
	<style type="text/css">
      <!--
        body { font-family: Monospaced; font-size: 12pt }
        pre { font-family: Monospaced; font-size: 12pt }
      -->
    </style>
	<table id="table1">
		<tr>
			<td colspan="2">
			<h3>Problem Statement </h3>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>We wish to assign a score to the given array <b>values</b>. An 
			empty array is assigned the score 0. Otherwise, we have one of the 
			following cases:

          	<ul>
				<li>The array has an odd number of elements: Compute the median 
				m, remove one copy of m from the array, and then compute the 
				score s of the new smaller array. The resulting score is s + m. 
				</li>
				<li>The array has an even number of elements: Find the maximum 
				element m, remove one copy of m from the array, and then compute 
				the score s of the new smaller array. The resulting score is s - 
				m.
            	</li>
			</ul>
			Return the score of <b>values</b>.
        	</td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Definition </h3>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table2">
				<tr>
					<td>Class:
              		</td>
					<td>MedianProcess
              		</td>
				</tr>
				<tr>
					<td>Method:
              		</td>
					<td>getScore
              		</td>
				</tr>
				<tr>
					<td>Parameters:
              		</td>
					<td>vector &lt;int&gt;
              		</td>
				</tr>
				<tr>
					<td>Returns:
              		</td>
					<td>int
              		</td>
				</tr>
				<tr>
					<td>Method signature:
              		</td>
					<td>int getScore(vector &lt;int&gt; values)
              		</td>
				</tr>
				<tr>
					<td colspan="2">(be sure your method is public)
              		</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
		</tr>
		<tr>
			<td>　</td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Notes </h3>
			</td>
		</tr>
		<tr>
			<td valign="top" align="center">-
        	</td>
			<td>Given K numbers, their median is the ((K+1)/2)-th smallest of 
			them, rounding down for even K, and indexing from 1. For example, 
			the median of (1, 2, 2, 3, 5) is 2, and the median of (11, 13, 12, 
			14, 15) is 13.
        	</td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Constraints </h3>
			</td>
		</tr>
		<tr>
			<td valign="top" align="center">-
        	</td>
			<td><b>values</b> will contain between 0 and 50 elements, inclusive.
        	</td>
		</tr>
		<tr>
			<td valign="top" align="center">-
        	</td>
			<td>Each element of <b>values</b> will be between -1000 and 1000, 
			inclusive.
        	</td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Examples </h3>
			</td>
		</tr>
		<tr>
			<td nowrap="true" align="center">0)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table3">
				<tr>
					<td>
					<table id="table4">
						<tr>
							<td>
							<pre style="font-family: Monospaced; font-size: 12pt">{}</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre style="font-family: Monospaced; font-size: 12pt">Returns: 0</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table5">
						<tr>
							<td colspan="2">The array is empty, so we return 0.
                    		</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td nowrap="true" align="center">1)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table6">
				<tr>
					<td>
					<table id="table7">
						<tr>
							<td>
							<pre style="font-family: Monospaced; font-size: 12pt">{2}</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre style="font-family: Monospaced; font-size: 12pt">Returns: 2</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table8">
						<tr>
							<td colspan="2">We remove the median 2 and obtain an 
							empty array. Thus the return value is 2.
                    		</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td nowrap="true" align="center">2)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table9">
				<tr>
					<td>
					<table id="table10">
						<tr>
							<td>
							<pre style="font-family: Monospaced; font-size: 12pt">{2,3}</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre style="font-family: Monospaced; font-size: 12pt">Returns: -1</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table11">
						<tr>
							<td colspan="2">We first remove the max 3, and later 
							the median 2. Thus the result is -1.
                    		</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td nowrap="true" align="center">3)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table12">
				<tr>
					<td>
					<table id="table13">
						<tr>
							<td>
							<pre style="font-family: Monospaced; font-size: 12pt">{1,1,1,1}</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre style="font-family: Monospaced; font-size: 12pt">Returns: 0</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table14">
						<tr>
							<td colspan="2">　</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td nowrap="true" align="center">4)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table15">
				<tr>
					<td>
					<table id="table16">
						<tr>
							<td>
							<pre style="font-family: Monospaced; font-size: 12pt">{371,-56,933,519,583,520,938,-398,75,-269,895,-790,982,-941,937,888,-416,-360,714,-594,-783,431,595}</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre style="font-family: Monospaced; font-size: 12pt">Returns: -12234</pre>
					</td>
				</tr>
				<tr>
					<td>　</td>
				</tr>
			</table>
			</td>
		</tr>
	</table>
	<p>This problem statement is the exclusive and proprietary property of 
	TopCoder, Inc. Any unauthorized use or reproduction of this information 
	without the prior written consent of TopCoder, Inc. is strictly prohibited. 
	(c)2003, TopCoder, Inc. All rights reserved. </p>
	<pre>　</pre>
	<pre>#include &lt;vector&gt;
#include &lt;algorithm&gt;

using namespace std;

class MedianProcess
{
private:
	int doGetScore(vector&lt;int&gt;&amp; values)
	{
		int length = 0;
		int number;
		length = values.size();
		vector&lt;int&gt;::iterator it;
		if (length == 0)
		{
			return 0;
		}	
		else
		{
			if (length % 2 == 0)
			{
				number = values[length - 1];
				values.pop_back();
				return 	doGetScore(values) - number;
			}
			else
			{
				number = values[length/2];
				it = values.begin();
				for (int i = 0; i &lt; length/2; i ++)
				{
					it ++;
				}
				values.erase(it);
				return doGetScore(values) + number;
			}
		}
	}				
		
public:
	int getScore(vector&lt;int&gt; values)
	{
		sort(values.begin(), values.end());
		return doGetScore(values);
	}
};</pre>
	<pre>　</pre>
	V<a name="basket">ersion:0.9 StartHTML</a>:-1 EndHTML:-1 
	StartFragment:0000000111 EndFragment:0000012513
	<style type="text/css">
      <!--
        body { font-family: Monospaced; font-size: 12pt }
        pre { font-family: Monospaced; font-size: 12pt }
      -->
    </style>
	<table id="table17">
		<tr>
			<td colspan="2">
			<h3>Problem Statement </h3>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>Since the rules of basketball do not allow games to result in 
			ties, comparing two teams during a tournament is usually done by 
			comparing the number of games they've won. When comparing two teams 
			that have played a different number of games, the team that played
			<i>k</i> games less than the other gets <i>k</i>/2 added to its win 
			total. For further clarification, please see the following table 
			(the notation 7/15 means that a team played 15 games and won 7 of 
			them):
			<pre>Team A   Team B    Difference
7 / 15   8 / 20    Team A played 5 games less than team B, so we assume it will win 2.5 games more.
		   Therefore Team A is (7 + 2.5) - 8 = 1.5 games ahead of team B.
8 / 8    18 / 18   Though both teams have won all their games, team A played 10 games less. 
                   We assume it wins 5 of those 10 games, so it is 5 games behind team B.
20 / 40  10 / 20   Team B played 20 games less, so we assume it will have 10 more wins.
                   Therefore, the gap between the teams is 0. </pre>
			<p>Since this comparison is transitive (if team A is better than 
			team B, and team B is better than team C, then team A is better than 
			team C), the teams can be placed into the standings table according 
			to this order. If the gap between two teams is 0, the team whose 
			name comes earlier alphabetically is given the better rank. </p>
			<p>You will be given a vector &lt;string&gt; <b>teams</b>, with the i-th 
			element of <b>teams</b> representing the i-th team. Each element of
			<b>teams</b> will be formatted as &quot;<i>NAME WINS LOSSES</i>&quot; (quotes 
			for clarity), where <i>NAME</i> is a sequence of uppercase letters 
			representing the name of the team, and <i>WINS</i> and <i>LOSSES</i> 
			are integers representing the number of games the team won and lost 
			respectively. You are to sort teams from the best to worst according 
			to the comparison method defined above, and return the standings as 
			a vector &lt;string&gt;. Each element of the result must be formatted as &quot;<i>NAME 
			GAP</i>&quot; (quotes for clarity), where <i>NAME</i> is the team's name 
			and <i>GAP</i> is the gap between this team and the top team in the 
			standings. <i>GAP</i> must consist of one or more digits, followed 
			by a decimal point, followed by exactly one digit. There must be no 
			extra leading zeroes. For example, &quot;0.0&quot;, &quot;0.5&quot; and &quot;1.0&quot; are valid, 
			but &quot;.5&quot;, &quot;0&quot;, &quot;05.0&quot; and &quot;5.&quot; are not. </td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Definition </h3>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table18">
				<tr>
					<td>Class:
              		</td>
					<td>BasketballStandings
              		</td>
				</tr>
				<tr>
					<td>Method:
              		</td>
					<td>constructTable
              		</td>
				</tr>
				<tr>
					<td>Parameters:
              		</td>
					<td>vector &lt;string&gt;
              		</td>
				</tr>
				<tr>
					<td>Returns:
              		</td>
					<td>vector &lt;string&gt;
              		</td>
				</tr>
				<tr>
					<td>Method signature:
              		</td>
					<td>vector &lt;string&gt; constructTable(vector &lt;string&gt; teams)
              		</td>
				</tr>
				<tr>
					<td colspan="2">(be sure your method is public)
              		</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
		</tr>
		<tr>
			<td>　</td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Constraints </h3>
			</td>
		</tr>
		<tr>
			<td valign="top" align="center">-
        	</td>
			<td><b>teams</b> will contain between 1 and 50 elements, inclusive.
        	</td>
		</tr>
		<tr>
			<td valign="top" align="center">-
        	</td>
			<td>Each element of <b>teams</b> will contain between 5 and 16 
			characters, inclusive.
        	</td>
		</tr>
		<tr>
			<td valign="top" align="center">-
        	</td>
			<td>Each element of <b>teams</b> will be formatted as &quot;<i>NAME WINS 
			LOSSES</i>&quot; (quotes for clarity).
        	</td>
		</tr>
		<tr>
			<td valign="top" align="center">-
        	</td>
			<td>In each element of <b>teams</b>, <i>NAME</i> will contain 
			between 1 and 10 uppercase letters ('A'-'Z'), inclusive.
        	</td>
		</tr>
		<tr>
			<td valign="top" align="center">-
        	</td>
			<td>In each element of <b>teams</b>, <i>WINS</i> will be an integer 
			between 0 and 99, with no extra leading zeroes.
        	</td>
		</tr>
		<tr>
			<td valign="top" align="center">-
        	</td>
			<td>In each element of <b>teams</b>, <i>LOSSES</i> will be an 
			integer between 0 and 99, with no extra leading zeroes.
        	</td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Examples </h3>
			</td>
		</tr>
		<tr>
			<td align="center" nowrap="true">0)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table19">
				<tr>
					<td>
					<table id="table20">
						<tr>
							<td>
							<pre>{&quot;A 1 1&quot;, &quot;A 8 8&quot;,&quot;D 0 0&quot;, &quot;B 7 7&quot;, &quot;C 99 99&quot;, &quot;BAA 13 13&quot;}</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: {&quot;A 0.0&quot;, &quot;A 0.0&quot;, &quot;B 0.0&quot;, &quot;BAA 0.0&quot;, &quot;C 0.0&quot;, &quot;D 0.0&quot; }</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table21">
						<tr>
							<td colspan="2">All teams win 50% of their games. 
							They are all tied in the standings and sorted in 
							alphabetical order. Please note that multiple teams 
							may have the same name.
                    		</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td align="center" nowrap="true">1)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table22">
				<tr>
					<td>
					<table id="table23">
						<tr>
							<td>
							<pre>{&quot;B 11 10&quot;, &quot;A 10 10&quot;, &quot;C 10 11&quot;, &quot;D 20 5&quot;}</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: {&quot;D 0.0&quot;, &quot;B 7.0&quot;, &quot;A 7.5&quot;, &quot;C 8.0&quot; }</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table24">
						<tr>
							<td colspan="2">D is a clear favorite here.<br>
							Team B played 1 game more than team A, therefore A 
							is supposed to get 0.5 wins out of this game. Since 
							B has 1 win more, it is 0.5 wins ahead of team A.<br>
							Team B and team C played the same number of games. 
							Team B get 1 win more, therefore it is 1 win ahead.
                    		</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td align="center" nowrap="true">2)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table25">
				<tr>
					<td>
					<table id="table26">
						<tr>
							<td>
							<pre>{&quot;MAVS 54 11&quot;, &quot;SUNS 50 16&quot;, &quot;SPURS 46 20&quot;, &quot;JAZZ 43 23&quot;}</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: {&quot;MAVS 0.0&quot;, &quot;SUNS 4.5&quot;, &quot;SPURS 8.5&quot;, &quot;JAZZ 11.5&quot; }</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table27">
						<tr>
							<td colspan="2">Top 4 NBA team as of March 20, 2007.
                    		</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td align="center" nowrap="true">3)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table28">
				<tr>
					<td>
					<table id="table29">
						<tr>
							<td>
							<pre>{&quot;TEAMA 17 15&quot;, &quot;TEAMB 14 11&quot;, &quot;TEAMC 3 0&quot;,
 &quot;TEAMD 99 94&quot;, &quot;LOSER 10 85&quot;, &quot;WINNER 76 34&quot;}</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: 
{&quot;WINNER 0.0&quot;,
 &quot;TEAMD 18.5&quot;,
 &quot;TEAMB 19.5&quot;,
 &quot;TEAMC 19.5&quot;,
 &quot;TEAMA 20.0&quot;,
 &quot;LOSER 58.5&quot; }</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table30">
						<tr>
							<td colspan="2">　</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
	</table>
	<p>This problem statement is the exclusive and proprietary property of 
	TopCoder, Inc. Any unauthorized use or reproduction of this information 
	without the prior written consent of TopCoder, Inc. is strictly prohibited. 
	(c)2003, TopCoder, Inc. All rights reserved. </p>
	<pre>#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;set&gt;

using namespace std;

class BasketballStandings
{
private:
struct Score;
struct ScoreComp
{
	bool operator()(const Score&amp; left, const Score&amp; right) const
	{
		float result = 0;
		result = left.comp(right);
		if (result == 0.0)
		{
			return left.name.compare(right.name) &lt; 0;
		}
		else
		{
			return result &gt; 0.0;
		}
	}
};
		
struct Score
{	
	int win;
	int lose;
	string name;
	float comp(const Score&amp; other) const
	{		
		return win - other.win - (float)(win + lose - other.win - other.lose) / 2.0;
	}
};

typedef multiset&lt;Score, ScoreComp&gt; ScoreSet;
	Score string2score(string&amp; str)
	{
		Score result;
		int len;
		len = str.find(' ');
		result.name.assign(&amp;(*str.begin()), len);
		sscanf(str.c_str() + len, &quot; %d %d&quot;, &amp;result.win, &amp;result.lose);
		return result;
	}
public:
	vector &lt;string&gt; constructTable(vector &lt;string&gt; teams)
	{
		ScoreSet scoreSet;	
		vector&lt;string&gt; result;
		char buffer[32];
		Score top;
		float gap;
		int i;
		for (i = 0; i &lt; teams.size(); i ++)
		{
			scoreSet.insert(string2score(teams[i]));
		}
		top = *scoreSet.begin();
		for (ScoreSet::iterator it = scoreSet.begin(); it != scoreSet.end(); it ++)
		{
			gap = top.comp(*it);
			sprintf(buffer, &quot;%s %.1f&quot;, it-&gt;name.c_str(), gap);
			result.push_back(buffer);
		}
		return result;
	}
};
				
			
			
		
			
		
		
		

int main()
{
	BasketballStandings basket;
	char* str[] = {&quot;A 1 1&quot;, &quot;A 8 8&quot;,&quot;D 0 0&quot;, &quot;B 7 7&quot;, &quot;C 99 99&quot;, &quot;BAA 13 13&quot;};
	vector&lt;string&gt; input, output;
	int i;
	for (i = 0; i &lt;sizeof(str)/sizeof(char*); i ++)
	{
		input.push_back(str[i]);
	}

	output = basket.constructTable(input);

	for ( i = 0; i &lt; output.size(); i ++)
	{
		printf(&quot;%s\n&quot;, output[i].c_str());
	}
	return 0;
}

</pre>
	V<a name="momentum"></a>ersion:0.9 StartHTML:-1 EndHTML:-1 
	StartFragment:0000000111 EndFragment:0000010588
	<style type="text/css">
      <!--
        body { font-size: 12pt; font-family: Monospaced }
        pre { font-size: 12pt; font-family: Monospaced }
      -->
    </style>
	<table id="table31">
		<tr>
			<td colspan="2">
			<h3>Problem Statement </h3>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>Football announcers are always pontificating about the 
			importance of momentum. Of course, whenever the team that doesn't 
			have the momentum does something good, the explanation is that the 
			momentum has shifted. We want to do a study of football games to 
			determine how big a role momentum actually plays.

          	<p>We define the &quot;m-factor&quot; to be the number of scores that 
			immediately follow a score by the same team minus the number of 
			scores that immediately follow a score by the opponent. So if &quot;ABBAAAAAB&quot; 
			were the sequence of scores in a game between teams A and B, then 
			the m-factor would be 5 - 3 = 2. (The 5 comes from one time that a 
			score by B was immediately followed by another score by B and four 
			times that a score by A was immediately followed by a score by A. 
			Similarly, the 3 comes from the one BA and the two AB's that occur.)
			</p>
			<p>But we have to be careful. A high m-factor will naturally occur 
			if one team is just a lot better than the other. If only one team 
			scores, no score will follow a score by the opponent. So to judge 
			whether momentum played an important role, we have to compare the 
			m-factor for a game to the random m-factor for the given number of 
			scores by the two teams. For a game with n scores, the random 
			m-factor is defined to be the average of the n! m-factors 
			corresponding to the n! (not necessarily distinct) permutations of 
			the scores. For example, the random m-factor of the game &quot;ABA&quot; is 
			the average of the 6 m-factors corresponding to &quot;ABA&quot;, &quot;AAB&quot;, &quot;BAA&quot;, 
			&quot;BAA&quot;, &quot;AAB&quot;, &quot;ABA&quot;. </p>
			<p>Create a class NoMo that contains a method momentum that is given 
			a string <b>game</b> giving the sequence of scores by teams A and B 
			and that returns the m-factor for the <b>game</b> minus the random 
			m-factor for that number of scores by A and B. </td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Definition </h3>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table32">
				<tr>
					<td>Class:
              		</td>
					<td>NoMo
              		</td>
				</tr>
				<tr>
					<td>Method:
              		</td>
					<td>momentum
              		</td>
				</tr>
				<tr>
					<td>Parameters:
              		</td>
					<td>string
              		</td>
				</tr>
				<tr>
					<td>Returns:
              		</td>
					<td>double
              		</td>
				</tr>
				<tr>
					<td>Method signature:
              		</td>
					<td>double momentum(string game)
              		</td>
				</tr>
				<tr>
					<td colspan="2">(be sure your method is public)
              		</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
		</tr>
		<tr>
			<td>　</td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Notes </h3>
			</td>
		</tr>
		<tr>
			<td align="center" valign="top">-
        	</td>
			<td>The returned value must be accurate to within a relative or 
			absolute value of 1E-9.
        	</td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Constraints </h3>
			</td>
		</tr>
		<tr>
			<td align="center" valign="top">-
        	</td>
			<td><b>game</b> will contain between 0 and 25 characters inclusive.
        	</td>
		</tr>
		<tr>
			<td align="center" valign="top">-
        	</td>
			<td>Each character in <b>game</b> will be 'A' or 'B'.
        	</td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Examples </h3>
			</td>
		</tr>
		<tr>
			<td align="center" nowrap="true">0)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table33">
				<tr>
					<td>
					<table id="table34">
						<tr>
							<td>
							<pre>&quot;AAAAAAA&quot;</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: 0.0</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table35">
						<tr>
							<td colspan="2">The m-factor is 6, but so is the 
							random m-factor for 7 scores against 0 scores.
                    		</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td align="center" nowrap="true">1)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table36">
				<tr>
					<td>
					<table id="table37">
						<tr>
							<td>
							<pre>&quot;AAB&quot;</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: 0.6666666666666666</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table38">
						<tr>
							<td colspan="2">The m-factor for this game is 0. The 
							six permutations with their m-factors are: AAB = 0, 
							AAB = 0, ABA = -2, ABA = -2, BAA = 0, BAA = 0 so the 
							random m-factor for 2 scores against 1 is -4/6. Thus 
							we return 0 - (-4/6) = .6666666... So momentum 
							played a positive role in this game.
                    		</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td align="center" nowrap="true">2)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table39">
				<tr>
					<td>
					<table id="table40">
						<tr>
							<td>
							<pre>&quot;AAABBBBB&quot;</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: 5.5</pre>
					</td>
				</tr>
				<tr>
					<td>　</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td align="center" nowrap="true">3)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table41">
				<tr>
					<td>
					<table id="table42">
						<tr>
							<td>
							<pre>&quot;&quot;</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: 0.0</pre>
					</td>
				</tr>
				<tr>
					<td>　</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td align="center" nowrap="true">4)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table43">
				<tr>
					<td>
					<table id="table44">
						<tr>
							<td>
							<pre>&quot;ABABABABABABABABABABABABA&quot;</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: -23.04</pre>
					</td>
				</tr>
				<tr>
					<td>　</td>
				</tr>
			</table>
			</td>
		</tr>
	</table>
	<p>This problem statement is the exclusive and proprietary property of 
	TopCoder, Inc. Any unauthorized use or reproduction of this information 
	without the prior written consent of TopCoder, Inc. is strictly prohibited. 
	(c)2003, TopCoder, Inc. All rights reserved. </p>
	<pre>#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

using namespace std;

class NoMo
{
private:
	int calcMomentum(string game)
	{
		const char* ptr = game.c_str();
		int pos = 0, neg = 0;
		bool bStart = false;
		char prev;
		while (*ptr != '\0')
		{
			if (!bStart)
			{
				bStart = true;						
			}
			else
			{
				if (*ptr == prev)
				{
					pos ++;
				}
				else
				{
					neg ++;
				}
			}
			prev = *ptr;
			ptr ++;
		}	
		return pos - neg;
	}		
		
public:
	double momentum(string game)
	{
		int total = 0, number = 0;
		int cur = 0;
		string value(game);
		vector&lt;int&gt; seq;
		int i;
		cur = calcMomentum(value);
		
		for (i = 0; i &lt; game.size(); i ++)
		{
			seq.push_back(i);
		}
		
		total = cur;
		number = 1;

		while (next_permutation(seq.begin(), seq.end()))
		{
			for (i = 0; i &lt; game.size(); i ++)
			{	
				value[i] = game[seq[i]];
			}				
			total += calcMomentum(value);
			number ++;
		}
				
		return (double)cur - (double)total/(double)number;
	}
};
	

class NoMomentum
{
private:
	int calcMomentum(string game)
	{
		int pos = 0, neg = 0, i;
		for ( i = 1; i &lt; game.size(); i ++)
		{
			if (game[i] == game[ i -1])
			{
				pos ++;
			}
			else
			{
				neg ++;
			}
		}	
		return pos - neg;
	}		
		
public:
	double momentum(string game)
	{
		int total = 0, number = 0;
		int cur = 0;
		int length = 0;
		vector&lt;int&gt; seq;
		int i;
		cur = calcMomentum(game);
		
		for (i = 0; i &lt; game.size(); i ++)
		{
			if (game[i] == 'A')
			{
				length ++;
			}			
		}
		for ( i = 0; i &lt; game.size(); i ++)
		{
			if ( i &lt; length)
			{
				seq.push_back(0);
			}
			else
			{
				seq.push_back(1);
			}
		}
		
		do
		{
			for ( i = 1; i &lt; game.size(); i ++)
			{
				if (seq[i] == seq[ i - 1])
				{
					total ++;
				}
				else
				{
					total --;
				}
			}
			number ++;
		}
		while (next_permutation(seq.begin(), seq.end()));
		double result = (number != 0)?((double)total/(double)number):0;
		return (double)cur - ((number != 0)?((double)total/(double)number):0);
	}
};
	

	
int main()
{
	NoMomentum nomo;
	string game = &quot;ABABABABABABABABABABABABA&quot;;
	double result;
	result = nomo.momentum(game);

	printf(&quot;result = %f\n&quot;, result);

	return 0;
}</pre>
	<pre>　</pre>
	<table id="table45">
		<tr>
			<td colspan="2">
			<h3><a name="music-licenses">Problem Statement </a></h3>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>A popular on-line music service allows customers to play their 
			music on up to three different computers, each of which must have a 
			license. A license can be transferred from one computer to another, 
			but it cannot be transferred back. In other words, once a computer 
			has lost its license, it can never acquire a new one.
			<p>You are auditing customer access records, trying to detect fraud. 
			You have a <b>log</b> of the computers a particular customer has 
			used to play music, and you want to know whether she has done so 
			legally. Each different computer is identified by a single uppercase 
			letter ('A'-'Z'). The string <b>log</b> contains a sequence of these 
			letters. For example, &quot;ABCBAD&quot; means that the customer has played a 
			total of six songs on four distinct computers, with the first song 
			played on computer 'A' and the last song played on computer 'D'. You 
			do not know which computers had licenses at which times, but you can 
			infer that the customer must have transferred a license to computer 
			'D' sometime after the third song and before the sixth song. </p>
			<p>You want to detect when a customer's <b>log</b> could not have 
			happened legally, no matter how she managed her licenses. For 
			example, there is no legal way to produce the <b>log</b> &quot;ABCDABCD&quot;: 
			The customer can only play the first song on computer 'D' by 
			transferring a license from one of the other computers, so one of 
			the three subsequent songs will be played on a computer that has 
			lost its license. When you have detected fraud, you are to return 
			the zero-based index of the first song that proves that fraud 
			occurred. In this case, you would return 6, the index of the second 
			'C', because the prefix &quot;ABCDAB&quot; could have been produced legally 
			but &quot;ABCDABC&quot; could not. (Note that this does not necessarily mean 
			that the 'C' access itself is illegal because the illegal access 
			might have occurred earlier.) </p>
			<p>If the <b>log</b> does not prove fraud, return -1. </td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Definition </h3>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table46">
				<tr>
					<td>Class:
              		</td>
					<td>MusicLicenses
              		</td>
				</tr>
				<tr>
					<td>Method:
              		</td>
					<td>audit
              		</td>
				</tr>
				<tr>
					<td>Parameters:
              		</td>
					<td>string
              		</td>
				</tr>
				<tr>
					<td>Returns:
              		</td>
					<td>int
              		</td>
				</tr>
				<tr>
					<td>Method signature:
              		</td>
					<td>int audit(string log)
              		</td>
				</tr>
				<tr>
					<td colspan="2">(be sure your method is public)
              		</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
		</tr>
		<tr>
			<td>　</td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Constraints </h3>
			</td>
		</tr>
		<tr>
			<td align="center" valign="top">-
        	</td>
			<td><b>log</b> contains between 1 and 50 characters, inclusive.
        	</td>
		</tr>
		<tr>
			<td align="center" valign="top">-
        	</td>
			<td>Each character in <b>log</b> is an uppercase letter ('A'-'Z').
        	</td>
		</tr>
		<tr>
			<td colspan="2">
			<h3>Examples </h3>
			</td>
		</tr>
		<tr>
			<td nowrap="true" align="center">0)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table47">
				<tr>
					<td>
					<table id="table48">
						<tr>
							<td>
							<pre>&quot;ABCBAD&quot;</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: -1</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table49">
						<tr>
							<td colspan="2">The first example above. No fraud 
							has been detected.
                    		</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td nowrap="true" align="center">1)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table50">
				<tr>
					<td>
					<table id="table51">
						<tr>
							<td>
							<pre>&quot;ABCDABCD&quot;</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: 6</pre>
					</td>
				</tr>
				<tr>
					<td>
					<table id="table52">
						<tr>
							<td colspan="2">The second example above. The prefix 
							&quot;ABCDAB&quot; could have been produced legally but &quot;ABCDABC&quot; 
							could not.
                    		</td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td nowrap="true" align="center">2)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table53">
				<tr>
					<td>
					<table id="table54">
						<tr>
							<td>
							<pre>&quot;X&quot;</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: -1</pre>
					</td>
				</tr>
				<tr>
					<td>　</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td nowrap="true" align="center">3)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table55">
				<tr>
					<td>
					<table id="table56">
						<tr>
							<td>
							<pre>&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: -1</pre>
					</td>
				</tr>
				<tr>
					<td>　</td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td nowrap="true" align="center">4)
        	</td>
			<td>　</td>
		</tr>
		<tr>
			<td>&nbsp;&nbsp;&nbsp;&nbsp;
        	</td>
			<td>
			<table id="table57">
				<tr>
					<td>
					<table id="table58">
						<tr>
							<td>
							<pre>&quot;AVERYUNUSUALACCESSPATTERNINDEED&quot;</pre>
							</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td>
					<pre>Returns: 15</pre>
					</td>
				</tr>
				<tr>
					<td>　</td>
				</tr>
			</table>
			</td>
		</tr>
	</table>
	<p>This problem statement is the exclusive and proprietary property of 
	TopCoder, Inc. Any unauthorized use or reproduction of this information 
	without the prior written consent of TopCoder, Inc. is strictly prohibited. 
	(c)2003, TopCoder, Inc. All rights reserved. </p>
	<pre>　</pre>
	<pre>　</pre>
	<pre>
</pre>
</div>
<pre><span lang="en-ca">					</span> <a href="game24.htm"><img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35"> </pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp;</p>

</body>

</html>