Friday, September 20, 2013

Topcoder SRM 589 DIV 2 L1 GooseTattarrattatDiv2

// Topcoder SRM 589 DIV 2 L1 GooseTattarrattatDiv2

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

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

public int getmin(String S) {
int[] cnt = new int[26];
for (int i = 0; i < S.length(); i++) {
int c = S.charAt(i) - 'a';
cnt[c] += 1;
}
int maxCnt = 0;
for (int i = 0; i < cnt.length; i++) {
maxCnt = Math.max(maxCnt, cnt[i]);
}
return S.length() - maxCnt;
}
}

Thursday, September 19, 2013

Topcoder SRM 588 DIV 2 L1 KeyDungeonDiv2

// Topcoder SRM 588 DIV 2 L1 KeyDungeonDiv2

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

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

public int countDoors(int[] doorR, int[] doorG, int[] keys) {
int n = doorR.length;
int cnt = 0;
for (int i = 0; i < n; i++) {
int kwhite = keys[2];
if (keys[0] < doorR[i]) {
kwhite -= doorR[i] - keys[0];
}
if (keys[1] < doorG[i]) {
kwhite -= doorG[i] - keys[1];
}
if (kwhite >= 0)
cnt++;
}
return cnt;
}
}

Monday, September 16, 2013

Topcoder SRM 590 DIV 2 L1 FoxAndGomoku

// Topcoder SRM 590 DIV 2 L1 FoxAndGomoku

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

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

public String win(String[] board) {
int n = board.length;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
// check hor
if (x + 5 <= n) {
int no = 0;
for (int i = x; i < x + 5; i++) {
if (board[y].charAt(i) == 'o')
no++;
}
if (no == 5)
return "found";
}
// check ver
if (y + 5 <= n) {
int no = 0;
for (int i = y; i < y + 5; i++) {
if (board[i].charAt(x) == 'o')
no++;
}
if (no == 5)
return "found";
}
// check right
if (x + 5 <= n && y + 5 <= n) {
int no = 0;
for (int i = 0; i < 5; i++) {
if (board[x + i].charAt(y + i) == 'o')
no++;
}
if (no == 5)
return "found";
}
// check left
if (x + 5 <= n && y + 5 <= n) {
int no = 0;
for (int i = 0; i < 5; i++) {
if (board[x + i].charAt(y + 4 - i) == 'o')
no++;
}
if (no == 5)
return "found";
}
}
}
return "not found";
}
}

Sunday, September 15, 2013

Topcoder SRM 587 DIV 2 L1 InsertZ

// Topcoder SRM 587 DIV 2 L1 InsertZ

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

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

public String canTransform(String init, String goal) {
String s = goal.replaceAll("z", "");
if (s.equals(init))
return "Yes";
return "No";
}
}

Topcoder SRM 586 DIV 2 L1 TeamsSelection

// Topcoder SRM 586 DIV 2 L1 TeamsSelection

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

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

public String simulate(int[] preference1, int[] preference2) {
String result = "";
HashSet<Integer> selected = new HashSet<Integer>();
HashSet<Integer> selectedBy1 = new HashSet<Integer>();
int N = preference1.length;
for (int i = 0; i < N; i++) {
for (int j1 = 0; j1 < N; j1++) {
if (!selected.contains(preference1[j1])) {
selected.add(preference1[j1]);
selectedBy1.add(preference1[j1]);
break;
}
}
for (int j2 = 0; j2 < N; j2++) {
if (!selected.contains(preference2[j2])) {
selected.add(preference2[j2]);
break;
}
}
}
for (int i = 1; i <= N; i++) {
if (selectedBy1.contains(i))
result += "1";
else
result += "2";
}
return result;
}
}

Saturday, September 14, 2013

Topcoder SRM 585 DIV 2 L1 LISNumberDivTwo

// Topcoder SRM 585 DIV 2 L1 LISNumberDivTwo

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

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

public int calculate(int[] seq) {
int cnt = 1;
for (int i = 1; i < seq.length; i++) {
if (seq[i] <= seq[i - 1])
cnt++;
}
return cnt;
}
}

Google Code Jam Round 1C 2013 A Consonants

// Google Code Jam Round 1C 2013 A Consonants

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

//Google Code Jam
public class GoogleCode1 {
private static MyScanner in;
private static PrintStream out;
private static PrintStream sysout = System.out;
static int caseNum;

private static void solve() throws IOException
{
PreCompute();
int C = in.nextInt();
for (int i = 0; i < C; i++) {
caseNum = i + 1;
out.print("Case #" + caseNum + ": ");
solveCase();
}
}

private static void PreCompute() {
}

private static boolean isConsonant(char c) {
if ("aeiou".contains(Character.toString(c)))
return false;
return true;

}

private static void solveCase() throws IOException {
String s = in.nextString();
int N = in.nextInt();
int lastConsConsIdx = -1;
int nCons = 0;
long nv = 0;
for (int i = 0; i < s.length(); i++) {
if (isConsonant(s.charAt(i)))
nCons++;
else
nCons = 0;
if (nCons >= N) {
int firstCharIdx = i + 1 - N;
long nAfter = s.length() - i;
long nBefore = firstCharIdx - lastConsConsIdx;
if (firstCharIdx == 0)
nBefore = 1;
long cnt = nAfter * nBefore;
nv += cnt;
lastConsConsIdx = firstCharIdx;
}
}

out.println(nv);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
boolean usingFileForIO = true;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("E:\\zin.txt");
out = new PrintStream("E:\\zout.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();
}
}

}

Wednesday, September 4, 2013

Codeforces Round #197 (Div. 2) B Xenia and Ringroad

// Codeforces Round #197 (Div. 2) B Xenia and Ringroad

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
{
int n = in.nextInt();
int m = in.nextInt();
long time = 0;
int[] a = new int[m];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
for (int i = 0; i < a.length; i++) {
if (i == 0)
time += a[i] - 1;
else {
if (a[i] >= a[i - 1])
time += a[i] - a[i - 1];
else
time += (n - a[i - 1]) + a[i];
}
}
out.println(time);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
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 #197 (Div. 2) A Helpful Maths

// Codeforces Round #197 (Div. 2) A Helpful Maths

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.nextLine();
int[] nums = new int[4];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '1')
nums[1]++;
else if (c == '2')
nums[2]++;
else if (c == '3')
nums[3]++;
}
int numAll = nums[1] + nums[2] + nums[3];
String r = "";
int i = 0;
for (int j = 1; j <= 3; j++) {
for (int k = 0; k < nums[j]; k++) {
r += j;
i++;
if (i < numAll)
r += "+";
}
}
out.println(r);
}

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

}