// Topcoder SRM 577 DIV 2 L2 EllysRoomAssignmentsDiv2
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class EllysRoomAssignmentsDiv2 {
public static void main(String[] args) {
EllysRoomAssignmentsDiv2 obj = new EllysRoomAssignmentsDiv2();
System.out.println(
obj.getProbability(
new String[]
{ "1168"
}
));
}
public double getProbability(String[] ratings) {
ArrayList<Integer> all = new ArrayList<Integer>();
StringBuilder sb = new StringBuilder("");
for (String r : ratings) {
sb.append(r);
}
String[] rr = sb.toString().split(" ");
for (String s : rr) {
all.add(Integer.valueOf(s));
}
int rating = all.get(0);
Collections.sort(all, Collections.reverseOrder());
int N = all.size();
if (N <= 20)
return 1;
if (rating == all.get(0))
return 1;
int nrooms = N / 20;
if (N % 20 != 0)
nrooms += 1;
if (nrooms == 1)
return 0;
else {
for (int i = 0; i < nrooms; i++) {
if (rating == all.get(i))
return 0;
}
return 1.0 / nrooms;
}
}
}
Some solution examples of problems in topcoder, codeeval, google code jam, interviewstreet, onlinejudge-uva, codeforces, projecteuler, etc
Showing posts with label probability. Show all posts
Showing posts with label probability. Show all posts
Thursday, June 6, 2013
Wednesday, June 5, 2013
Codeforces Round #185 (Div. 2) B Archer
import java.io.*;
import java.math.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;
private static void solve() throws IOException
{
double a = in.nextInt();
double b = in.nextInt();
double c = in.nextInt();
double d = in.nextInt();
double probWin;
double probZanoesLose = (d - c) / d;
double totProbWin = 0;
double probContinue = 1;
while (true) {
probWin = a / b * probContinue;
double probLose = (1.0 - a / b) * probContinue;
totProbWin += probWin;
probContinue = (probLose * probZanoesLose);
if (probWin < 1e-10)
break;
}
out.println((double) totProbWin);
}
public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
LOCAL_TEST = (cname.equals("ALPHA530"));
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}
solve();
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
public String nextLine() throws IOException {
return inp.nextLine();
}
}
}
import java.math.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;
private static void solve() throws IOException
{
double a = in.nextInt();
double b = in.nextInt();
double c = in.nextInt();
double d = in.nextInt();
double probWin;
double probZanoesLose = (d - c) / d;
double totProbWin = 0;
double probContinue = 1;
while (true) {
probWin = a / b * probContinue;
double probLose = (1.0 - a / b) * probContinue;
totProbWin += probWin;
probContinue = (probLose * probZanoesLose);
if (probWin < 1e-10)
break;
}
out.println((double) totProbWin);
}
public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
LOCAL_TEST = (cname.equals("ALPHA530"));
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}
solve();
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
public String nextLine() throws IOException {
return inp.nextLine();
}
}
}
Subscribe to:
Posts (Atom)