Showing posts with label bruteforce. Show all posts
Showing posts with label bruteforce. Show all posts

Friday, April 25, 2014

Codeforces Round #208 (Div. 2) B Dima and Text Messages

// Codeforces Round #208 (Div. 2) B Dima and Text Messages
// Problem: http://codeforces.com/contest/358/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();
String[] s = new String[n];
for (int i = 0; i < s.length; i++) {
s[i] = in.nextLine();
}
String msg = in.nextLine();
int pos = -1;
boolean failed = false;
for (int i = 0; i < s.length; i++) {
pos = msg.indexOf("<", pos + 1);
if (pos == -1) {
failed = true;
break;
}
pos = msg.indexOf("3", pos + 1);
if (pos == -1) {
failed = true;
break;
}
for (int j = 0; j < s[i].length(); j++) {
pos = msg.indexOf(s[i].charAt(j), pos + 1);
if (pos == -1) {
failed = true;
break;
}
}
if (pos == -1) {
failed = true;
break;
}
}
if (!failed) {
pos = msg.indexOf("<", pos + 1);
if (pos == -1) {
failed = true;
}
if (!failed) {
pos = msg.indexOf("3", pos + 1);
if (pos == -1) {
failed = true;
}
}
}
if (failed)
out.println("no");
else
out.println("yes");
}

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

}

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

}

Saturday, April 5, 2014

Codeforces Round #203 (Div. 2) B Resort

// Codeforces Round #203 (Div. 2) B Resort
// Problem: http://codeforces.com/contest/350/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();
int[] type = new int[n + 1];
int[] a = new int[n + 1];
int[] na = new int[n + 1];
for (int i = 1; i <= n; i++) {
type[i] = in.nextInt();
}
for (int i = 1; i <= n; i++) {
a[i] = in.nextInt();
if (a[i] > 0)
na[a[i]]++;
}
int maxLength = 0;
ArrayList<Integer> bestList = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
if (type[i] == 0)
continue;
ArrayList<Integer> list = new ArrayList<Integer>();
int p = i;
list.add(p);
while (true) {
p = a[p];
if (na[p] == 0 || na[p] > 1)
break;
else {
if (type[p] == 0)
list.add(p);
else
break;
}
}
if (list.size() > maxLength) {
maxLength = list.size();
bestList = list;
}
}
out.println(bestList.size());
StringBuilder sb = new StringBuilder();
for (int j = bestList.size() - 1; j >= 0; j--) {
sb.append(bestList.get(j).toString());
if (j > 0)
sb.append(" ");
}
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 #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();
}
}

}

Sunday, March 30, 2014

Codeforces Round #200 (Div. 2) B Simple Molecules

// Codeforces Round #200 (Div. 2) B Simple Molecules
// Problem: http://codeforces.com/contest/344/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 a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
boolean valid = false;
for (int i = 0; i <= a; i++) {
int b12 = i;
int b31 = a - i;
int b23 = c - b31;
valid = (b12 >= 0 && b23 >= 0 && b31 >= 0 && b == (b12 + b23));
if (valid) {
out.println(b12 + " " + b23 + " " + b31);
return;
}
}
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();
}
}

}

Sunday, March 9, 2014

Codeforces Round #226 (Div. 2) B Bear and Strings

// Codeforces Round #226 (Div. 2) B Bear and Strings

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 bearIdx = 0;
int prevBearIdx = -1;
long totalCnt = 0;
while (bearIdx < s.length()) {
int i = s.indexOf("bear", bearIdx);
if (i >= 0) {
int startIdx = i - prevBearIdx;
int endIdx = s.length() - (i + 3);
int cnt = startIdx * endIdx;
totalCnt += cnt;
prevBearIdx = i;
}
bearIdx += 4;
}
out.println(totalCnt);
}

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 #226 (Div. 2) A Bear and Raspberry

// Codeforces Round #226 (Div. 2) A Bear and Raspberry

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 c = in.nextInt();
int maxResult = 0;
int[] x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = in.nextInt();
}
for (int i = 0; i < n - 1; i++) {
int r = x[i] - x[i + 1] - c;
maxResult = Math.max(maxResult, r);
}
out.println(maxResult);
}

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, July 6, 2013

Codeforces Round #191 (Div. 2) A Flipping Game

// Codeforces Round #191 (Div. 2) A Flipping Game

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[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int maxcnt = 0;
for (int from = 0; from < n; from++) {
for (int to = from; to < n; to++) {
int cnt = 0;
for (int i = 0; i < n; i++) {
if (i < from || i > to) {
if (a[i] == 1)
cnt++;
}
else {
if (a[i] == 0)
cnt++;
}
maxcnt = Math.max(cnt, maxcnt);
}
}
}

out.println(maxcnt);
}

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

}

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

}

Thursday, May 30, 2013

Codeforces Round #183 (Div. 2) B Calendar

// Codeforces Round #183 (Div. 2) B Calendar

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

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

    private static void solve() throws IOException
    {
        String s1 = in.nextString();
        String s2 = in.nextString();
        String stemp;
        if (s1.compareTo(s2) > 0) {
            stemp = s1;
            s1 = s2;
            s2 = stemp;
        }
        String[] ss1 = s1.split(":");
        String[] ss2 = s2.split(":");
        int[] maxdate = new int[] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
                31 };
        int[] date1 = new int[3];
        int[] date2 = new int[3];
        for (int i = 0; i < 3; i++) {
            date1[i] = Integer.valueOf(ss1[i]);
            date2[i] = Integer.valueOf(ss2[i]);
        }
        int i = 0;
        while (true) {
            boolean equal = true;
            for (int j = 0; j < 3; j++) {
                if (date1[j] != date2[j])
                    equal = false;
            }
            if (equal)
                break;

            i++;
            // inc date1
            boolean leapyear = false;
            if (date1[0] % 400 == 0)
                leapyear = true;
            else if (date1[0] % 100 == 0)
                leapyear = false;
            else if (date1[0] % 4 == 0)
                leapyear = true;
            int curMaxDate = maxdate[date1[1]];
            if (date1[1] == 2 && leapyear)
                curMaxDate = 29;

            if (date1[2] == curMaxDate)
                date1[2] = 1;
            else
                date1[2] += 1;
            if (date1[2] == 1) {
                if (date1[1] == 12)
                    date1[1] = 1;
                else
                    date1[1] += 1;
                if (date1[1] == 1)
                    date1[0]++;
            }

        }

        out.println(i);
    }

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

    }

}