Sunday, June 30, 2013

Codeforces Round #188 (Div. 2) C Perfect Pair

// Codeforces Round #188 (Div. 2) C Perfect Pair

import java.io.*;
import java.util.*;

//Codeforces
public class Codeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException {
long ops;
long x = in.nextLong();
long y = in.nextLong();
long m = in.nextLong();
ops = 0;
long sumxy;
while (x < m && y < m) {
if (x < 0 && y < 0)
break;

sumxy = x + y;
if (Math.signum(x) * Math.signum(y) < 0) {
long bx = Math.max(x, y);
long by = Math.min(x, y);
long dops = -by / bx;
ops += dops;
by += bx * dops;
x = bx;
y = by;
}
if (x >= m || y >= m)
break;

if (x < y) {
x = x + y;
} else {
y = x + y;
}
if ((x + y) > sumxy) {
ops++;
continue;
} else
break;
}
if (x < m && y < m)
ops = -1;

out.println(ops);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (cname != null)
LOCAL_TEST = true;
} 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 {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException {
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Codeforces Round #188 (Div. 2) B Strings of Power

// Codeforces Round #188 (Div. 2) B Strings of Power

import java.io.*;
import java.util.*;

//Codeforces
public class Codeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException {
String s = in.nextString();
long cnt = 0;
ArrayList<Integer> heavyPos = new ArrayList<Integer>();
ArrayList<Integer> metalPos = new ArrayList<Integer>();
int fromIndex = 0;
while (fromIndex < s.length()) {
int pos = s.indexOf("heavy", fromIndex);
if (pos == -1)
break;
else {
heavyPos.add(pos);
fromIndex = pos + 5;
}
}
fromIndex = 0;
while (fromIndex < s.length()) {
int pos = s.indexOf("metal", fromIndex);
if (pos == -1)
break;
else
metalPos.add(pos);
fromIndex = pos + 5;
}
HashMap<Integer, Integer> firstMetalPosLargerThanHeavyPos = new HashMap<Integer, Integer>();
int lastMetalIdx = 0;
for (int i = 0; i < heavyPos.size(); i++) {
int hpos = heavyPos.get(i);
firstMetalPosLargerThanHeavyPos.put(hpos, -1);
for (int j = lastMetalIdx; j < metalPos.size(); j++) {
if (metalPos.get(j) > heavyPos.get(i)) {
firstMetalPosLargerThanHeavyPos.put(hpos, metalPos.get(j));
lastMetalIdx = j;
break;
}
}
}

HashMap<Integer, Integer> cntMetalsLargerThanPos = new HashMap<Integer, Integer>();
for (int i = 0; i < metalPos.size(); i++) {
int pos = metalPos.get(i);
cntMetalsLargerThanPos.put(pos, metalPos.size() - 1 - i);
}

for (int i = 0; i < heavyPos.size(); i++) {
int hpos = heavyPos.get(i);
int firstMetalLarger = firstMetalPosLargerThanHeavyPos.get(hpos);
if (firstMetalLarger < 0)
continue;
long cntMetalLarger = cntMetalsLargerThanPos.get(firstMetalLarger);
cnt += cntMetalLarger + 1;
}

out.println(cnt);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (cname != null)
LOCAL_TEST = true;
} 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 {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException {
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Saturday, June 29, 2013

Codeforces Round #188 (Div. 2) A Even Odds

// Codeforces Round #188 (Div. 2) A Even Odds

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.StringTokenizer;

//Codeforces
public class Codeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException {
long n = in.nextLong();
long k = in.nextLong();
if (n % 2 == 1)
n = n + 1;
long num;
if (k <= n / 2)
num = (k * 2) - 1;
else
num = (k - n / 2) * 2;

out.println(num);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
LOCAL_TEST = true;
} 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 {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException {
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Friday, June 28, 2013

Topcoder SRM 581 DIV 2 L1 BlackAndWhiteSolitaire

// Topcoder SRM 581 DIV 2 L1 BlackAndWhiteSolitaire

import java.util.*;
import java.math.*;

//rename the class name before submitting
public class BlackAndWhiteSolitaire {
public static void main(String[] args) {
BlackAndWhiteSolitaire obj = new BlackAndWhiteSolitaire();
System.out.println(
obj.minimumTurns(
"BW"
));
}

public int minimumTurns(String cardFront) {
int nbfirst = 0;
int nwfirst = 0;
for (int i = 0; i < cardFront.length(); i++) {
if (i % 2 == 0) {
if (cardFront.charAt(i) == 'B')
nwfirst++;
else
nbfirst++;
}
else {
if (cardFront.charAt(i) == 'B')
nbfirst++;
else
nwfirst++;
}
}
return Math.min(nwfirst, nbfirst);
}

}

Monday, June 24, 2013

Topcoder SRM 580 DIV 2 L2 EelAndRabbit

// Topcoder SRM 580 DIV 2 L2 EelAndRabbit

import java.util.*;
import java.math.*;

//rename the class name before submitting
public class EelAndRabbit {
public static void main(String[] args) {
EelAndRabbit obj = new EelAndRabbit();
System.out.println(
obj.getmax(
new int[] { 2, 4, 3, 2, 2, 1, 10 },
new int[] { 2, 6, 3, 7, 0, 2, 0 }
));
}

public int getmax(int[] l, int[] t) {
HashSet<Integer> times = new HashSet<Integer>();
for (int i = 0; i < t.length; i++) {
times.add(t[i]);
times.add(t[i] + l[i]);
}

ArrayList<Integer> T = new ArrayList<Integer>(times);
Collections.sort(T);
int maxEels = 0;
for (int i = 0; i < T.size(); i++) {
int t1 = T.get(i);
for (int j = i + 1; j < T.size(); j++) {
int t2 = T.get(j);
int numEels = 0;
for (int k = 0; k < l.length; k++) {
if (t1 >= t[k] && t1 <= t[k] + l[k]) {
numEels++;
}
else if (t2 >= t[k] && t2 <= t[k] + l[k]) {
numEels++;
}
}
maxEels = Math.max(maxEels, numEels);
}
}

return maxEels;
}

}

Wednesday, June 12, 2013

Topcoder SRM 579 DIV 2 L1 PrimalUnlicensedCreatures

//  Topcoder SRM 579 DIV 2 L1 PrimalUnlicensedCreatures
import java.util.*;
import java.math.*;

//rename the class name before submitting
public class PrimalUnlicensedCreatures {
    public static void main(String[] args) {
        PrimalUnlicensedCreatures obj = new PrimalUnlicensedCreatures();
        System.out.println(
                obj.maxWins(
                        0, null
                        ));
    }

    public int maxWins(int initialLevel, int[] grezPower) {
        Arrays.sort(grezPower);
        int cnt = 0;
        for (int i = 0; i < grezPower.length; i++) {
            if (initialLevel > grezPower[i]) {
                initialLevel += grezPower[i] / 2;
                cnt++;
            }
            else
                break;
        }
        return cnt;
    }

}

Saturday, June 8, 2013

Codeforces Round #133 (Div. 2) A Tiling with Hexagons

// Codeforces Round #133 (Div. 2) A Tiling with Hexagons

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class Codeforces1 {
    private static MyScanner in;
    private static PrintStream out;
    private static boolean LOCAL_TEST = false;

    private static void solve() throws IOException
    {
        int a = in.nextInt();
        int b = in.nextInt();
        int c = in.nextInt();
        long cnt = 0;
        while (true){
            if (a==1 || b==1 || c==1){
                cnt+=(a*b*c);
                break;
            }
            else{
                cnt+=(a+b+c-2+a+b+c-2-2);
            }
            a--;b--;c--;
        }
       
        out.println(cnt);
    }

    public static void main(String[] args) throws IOException {
        // helpers for input/output
        out = System.out;
        try {
            String cname = System.getenv("COMPUTERNAME");
            LOCAL_TEST = true;// (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 {
        BufferedReader bufReader;
        StringTokenizer strTok;

        public MyScanner() throws IOException
        {
            bufReader = new BufferedReader(new InputStreamReader(System.in));
            strTok = new StringTokenizer("");
        }

        public MyScanner(String inputFile) throws IOException {
            bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
                    inputFile)));
            strTok = new StringTokenizer("");
        }

        String GetNextToken() throws IOException {
            if (!strTok.hasMoreTokens())
                strTok = new StringTokenizer(bufReader.readLine());
            return strTok.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.valueOf(GetNextToken());
        }

        public long nextLong() throws IOException {
            return Long.valueOf(GetNextToken());
        }

        public double nextDouble() throws IOException {
            return Double.valueOf(GetNextToken());
        }

        public String nextString() throws IOException {
            return GetNextToken();
        }

        public String nextLine() throws IOException {
            return bufReader.readLine();
        }

        public int countTokens() {
            return strTok.countTokens();
        }

        public boolean hasMoreTokens() {
            return strTok.hasMoreTokens();
        }
    }

}

Friday, June 7, 2013

Codeforces Round #186 (Div. 2) B Ilya and Queries

// Codeforces Round #186 (Div. 2) B Ilya and Queries

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
    {
        String s = in.nextString();
        char[] cs = s.toCharArray();
        int N = s.length();
        int[] cntFrom1ToX = new int[N + 1];
        cntFrom1ToX[1] = 0;
        if (cs[0] == cs[1])
            cntFrom1ToX[1] = 1;
        for (int i = 1; i < N - 1; i++) {
            if (cs[i] == cs[i + 1])
                cntFrom1ToX[i + 1] = cntFrom1ToX[i] + 1;
            else
                cntFrom1ToX[i + 1] = cntFrom1ToX[i];
        }

        int M = in.nextInt();
        for (int i = 0; i < M; i++) {
            int L = in.nextInt();
            int R = in.nextInt();
            int cnt = cntFrom1ToX[R - 1] - cntFrom1ToX[L - 1];
            out.println(cnt);
        }
    }

    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();
        }

    }

}

Codeforces Round #186 (Div. 2) A Ilya and Bank Account

// Codeforces Round #186 (Div. 2) A Ilya and Bank Account

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
    {
        long N = in.nextLong();
        long maxN;
        if (N > 0)
            maxN = N;
        else {
            long m1 = N / 10;
            long m2 = N % 10 + N / 100 * 10;
            maxN = Math.max(m1, m2);
        }
        out.println(maxN);
    }

    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();
        }

    }

}

Thursday, June 6, 2013

Topcoder SRM 578 DIV 2 L1 DeerInZooDivTwo

//  Topcoder SRM 578 DIV 2 L1 DeerInZooDivTwo
import java.util.*;
import java.math.*;

//rename the class name before submitting
public class DeerInZooDivTwo {
    public static void main(String[] args) {
        DeerInZooDivTwo obj = new DeerInZooDivTwo();
        System.out.println(
                obj.getminmax(
                        0, 0
                        ));
    }

    public int[] getminmax(int N, int K) {
        int min = 0;
        int max = N;
        int remain = 2 * N - K;
        min = remain - N;
        if (min < 0)
            min = 0;
        max = remain / 2;
        return new int[] { min, max };
    }
}

Topcoder SRM 577 DIV 2 L2 EllysRoomAssignmentsDiv2

// 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;
        }
    }
}

Topcoder SRM 577 DIV 2 L1 EllysNewNickname

// Topcoder SRM 577 DIV 2 L1 EllysNewNickname

import java.util.*;
import java.math.*;

//rename the class name before submitting
public class EllysNewNickname {
    public static void main(String[] args) {
        EllysNewNickname obj = new EllysNewNickname();
        System.out.println(
                obj.getLength(
                        "a"
                        ));
    }

    public int getLength(String nickname) {
        int cnt = 0;
        boolean prevCharIsVowel = false;
        String vowels = "aeiouy";
        for (int i = 0; i < nickname.length(); i++) {
            boolean isVowel = vowels.contains(String.valueOf(nickname.charAt(i)));
            if (!prevCharIsVowel || !isVowel)
                cnt++;
            prevCharIsVowel = isVowel;
        }
        return cnt;
    }

}

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();
        }

    }

}

Monday, June 3, 2013

Topcoder SRM 580 DIV 2 L1 ShoutterDiv2

// Topcoder SRM 580 DIV 2 L1 ShoutterDiv2

import java.util.*;
import java.math.*;

//rename the class name before submitting
public class ShoutterDiv2 {
    public static void main(String[] args) {
        ShoutterDiv2 obj = new ShoutterDiv2();
        System.out.println(
                obj.count(
                        new int[] { 9, 26, 8, 35, 3, 58, 91, 24, 10, 26, 22, 18, 15, 12,
                                15, 27, 15, 60, 76, 19, 12, 16, 37, 35, 25, 4, 22, 47, 65, 3,
                                2, 23, 26, 33, 7, 11, 34, 74, 67, 32, 15, 45, 20, 53, 60, 25,
                                74, 13, 44, 51 },
                        new int[] { 26, 62, 80, 80, 52, 83, 100, 71, 20, 73, 23, 32, 80,
                                37, 34, 55, 51, 86, 97, 89, 17, 81, 74, 94, 79, 85, 77, 97, 87,
                                8, 70, 46, 58, 70, 97, 35, 80, 76, 82, 80, 19, 56, 65, 62, 80,
                                49, 79, 28, 75, 78 }
                        ));
    }

    public int count(int[] s, int[] t) {
        int len = s.length;
        int cnt = 0;
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < len; j++) {
                if (s[i] > t[j] || s[j] > t[i])
                    continue;
                cnt++;
            }
        }
        return cnt;
    }

}

Sunday, June 2, 2013

Topcoder SRM 576 DIV 2 L2 ArcadeManao

// Topcoder SRM 576 DIV 2 L2 ArcadeManao

import java.util.*;
import java.math.*;

//rename the class name before submitting
public class ArcadeManao {
    public static void main(String[] args) {
        ArcadeManao obj = new ArcadeManao();
        Object o =
                obj.shortestLadder(
                        null,
                        0,
                        0
                        );
        System.out.println(o);
    }

    static boolean found;

    public int shortestLadder(String[] level, int coinRow, int coinColumn) {
        int row = level.length;
        int col = level[0].length();
        int L;
        for (L = 0; L < row; L++) {
            boolean[][] visited = new boolean[row][col];
            found = false;
            Search(L, level, row, 1, coinRow, coinColumn, visited);
            if (found)
                break;
        }
        return L;
    }

    private void Search(int L, String[] lev, int fromRow, int fromCol, int coinR,
            int coinC, boolean[][] visited) {
        if (found)
            return;
        if (fromRow < 1 || fromRow > lev.length || fromCol < 1
                || fromCol > lev[0].length())
            return;
        if (visited[fromRow - 1][fromCol - 1])
            return;
        else
            visited[fromRow - 1][fromCol - 1] = true;
        if (lev[fromRow - 1].charAt(fromCol - 1) == '.')
            return;
        if (fromRow == coinR && fromCol == coinC)
            found = true;
        // to left
        if (fromCol > 1 && lev[fromRow - 1].charAt(fromCol - 1 - 1) == 'X')
            Search(L, lev, fromRow, fromCol - 1, coinR, coinC, visited);
        // to right
        if (fromCol < lev[0].length()
                && lev[fromRow - 1].charAt(fromCol - 1 + 1) == 'X')
            Search(L, lev, fromRow, fromCol + 1, coinR, coinC, visited);
        for (int LL = 1; LL <= L; LL++) {
            // up
            int newR = fromRow - LL;
            Search(L, lev, newR, fromCol, coinR, coinC, visited);
            // down
            newR = fromRow + LL;
            Search(L, lev, newR, fromCol, coinR, coinC, visited);
        }
        return;
    }
}