// Topcoder Single Round Match 591 Round 1 - Division II, Level One
using System;
using System.Collections.Generic;
using System.Linq;
public class TheArithmeticProgression
{
public double minimumChange ( int a, int b, int c )
{
double bb = ( a + c ) / 2.0;
double aa = 2 * b - c;
double cc = 2 * b - a;
double ans = double.MaxValue;
double r = Math.Abs ( bb - b );
ans = Math.Min ( ans, r );
r = Math.Abs ( aa - a );
ans = Math.Min ( ans, r );
r = Math.Abs ( cc - c );
ans = Math.Min ( ans, r );
return ans;
}
static void Main ( string[] args ) { }
}
Some solution examples of problems in topcoder, codeeval, google code jam, interviewstreet, onlinejudge-uva, codeforces, projecteuler, etc
Showing posts with label easy. Show all posts
Showing posts with label easy. Show all posts
Sunday, December 14, 2014
Topcoder Single Round Match 590 Round 1 - Division II, Level One
// Topcoder Single Round Match 590 Round 1 - Division II, Level One
using System;
using System.Collections.Generic;
using System.Linq;
public class FoxAndGomoku
{
public String win ( String[] board )
{
int cx = board[0].Length;
int cy = board.Length;
for ( int y = 0; y < cy; y++ )
{
for ( int x = 0; x < cx; x++ )
{
int cnt1=0;
int cnt2=0;
int cnt3=0;
int cnt4=0;
for ( int i = 0; i < 5; i++ )
{
if ( ( y + i ) >= 0 && ( y + i ) < cy )
if ( board[y + i][x] == 'o' )
cnt1++;
if ( ( x + i ) >= 0 && ( x + i ) < cx )
if ( board[y][x + i] == 'o' )
cnt2++;
if ( ( ( x + i ) >= 0 && ( x + i ) < cx ) && ( ( y
+ i ) >= 0 && ( y + i ) < cy ) )
if ( board[y + i][x + i] == 'o' )
cnt3++;
if ( ( ( x - i ) >= 0 && ( x - i ) < cx ) && ( ( y
+ i ) >= 0 && ( y + i ) < cy ) )
if ( board[y + i][x - i] == 'o' )
cnt4++;
}
if ( cnt1 >= 5 || cnt2 >= 5 || cnt3 >= 5 || cnt4 >= 5 )
return "found";
}
}
return "not found";
}
static void Main ( string[] args ) { }
}
using System;
using System.Collections.Generic;
using System.Linq;
public class FoxAndGomoku
{
public String win ( String[] board )
{
int cx = board[0].Length;
int cy = board.Length;
for ( int y = 0; y < cy; y++ )
{
for ( int x = 0; x < cx; x++ )
{
int cnt1=0;
int cnt2=0;
int cnt3=0;
int cnt4=0;
for ( int i = 0; i < 5; i++ )
{
if ( ( y + i ) >= 0 && ( y + i ) < cy )
if ( board[y + i][x] == 'o' )
cnt1++;
if ( ( x + i ) >= 0 && ( x + i ) < cx )
if ( board[y][x + i] == 'o' )
cnt2++;
if ( ( ( x + i ) >= 0 && ( x + i ) < cx ) && ( ( y
+ i ) >= 0 && ( y + i ) < cy ) )
if ( board[y + i][x + i] == 'o' )
cnt3++;
if ( ( ( x - i ) >= 0 && ( x - i ) < cx ) && ( ( y
+ i ) >= 0 && ( y + i ) < cy ) )
if ( board[y + i][x - i] == 'o' )
cnt4++;
}
if ( cnt1 >= 5 || cnt2 >= 5 || cnt3 >= 5 || cnt4 >= 5 )
return "found";
}
}
return "not found";
}
static void Main ( string[] args ) { }
}
Sunday, April 27, 2014
Codeforces Round #209 (Div. 2) A Table
// Codeforces Round #209 (Div. 2) A Table
// Problem: http://codeforces.com/contest/359/problem/A
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 ans = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int x = in.nextInt();
if (i == 0 && j == 0)
continue;
if (i == 0 && j == m - 1)
continue;
if (i == n - 1 && j == 0)
continue;
if (i == n - 1 && j == m - 1)
continue;
if (x == 1)
if (i == 0 || j == 0 || i == n - 1 || j == m - 1)
ans = 2;
}
}
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();
}
}
}
// Problem: http://codeforces.com/contest/359/problem/A
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 ans = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int x = in.nextInt();
if (i == 0 && j == 0)
continue;
if (i == 0 && j == m - 1)
continue;
if (i == n - 1 && j == 0)
continue;
if (i == n - 1 && j == m - 1)
continue;
if (x == 1)
if (i == 0 || j == 0 || i == n - 1 || j == m - 1)
ans = 2;
}
}
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();
}
}
}
Friday, April 25, 2014
Codeforces Round #208 (Div. 2) A Dima and Continuous Line
// Codeforces Round #208 (Div. 2) A Dima and Continuous Line
// Problems: http://codeforces.com/contest/358/problem/A
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[] x = new int[n];
for (int i = 0; i < x.length; i++) {
x[i] = in.nextInt();
}
boolean intersect = false;
for (int i = 0; i < n - 1; i++) {
int iL = Math.min(x[i], x[i + 1]);
int iR = Math.max(x[i], x[i + 1]);
for (int j = 0; j < n - 1; j++) {
int jL = Math.min(x[j], x[j + 1]);
int jR = Math.max(x[j], x[j + 1]);
if (i == j)
continue;
if (jL > iL && jL < iR && jR > iR)
intersect = true;
if (jL < iL && jR > iL && jR < iR)
intersect = true;
if (intersect)
break;
}
if (intersect)
break;
}
if (intersect)
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();
}
}
}
// Problems: http://codeforces.com/contest/358/problem/A
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[] x = new int[n];
for (int i = 0; i < x.length; i++) {
x[i] = in.nextInt();
}
boolean intersect = false;
for (int i = 0; i < n - 1; i++) {
int iL = Math.min(x[i], x[i + 1]);
int iR = Math.max(x[i], x[i + 1]);
for (int j = 0; j < n - 1; j++) {
int jL = Math.min(x[j], x[j + 1]);
int jR = Math.max(x[j], x[j + 1]);
if (i == j)
continue;
if (jL > iL && jL < iR && jR > iR)
intersect = true;
if (jL < iL && jR > iL && jR < iR)
intersect = true;
if (intersect)
break;
}
if (intersect)
break;
}
if (intersect)
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();
}
}
}
Friday, April 18, 2014
Codeforces Round #207 (Div. 2) A Group of Students
// Codeforces Round #207 (Div. 2) A Group of Students
// Problem: http://codeforces.com/contest/357/problem/A
// Problem: http://codeforces.com/contest/357/problem/A
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 m = in.nextInt(); int[] c = new int[m + 1]; int[] cum = new int[m + 1]; int total = 0; for (int i = 1; i <= m; i++) { c[i] = in.nextInt(); total += c[i]; cum[i] = total; } int x = in.nextInt(); int y = in.nextInt(); for (int k = 2; k <= m; k++) { int lessThanK = cum[k - 1]; int kOrHigher = total - lessThanK; if (lessThanK >= x && lessThanK <= y && kOrHigher >= x && kOrHigher <= y) { out.println(k); return; } } out.println(0); } 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, April 13, 2014
Codeforces Round #206 (Div. 2) A Vasya and Digital Root
// Codeforces Round #206 (Div. 2) A Vasya and Digital Root
// Problem: http://codeforces.com/contest/355/problem/A
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 k = in.nextInt();
int d = in.nextInt();
if (k == 1)
out.println(d);
else {
if (d == 0)
out.println("No solution");
else {
StringBuilder sb = new StringBuilder();
sb.append("1");
for (int i = 1; i < k - 1; i++) {
sb.append("0");
}
sb.append((d - 1));
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();
}
}
}
// Problem: http://codeforces.com/contest/355/problem/A
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 k = in.nextInt();
int d = in.nextInt();
if (k == 1)
out.println(d);
else {
if (d == 0)
out.println("No solution");
else {
StringBuilder sb = new StringBuilder();
sb.append("1");
for (int i = 1; i < k - 1; i++) {
sb.append("0");
}
sb.append((d - 1));
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();
}
}
}
Codeforces Round #241 (Div. 2) A Guess a number!
// Codeforces Round #241 (Div. 2) A Guess a number!
// Problems: http://codeforces.com/contest/416/problem/A
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 ymin = Integer.MIN_VALUE;
int ymax = Integer.MAX_VALUE;
int n = in.nextInt();
for (int i = 0; i < n; i++) {
String eq = in.nextString();
int x = in.nextInt();
String yn = in.nextString();
if (yn.equals("Y")) {
if (eq.equals(">=")) {
if (x > ymin)
ymin = x;
}
else if (eq.equals("<")) {
if (x - 1 < ymax)
ymax = x - 1;
}
else if (eq.equals(">")) {
if (x + 1 > ymin)
ymin = x + 1;
}
else if (eq.equals("<=")) {
if (x < ymax)
ymax = x;
}
}
else {
if (eq.equals("<")) {
if (x > ymin)
ymin = x;
}
else if (eq.equals(">=")) {
if (x - 1 < ymax)
ymax = x - 1;
}
else if (eq.equals("<=")) {
if (x + 1 > ymin)
ymin = x + 1;
}
else if (eq.equals(">")) {
if (x < ymax)
ymax = x;
}
}
}
if (ymin <= ymax) {
ymin = Math.max(ymin, -2000000000);
ymin = Math.min(ymin, 2000000000);
out.println(ymin);
}
else
out.println("Impossible");
}
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();
}
}
}
// Problems: http://codeforces.com/contest/416/problem/A
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 ymin = Integer.MIN_VALUE;
int ymax = Integer.MAX_VALUE;
int n = in.nextInt();
for (int i = 0; i < n; i++) {
String eq = in.nextString();
int x = in.nextInt();
String yn = in.nextString();
if (yn.equals("Y")) {
if (eq.equals(">=")) {
if (x > ymin)
ymin = x;
}
else if (eq.equals("<")) {
if (x - 1 < ymax)
ymax = x - 1;
}
else if (eq.equals(">")) {
if (x + 1 > ymin)
ymin = x + 1;
}
else if (eq.equals("<=")) {
if (x < ymax)
ymax = x;
}
}
else {
if (eq.equals("<")) {
if (x > ymin)
ymin = x;
}
else if (eq.equals(">=")) {
if (x - 1 < ymax)
ymax = x - 1;
}
else if (eq.equals("<=")) {
if (x + 1 > ymin)
ymin = x + 1;
}
else if (eq.equals(">")) {
if (x < ymax)
ymax = x;
}
}
}
if (ymin <= ymax) {
ymin = Math.max(ymin, -2000000000);
ymin = Math.min(ymin, 2000000000);
out.println(ymin);
}
else
out.println("Impossible");
}
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();
}
}
}
Saturday, April 12, 2014
Codeforces Round #205 (Div. 2) A Domino
// Codeforces Round #205 (Div. 2) A Domino
// Problems: http://codeforces.com/contest/353/problem/A
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[] x = new int[n];
int[] y = new int[n];
int sum = 0;
int sumx = 0;
int sumy = 0;
for (int i = 0; i < n; i++) {
x[i] = in.nextInt();
y[i] = in.nextInt();
sumx += x[i];
sumy += y[i];
}
sum = sumx + sumy;
if (sum % 2 == 1)
out.println(-1);
else {
if (sumx % 2 == 0 && sumy % 2 == 0)
out.println(0);
else {
for (int i = 0; i < n; i++) {
if ((x[i] % 2 == 1 && y[i] % 2 == 0) ||
(x[i] % 2 == 0 && y[i] % 2 == 1)) {
out.println(1);
return;
}
}
out.println(-1);
}
}
}
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();
}
}
}
// Problems: http://codeforces.com/contest/353/problem/A
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[] x = new int[n];
int[] y = new int[n];
int sum = 0;
int sumx = 0;
int sumy = 0;
for (int i = 0; i < n; i++) {
x[i] = in.nextInt();
y[i] = in.nextInt();
sumx += x[i];
sumy += y[i];
}
sum = sumx + sumy;
if (sum % 2 == 1)
out.println(-1);
else {
if (sumx % 2 == 0 && sumy % 2 == 0)
out.println(0);
else {
for (int i = 0; i < n; i++) {
if ((x[i] % 2 == 1 && y[i] % 2 == 0) ||
(x[i] % 2 == 0 && y[i] % 2 == 1)) {
out.println(1);
return;
}
}
out.println(-1);
}
}
}
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();
}
}
}
Saturday, April 5, 2014
Codeforces Round #203 (Div. 2) A TL
// Codeforces Round #203 (Div. 2) A TL
// Problem: http://codeforces.com/contest/350/problem/A
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();
int[] a = new int[n];
int[] b = new int[m];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
for (int i = 0; i < b.length; i++) {
b[i] = in.nextInt();
}
for (int v = 1; v <= 100; v++) {
int nExtra = 0;
boolean allCorrectPass = true;
for (int j = 0; j < a.length; j++) {
if (a[j] > v) {
allCorrectPass = false;
break;
}
if (2 * a[j] <= v)
nExtra++;
}
boolean allWrongFailed = true;
for (int i = 0; i < b.length; i++) {
if (b[i] <= v) {
allWrongFailed = false;
break;
}
}
if (allCorrectPass && nExtra > 0 && allWrongFailed) {
out.println(v);
return;
}
}
out.println(-1);
}
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();
}
}
}
// Problem: http://codeforces.com/contest/350/problem/A
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();
int[] a = new int[n];
int[] b = new int[m];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
for (int i = 0; i < b.length; i++) {
b[i] = in.nextInt();
}
for (int v = 1; v <= 100; v++) {
int nExtra = 0;
boolean allCorrectPass = true;
for (int j = 0; j < a.length; j++) {
if (a[j] > v) {
allCorrectPass = false;
break;
}
if (2 * a[j] <= v)
nExtra++;
}
boolean allWrongFailed = true;
for (int i = 0; i < b.length; i++) {
if (b[i] <= v) {
allWrongFailed = false;
break;
}
}
if (allCorrectPass && nExtra > 0 && allWrongFailed) {
out.println(v);
return;
}
}
out.println(-1);
}
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();
}
}
}
Subscribe to:
Posts (Atom)