Sunday, August 25, 2013

Codeforces Round #195 (Div. 2) A Vasily the Bear and Triangle

// Codeforces Round #195 (Div. 2) A Vasily the Bear and Triangle

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 x = in.nextInt();
int y = in.nextInt();
int len = Math.abs(x) + Math.abs(y);
int x1;
int y1;
int x2;
int y2;
if (x < 0) {
x1 = -len;
y1 = 0;
x2 = 0;
if (y > 0)
y2 = len;
else
y2 = -len;
}
else {
x1 = 0;
x2 = len;
y2 = 0;
if (y > 0)
y1 = len;
else
y1 = -len;
}
out.println(x1 + " " + y1 + " " + x2 + " " + y2);
}

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 #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 #194 (Div. 2) A Candy Bags

// Codeforces Round #194 (Div. 2) A Candy Bags

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 n2 = n * n;
int p = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n / 2; j++) {
if (j > 1)
out.print(" ");
out.print(p + " ");
out.print(n2 - p + 1);
p++;
}
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();
}
}

}

Codeforces Round #192 (Div. 2) C Purification

// Codeforces Round #192 (Div. 2) C Purification

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[][] tiles = new char[n][n];
boolean allEvilsRowExists = false;
for (int i = 0; i < n; i++) {
String s = in.nextLine();
tiles[i] = s.toCharArray();
if (!s.contains("."))
allEvilsRowExists = true;
}
boolean allEvilsColExists = false;
for (int col = 0; col < n; col++) {
int cntEvils = 0;
for (int row = 0; row < n; row++) {
if (tiles[row][col] == 'E')
cntEvils++;
}
if (cntEvils == n)
allEvilsColExists = true;
}
if (allEvilsColExists && allEvilsRowExists)
out.println(-1);
else {
if (allEvilsRowExists) {
for (int col = 0; col < n; col++) {
for (int row = 0; row < n; row++) {
if (tiles[row][col] == '.') {
out.println((row + 1) + " " + (col + 1));
break;
}
}
}
}
else {
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
if (tiles[row][col] == '.') {
out.println((row + 1) + " " + (col + 1));
break;
}
}
}

}
}
}

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 #192 (Div. 2) B Road Construction

// Codeforces Round #192 (Div. 2) B Road Construction

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();
boolean[] forbid = new boolean[n + 1];
for (int i = 0; i < m; i++) {
int a = in.nextInt();
int b = in.nextInt();
forbid[a] = true;
forbid[b] = true;
}
int center = -1;
for (int i = 1; i <= n; i++) {
if (!forbid[i])
center = i;
}
out.println(n - 1);
if (center >= 0) {
for (int i = 1; i <= n; i++) {
if (i != center) {
out.println(center + " " + i);
}
}
}
}

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

}

Sunday, August 4, 2013

Topcoder SRM 584 DIV 2 L1 TopFox

// Topcoder SRM 584 DIV 2 L1 TopFox

import java.util.*;

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

    public int possibleHandles(String familyName, String givenName) {
        HashSet<String> names = new HashSet<String>();
        for (int i = 1; i <= familyName.length(); i++) {
            String s1 = familyName.substring(0, i);
            for (int j = 1; j <= givenName.length(); j++) {
                String s2 = givenName.substring(0, j);
                String s = s1 + s2;
                names.add(s);
            }
        }

        return names.size();
    }
}