Showing posts with label arrays. Show all posts
Showing posts with label arrays. Show all posts

Saturday, April 19, 2014

TCO 2014 Round 1A Div 1 L2 EllysScrabble

// TCO 2014 Round 1A Div 1 L2 EllysScrabble

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

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

public String getMin(String letters, int maxDistance) {
char[] c = letters.toCharArray();
char[] dest = new char[c.length];
for (int i = 0; i < dest.length; i++) {
dest[i] = ' ';
}
boolean[] moved = new boolean[c.length];
for (int i = 0; i < c.length; i++) {
char minChar = c[i];
if (moved[i])
minChar = Character.MAX_VALUE;

int idxMinChar = i;
for (int j = maxDistance; j >= -maxDistance; j--) {
if (i + j < 0)
continue;
if (i + j >= c.length)
continue;
if (moved[i + j])
continue;
if (j == -maxDistance && !moved[i + j]) {
idxMinChar = i + j;
break;
}
char cj = c[i + j];
if (cj <= minChar || (cj == minChar && j < 0)) {
minChar = cj;
idxMinChar = i + j;
}
}
if (idxMinChar != i) {
dest[i] = c[idxMinChar];
moved[idxMinChar] = true;
}
else {
dest[i] = c[i];
moved[i] = true;
}
c[i] = c[i];
}

return new String(dest);
}
}

Friday, April 18, 2014

Codeforces Round #207 (Div. 2) B Flag Day

// Codeforces Round #207 (Div. 2) B Flag Day
// Problem: http://codeforces.com/contest/357/problem/B

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

import org.omg.CORBA.Environment;

//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();
int[][] dance = new int[m][3];
for (int i = 0; i < m; i++) {
dance[i][0] = in.nextInt();
dance[i][1] = in.nextInt();
dance[i][2] = in.nextInt();
}
int[] color = new int[n + 1];
for (int i = 0; i < m; i++) {
int d1 = dance[i][0];
int d2 = dance[i][1];
int d3 = dance[i][2];
boolean[] colorused = new boolean[3];
for (int j = 0; j < 3; j++) {
colorused[j] = (color[d1] == j + 1 || color[d2] == j + 1 || color[d3] == j + 1);
}
for (int j = 0; j < 3; j++) {
if (color[dance[i][j]] == 0) {
for (int k = 0; k < 3; k++) {
if (!colorused[k]) {
color[dance[i][j]] = k + 1;
colorused[k] = true;
break;
}
}
}
}
}
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++) {
if (i > 1)
sb.append(" ");
sb.append(color[i]);
}

out.println(sb);
}

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

}

Tuesday, April 8, 2014

Codeforces Round #204 (Div. 2) B Jeff and Periods

// Codeforces Round #204 (Div. 2) B Jeff and Periods
// Problem: http://codeforces.com/contest/352/problem/B

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

import org.omg.CORBA.Environment;

//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[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int[] lastpos = new int[100001];
int[] diff = new int[100001];
int[] cnt = new int[100001];
boolean[] exclude = new boolean[100001];
for (int i = 1; i <= 100000; i++) {
lastpos[i] = 0;
diff[i] = 0;
cnt[i] = 0;
exclude[i] = false;
}
for (int i = 1; i <= n; i++) {
int x = a[i - 1];
cnt[x] += 1;
if (cnt[x] > 1) {
int diffpos = i - lastpos[x];
if (diff[x] == 0)
diff[x] = diffpos;
else {
if (diff[x] != diffpos) {
exclude[x] = true;
}
}
}
lastpos[x] = i;
}
int numx = 0;
for (int x = 1; x <= 100000; x++) {
if (cnt[x] > 0 && !exclude[x])
numx++;
}
out.println(numx);
StringBuilder sb = new StringBuilder();
for (int x = 1; x <= 100000; x++) {
if (cnt[x] > 0 && !exclude[x]) {
sb.append("" + x + " " + diff[x] + String.format("%n"));
}
}
out.println(sb);
}

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

}

Sunday, March 16, 2014

Codeforces Round #223 (Div. 2) B Sereja and Stairs

// Codeforces Round #223 (Div. 2) B Sereja and Stairs
// Problem: http://codeforces.com/contest/381/problem/B

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();
Integer[] x = new Integer[n];
for (int i = 0; i < x.length; i++) {
x[i] = in.nextInt();
}
Arrays.sort(x, Collections.reverseOrder());
ArrayList<Integer> list2 = new ArrayList<Integer>();
HashSet<Integer> set1 = new HashSet<Integer>();
list2.add(x[0]);
for (int i = 1; i < x.length; i++) {
if (x[i] < x[i - 1]) {
list2.add(x[i]);
}
else {
if (!x[i].equals(x[0])) {
set1.add(x[i]);
}
}
}
ArrayList<Integer> list1 = new ArrayList<Integer>();
list1.addAll(set1);
Collections.sort(list1);
list1.addAll(list2);
out.println(list1.size());
for (int i = 0; i < list1.size(); i++) {
if (i > 0)
out.print(" ");
out.print(list1.get(i));
}
out.println();
}

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

}

Wednesday, March 12, 2014

Codeforces Round #225 (Div. 2) A Coder

// Codeforces Round #225 (Div. 2) A Coder

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();
char[][] c = new char[n][n];
int cnt = 0;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
if ((x + y) % 2 == 0) {
c[y][x] = 'C';
cnt++;
}
else
c[y][x] = '.';
}
}
out.println(cnt);
for (int y = 0; y < n; y++) {
out.println(c[y]);
}
}

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

}

Sunday, February 23, 2014

Codeforces Round #228 (Div. 2) B - Fox and Cross

// Codeforces Round #228 (Div. 2) B - Fox and Cross

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();
char[][] board = new char[n][n];
for (int i = 0; i < n; i++) {
String s = in.nextLine();
board[i] = s.toCharArray();
}
for (int y = 0; y < n - 2; y++) {
for (int x = 0; x < n; x++) {
if (x == 0 || x == n - 1)
continue;
if (board[y][x] == '#' &&
board[y + 1][x - 1] == '#' &&
board[y + 1][x] == '#' &&
board[y + 1][x + 1] == '#' &&
board[y + 2][x] == '#') {
board[y][x] = '.';
board[y + 1][x - 1] = '.';
board[y + 1][x] = '.';
board[y + 1][x + 1] = '.';
board[y + 2][x] = '.';
}
}
}

int cntSign = 0;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
if (board[y][x] == '#')
cntSign++;
}
}
if (cntSign == 0)
out.println("YES");
else
out.println("NO");
}

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

}

Thursday, January 30, 2014

Codeforces Round #218 (Div. 2) A K-Periodic Array

// Codeforces Round #218 (Div. 2) A K-Periodic Array

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 k = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int ngroup = n / k;
int ans;
if (ngroup == 1)
ans = 0;
else {
ans = 0;
for (int i = 0; i < k; i++) {
int num1 = 0;
int num2 = 0;
for (int j = 0; j < ngroup; j++) {
if (a[j * k + i] == 1)
num1++;
else
num2++;
}
ans += Math.min(num1, num2);
}
}
out.println(ans);
}

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

}

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, August 25, 2013

Codeforces Round #194 (Div. 2) B Eight Point Sets

// Codeforces Round #194 (Div. 2) B Eight Point Sets

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;

public static class MyPoint implements Comparable<MyPoint> {
public int X;
public int Y;

public int compareTo(MyPoint o) {
if (X != o.X)
return X - o.X;
else
return Y - o.Y;
}
}

private static void solve() throws IOException
{
ArrayList<MyPoint> pp = new ArrayList<MainCodeforces1.MyPoint>();
HashSet<Integer> sx = new HashSet<Integer>();
HashSet<Integer> sy = new HashSet<Integer>();
for (int i = 0; i < 8; i++) {
MyPoint p = new MyPoint();
p.X = in.nextInt();
p.Y = in.nextInt();
pp.add(p);
sx.add(p.X);
sy.add(p.Y);
}
Collections.sort(pp);
MyPoint[] q = pp.toArray(new MyPoint[pp.size()]);
boolean respectable = false;
if (sx.size() == 3 && sy.size() == 3 &&
q[0].X == q[1].X &&
q[1].X == q[2].X &&
q[3].X == q[4].X &&
q[5].X == q[6].X &&
q[6].X == q[7].X &&
q[0].Y == q[3].Y &&
q[0].Y == q[5].Y &&
q[1].Y == q[6].Y &&
q[2].Y == q[4].Y &&
q[2].Y == q[7].Y)
respectable = true;
if (respectable)
out.println("respectable");
else
out.println("ugly");
}

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

}

Friday, August 23, 2013

Codeforces Round #192 (Div. 2) A Cakeminator

// Codeforces Round #192 (Div. 2) A Cakeminator
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 r = in.nextInt();
int c = in.nextInt();
char[][] cc = new char[r][c];
for (int i = 0; i < r; i++) {
String s = in.nextString();
cc[i] = s.toCharArray();
}

int cnt = 0;
for (int i = 0; i < r; i++) {
boolean hasStrawberry = false;
for (int j = 0; j < c; j++) {
if (cc[i][j] == 'S') {
hasStrawberry = true;
break;
}
}
if (hasStrawberry)
continue;
for (int j = 0; j < c; j++) {
if (cc[i][j] == '.') {
cnt++;
cc[i][j] = '-';
}
}
}

for (int j = 0; j < c; j++) {
boolean hasStrawberry = false;
for (int i = 0; i < r; i++) {
if (cc[i][j] == 'S') {
hasStrawberry = true;
break;
}
}
if (hasStrawberry)
continue;
for (int i = 0; i < r; i++) {
if (cc[i][j] == '.') {
cnt++;
cc[i][j] = '-';
}
}
}
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.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();
}
}

}

Friday, July 26, 2013

Topcoder SRM 583 DIV 2 L1 SwappingDigits

// Topcoder SRM 583 DIV 2 L1 SwappingDigits

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

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

public String minNumber(String num) {
char[] c = num.toCharArray();
for (int i = 0; i < c.length; i++) {
char minx = c[i];
for (int j = c.length - 1; j > i; j--) {
if (c[j] < minx) {
if (i > 0)
minx = c[j];
else if (c[j] > '0')
minx = c[j];
}
}
if (minx == c[i])
continue;

for (int j = c.length - 1; j > i; j--) {
if (c[j] == minx) {
char tmp = c[i];
c[i] = c[j];
c[j] = tmp;
return new String(c);
}
}
}
return new String(c);
}
}

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

}