Skip to content

Commit fc4943a

Browse files
author
Ahmad Medhat Othman
committed
after tco qualification round 1
1 parent 73b1d03 commit fc4943a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+7346
-0
lines changed

FoxListeningToMusic.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html><body bgcolor="#ffffff" text="#000000"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><p>Fox Jiro is going to listen to some music. He has N songs, numbered 0 to N-1, inclusive. The lengths of the songs are given in the int[] <b>length</b>, where the i-th element is the length in seconds of song i.</p>
2+
<br></br>
3+
<p>The music player he uses has a shuffle feature. Using this feature, he can listen to the songs in random order. More precisely, first the player chooses one song among all songs with equal probability and plays it. When the song ends, the player chooses the next song in the same fashion and plays it immediately. Note that the player may choose the same song more than once in a row.</p>
4+
<br></br>
5+
<p>You are given an int <b>T</b>. Return a double[] P, where P[i] indicates the probability that the player is playing the i-th song <b>T</b>+0.5 seconds after Jiro starts listening to the music in shuffle mode.</p></td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>FoxListeningToMusic</td></tr><tr><td>Method:</td><td>getProbabilities</td></tr><tr><td>Parameters:</td><td>int[], int</td></tr><tr><td>Returns:</td><td>double[]</td></tr><tr><td>Method signature:</td><td>double[] getProbabilities(int[] length, int T)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td>&#160;&#160;&#160;&#160;</td></tr><tr><td></td></tr><tr><td colspan="2"><h3>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>Each element of the returned array must have an absolute or relative error less than 1e-9.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>length</b> will contain 1 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>length</b> will be between 1 and 80,001, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>T</b> will be between 0 and 80,000, inclusive.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1, 2}</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: {0.25, 0.75 }</pre></td></tr><tr><td><table><tr><td colspan="2"><p>There are three possible scenarios that lead up to time 1.5:</p>
6+
<ul>
7+
<li>song 0 -> song 0 (with probability 1/4)</li>
8+
<li>song 0 -> song 1 (with probability 1/4)</li>
9+
<li>song 1 (with probability 1/2)</li>
10+
</ul>
11+
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1, 10, 100, 1000, 10000}</pre></td></tr><tr><td><pre>0</pre></td></tr></table></td></tr><tr><td><pre>Returns: {0.2, 0.2, 0.2, 0.2, 0.2 }</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{5, 8, 4, 7}</pre></td></tr><tr><td><pre>10</pre></td></tr></table></td></tr><tr><td><pre>Returns: {0.1875, 0.3125, 0.1875, 0.3125 }</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{10, 1}</pre></td></tr><tr><td><pre>9</pre></td></tr></table></td></tr><tr><td><pre>Returns: {0.9990234375, 9.765625E-4 }</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{58, 47, 36, 25, 14, 3}</pre></td></tr><tr><td><pre>100</pre></td></tr></table></td></tr><tr><td><pre>Returns:
12+
{0.32895835374381194, 0.26291497538241776, 0.18463894970453887, 0.1312301113062895,
13+
0.07518634032025856, 0.017071269542683242 }</pre></td></tr><tr><td><table><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></body></html>

FoxListeningToMusic.java

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
import java.util.*;
2+
import java.util.regex.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.awt.geom.*;
6+
7+
public class FoxListeningToMusic
8+
{
9+
public double[] getProbabilities(int[] length, int T)
10+
{
11+
12+
}
13+
14+
public static void main(String[] args)
15+
{
16+
long time;
17+
double[] answer;
18+
boolean errors = false;
19+
double[] desiredAnswer;
20+
21+
boolean same;
22+
23+
time = System.currentTimeMillis();
24+
answer = new FoxListeningToMusic().getProbabilities(new int[]{1, 2}, 1);
25+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
26+
desiredAnswer = new double[]{0.25D, 0.75D };
27+
System.out.println("Your answer:");
28+
if (answer.length > 0)
29+
{
30+
System.out.print("\t{ " + answer[0]);
31+
for (int i=1; i<answer.length; i++)
32+
System.out.print(", " + answer[i]);
33+
System.out.println(" }");
34+
}
35+
else
36+
System.out.println("\t{ }");
37+
System.out.println("Desired answer:");
38+
if (desiredAnswer.length > 0)
39+
{
40+
System.out.print("\t{ " + desiredAnswer[0]);
41+
for (int i=1; i<desiredAnswer.length; i++)
42+
System.out.print(", " + desiredAnswer[i]);
43+
System.out.println(" }");
44+
}
45+
else
46+
System.out.println("\t{ }");
47+
same = desiredAnswer.length == answer.length;
48+
for (int i=0; i<answer.length && same; i++)
49+
if (answer[i] != desiredAnswer[i])
50+
same = false;
51+
if (!same)
52+
{
53+
errors = true;
54+
System.out.println("DOESN'T MATCH!!!!");
55+
}
56+
else
57+
System.out.println("Match :-)");
58+
System.out.println();
59+
time = System.currentTimeMillis();
60+
answer = new FoxListeningToMusic().getProbabilities(new int[]{1, 10, 100, 1000, 10000}, 0);
61+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
62+
desiredAnswer = new double[]{0.2D, 0.2D, 0.2D, 0.2D, 0.2D };
63+
System.out.println("Your answer:");
64+
if (answer.length > 0)
65+
{
66+
System.out.print("\t{ " + answer[0]);
67+
for (int i=1; i<answer.length; i++)
68+
System.out.print(", " + answer[i]);
69+
System.out.println(" }");
70+
}
71+
else
72+
System.out.println("\t{ }");
73+
System.out.println("Desired answer:");
74+
if (desiredAnswer.length > 0)
75+
{
76+
System.out.print("\t{ " + desiredAnswer[0]);
77+
for (int i=1; i<desiredAnswer.length; i++)
78+
System.out.print(", " + desiredAnswer[i]);
79+
System.out.println(" }");
80+
}
81+
else
82+
System.out.println("\t{ }");
83+
same = desiredAnswer.length == answer.length;
84+
for (int i=0; i<answer.length && same; i++)
85+
if (answer[i] != desiredAnswer[i])
86+
same = false;
87+
if (!same)
88+
{
89+
errors = true;
90+
System.out.println("DOESN'T MATCH!!!!");
91+
}
92+
else
93+
System.out.println("Match :-)");
94+
System.out.println();
95+
time = System.currentTimeMillis();
96+
answer = new FoxListeningToMusic().getProbabilities(new int[]{5, 8, 4, 7}, 10);
97+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
98+
desiredAnswer = new double[]{0.1875D, 0.3125D, 0.1875D, 0.3125D };
99+
System.out.println("Your answer:");
100+
if (answer.length > 0)
101+
{
102+
System.out.print("\t{ " + answer[0]);
103+
for (int i=1; i<answer.length; i++)
104+
System.out.print(", " + answer[i]);
105+
System.out.println(" }");
106+
}
107+
else
108+
System.out.println("\t{ }");
109+
System.out.println("Desired answer:");
110+
if (desiredAnswer.length > 0)
111+
{
112+
System.out.print("\t{ " + desiredAnswer[0]);
113+
for (int i=1; i<desiredAnswer.length; i++)
114+
System.out.print(", " + desiredAnswer[i]);
115+
System.out.println(" }");
116+
}
117+
else
118+
System.out.println("\t{ }");
119+
same = desiredAnswer.length == answer.length;
120+
for (int i=0; i<answer.length && same; i++)
121+
if (answer[i] != desiredAnswer[i])
122+
same = false;
123+
if (!same)
124+
{
125+
errors = true;
126+
System.out.println("DOESN'T MATCH!!!!");
127+
}
128+
else
129+
System.out.println("Match :-)");
130+
System.out.println();
131+
time = System.currentTimeMillis();
132+
answer = new FoxListeningToMusic().getProbabilities(new int[]{10, 1}, 9);
133+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
134+
desiredAnswer = new double[]{0.9990234375D, 9.765625E-4D };
135+
System.out.println("Your answer:");
136+
if (answer.length > 0)
137+
{
138+
System.out.print("\t{ " + answer[0]);
139+
for (int i=1; i<answer.length; i++)
140+
System.out.print(", " + answer[i]);
141+
System.out.println(" }");
142+
}
143+
else
144+
System.out.println("\t{ }");
145+
System.out.println("Desired answer:");
146+
if (desiredAnswer.length > 0)
147+
{
148+
System.out.print("\t{ " + desiredAnswer[0]);
149+
for (int i=1; i<desiredAnswer.length; i++)
150+
System.out.print(", " + desiredAnswer[i]);
151+
System.out.println(" }");
152+
}
153+
else
154+
System.out.println("\t{ }");
155+
same = desiredAnswer.length == answer.length;
156+
for (int i=0; i<answer.length && same; i++)
157+
if (answer[i] != desiredAnswer[i])
158+
same = false;
159+
if (!same)
160+
{
161+
errors = true;
162+
System.out.println("DOESN'T MATCH!!!!");
163+
}
164+
else
165+
System.out.println("Match :-)");
166+
System.out.println();
167+
time = System.currentTimeMillis();
168+
answer = new FoxListeningToMusic().getProbabilities(new int[]{58, 47, 36, 25, 14, 3}, 100);
169+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
170+
desiredAnswer = new double[]{0.32895835374381194D, 0.26291497538241776D, 0.18463894970453887D, 0.1312301113062895D, 0.07518634032025856D, 0.017071269542683242D };
171+
System.out.println("Your answer:");
172+
if (answer.length > 0)
173+
{
174+
System.out.print("\t{ " + answer[0]);
175+
for (int i=1; i<answer.length; i++)
176+
System.out.print(", " + answer[i]);
177+
System.out.println(" }");
178+
}
179+
else
180+
System.out.println("\t{ }");
181+
System.out.println("Desired answer:");
182+
if (desiredAnswer.length > 0)
183+
{
184+
System.out.print("\t{ " + desiredAnswer[0]);
185+
for (int i=1; i<desiredAnswer.length; i++)
186+
System.out.print(", " + desiredAnswer[i]);
187+
System.out.println(" }");
188+
}
189+
else
190+
System.out.println("\t{ }");
191+
same = desiredAnswer.length == answer.length;
192+
for (int i=0; i<answer.length && same; i++)
193+
if (answer[i] != desiredAnswer[i])
194+
same = false;
195+
if (!same)
196+
{
197+
errors = true;
198+
System.out.println("DOESN'T MATCH!!!!");
199+
}
200+
else
201+
System.out.println("Match :-)");
202+
System.out.println();
203+
204+
205+
if (errors)
206+
System.out.println("Some of the test cases had errors :-(");
207+
else
208+
System.out.println("You're a stud (at least on the test data)! :-D ");
209+
}
210+
211+
}
212+
//Powered by [KawigiEdit] 2.0!

MinimumLiars.class

2.35 KB
Binary file not shown.

MinimumLiars.html

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<html><body bgcolor="#ffffff" text="#000000"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td>There is a group of N people numbered from 0 to N-1. Each of them is either an honest person or a liar. You wish to know how many liars are in the group. To do this, you have asked the same question to every person in the group: "What is the number of liars in this group?". The people in the group themselves know the exact number of liars, but since you are a foreigner in the group, they have not told it to you exactly. Instead, the i-th person answered you as follows: <i>"There are at least </i><b>claim</b>[i] <i>liars in the group."</i> It is known that statements by honest persons are always true. However, statements by liars are <i>always</i> false. So, if a liar claims that there are at least X liars in the group, then in fact there are less than X liars.<br></br>
2+
<br></br>
3+
Now, given a int[] <b>claim</b> containing the N answers that you received, you would like to determine the number of liars in the group. Unfortunately, there's another twist that makes this problem more complicated. The people in the group speak a different language than you, so you might have misunderstood some of their answers. The answers in <b>claim</b> are how you understood them, but they might not match what the people actually told you. If you definitely misunderstood at least one of the answers, return -1. Otherwise, assuming that you understood all answers correctly, return the minimum number of liars that could be in the group.
4+
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>MinimumLiars</td></tr><tr><td>Method:</td><td>getMinimum</td></tr><tr><td>Parameters:</td><td>int[]</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int getMinimum(int[] claim)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td>&#160;&#160;&#160;&#160;</td></tr><tr><td></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>claim</b> will contain between 2 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>claim</b> will be between 0 and 100, inclusive.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1,1,1,2}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2">It would be impossible for all the members of the group to be honest because in that case, all of their answers would be 0. It is, however, possible that only the last person is a liar and each of the first three persons is honest. Therefore the correct answer is 1.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{7,8,1}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">The first two people claim that there are at least 7 and 8 liars, respectively, which is impossible as the group only has three people. Thus, they must be lying. The third person claims there is at least one liar, and this is definitely true since we have already identified two liars, so this person is honest.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{5,5,5,5,5}</pre></td></tr></table></td></tr><tr><td><pre>Returns: -1</pre></td></tr><tr><td><table><tr><td colspan="2">Everybody agrees that there are at least 5 liars in the group. The group contains exactly 5 people, so in fact all of them claim that everybody is a liar. We can't assume that some person is honest because this person definitely wouldn't have claimed him/herself as being a liar. We also can't assume that all of them are liars because in this case it will appear that all their statements are true. Every scenario we can try leads to a contradiction, so you must have misunderstood at least one of the answers.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{0,0,0,4,3,0}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{4,7,5}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2">Every claim made is impossible. Therefore all three people in the group must be lying. </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></body></html>

MinimumLiars.java

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import java.util.*;
2+
import java.util.regex.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.awt.geom.*;
6+
7+
public class MinimumLiars
8+
{
9+
public int getMinimum(int[] claim)
10+
{
11+
int h=0;
12+
int l=0;
13+
int mid =0;
14+
//char res[] = new char[claim.length];
15+
int sz = claim.length;
16+
for (int i =0 ; i < sz ; i++) {
17+
if (sz - h - 1 >= claim[i]) {
18+
//honest
19+
h++;
20+
//res[i] = 'h';
21+
} else {
22+
//laier
23+
l++;
24+
//res[i] = 'l';
25+
if (sz == claim[i]) {
26+
mid ++;
27+
}
28+
}
29+
}
30+
31+
if (mid == sz) {
32+
return -1;
33+
} else {
34+
return l;
35+
}
36+
}
37+
38+
public static void main(String[] args)
39+
{
40+
long time;
41+
int answer;
42+
boolean errors = false;
43+
int desiredAnswer;
44+
45+
46+
time = System.currentTimeMillis();
47+
answer = new MinimumLiars().getMinimum(new int[]{1,1,1,2});
48+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
49+
desiredAnswer = 1;
50+
System.out.println("Your answer:");
51+
System.out.println("\t" + answer);
52+
System.out.println("Desired answer:");
53+
System.out.println("\t" + desiredAnswer);
54+
if (answer != desiredAnswer)
55+
{
56+
errors = true;
57+
System.out.println("DOESN'T MATCH!!!!");
58+
}
59+
else
60+
System.out.println("Match :-)");
61+
System.out.println();
62+
time = System.currentTimeMillis();
63+
answer = new MinimumLiars().getMinimum(new int[]{7,8,1});
64+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
65+
desiredAnswer = 2;
66+
System.out.println("Your answer:");
67+
System.out.println("\t" + answer);
68+
System.out.println("Desired answer:");
69+
System.out.println("\t" + desiredAnswer);
70+
if (answer != desiredAnswer)
71+
{
72+
errors = true;
73+
System.out.println("DOESN'T MATCH!!!!");
74+
}
75+
else
76+
System.out.println("Match :-)");
77+
System.out.println();
78+
time = System.currentTimeMillis();
79+
answer = new MinimumLiars().getMinimum(new int[]{5,5,5,5,5});
80+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
81+
desiredAnswer = -1;
82+
System.out.println("Your answer:");
83+
System.out.println("\t" + answer);
84+
System.out.println("Desired answer:");
85+
System.out.println("\t" + desiredAnswer);
86+
if (answer != desiredAnswer)
87+
{
88+
errors = true;
89+
System.out.println("DOESN'T MATCH!!!!");
90+
}
91+
else
92+
System.out.println("Match :-)");
93+
System.out.println();
94+
time = System.currentTimeMillis();
95+
answer = new MinimumLiars().getMinimum(new int[]{0,0,0,4,3,0});
96+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
97+
desiredAnswer = 2;
98+
System.out.println("Your answer:");
99+
System.out.println("\t" + answer);
100+
System.out.println("Desired answer:");
101+
System.out.println("\t" + desiredAnswer);
102+
if (answer != desiredAnswer)
103+
{
104+
errors = true;
105+
System.out.println("DOESN'T MATCH!!!!");
106+
}
107+
else
108+
System.out.println("Match :-)");
109+
System.out.println();
110+
time = System.currentTimeMillis();
111+
answer = new MinimumLiars().getMinimum(new int[]{4,7,5});
112+
System.out.println("Time: " + (System.currentTimeMillis()-time)/1000.0 + " seconds");
113+
desiredAnswer = 3;
114+
System.out.println("Your answer:");
115+
System.out.println("\t" + answer);
116+
System.out.println("Desired answer:");
117+
System.out.println("\t" + desiredAnswer);
118+
if (answer != desiredAnswer)
119+
{
120+
errors = true;
121+
System.out.println("DOESN'T MATCH!!!!");
122+
}
123+
else
124+
System.out.println("Match :-)");
125+
System.out.println();
126+
127+
128+
if (errors)
129+
System.out.println("Some of the test cases had errors :-(");
130+
else
131+
System.out.println("You're a stud (at least on the test data)! :-D ");
132+
}
133+
134+
}
135+
//Powered by [KawigiEdit] 2.0!

archive/ActivateGame

65.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)