// TopCoder TCO 2013 Round 1A Div 1, L1 HouseBuilding
import java.util.*;
import java.math.*;
class TopcoderSolution {
public static void main(String[] args) {
HouseBuilding obj = new HouseBuilding();
System.out.println(
// obj.numPairs(numbers)
obj.getMinimum(new String[]
{ "10",
"31" })
);
}
}
// change to public before submit
public class HouseBuilding {
public int getMinimum(String[] area) {
int minEffort = Integer.MAX_VALUE;
for (int lev = 0; lev <= 9; lev++) {
int effort = 0;
for (int i = 0; i < area.length; i++) {
for (int j = 0; j < area[i].length(); j++) {
int curlev = area[i].charAt(j) - '0';
int diff = Math.min(Math.abs(curlev - lev),
Math.abs(curlev - (lev + 1)));
if (diff > 0) {
int add = Math.min(Math.abs(curlev - lev),
Math.abs(curlev - (lev + 1)));
effort += add;
}
}
}
minEffort = Math.min(minEffort, effort);
}
return minEffort;
}
}
Some solution examples of problems in topcoder, codeeval, google code jam, interviewstreet, onlinejudge-uva, codeforces, projecteuler, etc
Saturday, February 23, 2013
Wednesday, February 20, 2013
Codeforces Round #168 (Div. 2) A. Lights Out
// Codeforces Round #168 (Div. 2) A. Lights Out
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
boolean[][] L = new boolean[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
L[i][j] = true;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int p = in.nextInt();
if (p % 2 == 1) {
L[i][j] = !L[i][j];
for (int k = 0; k < 3; k++) {
if (Math.abs(k - j) == 1)
L[i][k] = !L[i][k];
if (Math.abs(k - i) == 1)
L[k][j] = !L[k][j];
}
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (L[i][j])
out.print("1");
else
out.print("0");
}
if (i < 2)
out.println("");
}
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
boolean[][] L = new boolean[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
L[i][j] = true;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int p = in.nextInt();
if (p % 2 == 1) {
L[i][j] = !L[i][j];
for (int k = 0; k < 3; k++) {
if (Math.abs(k - j) == 1)
L[i][k] = !L[i][k];
if (Math.abs(k - i) == 1)
L[k][j] = !L[k][j];
}
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (L[i][j])
out.print("1");
else
out.print("0");
}
if (i < 2)
out.println("");
}
}
// =====================================
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();
}
}
}
Codeforces Round #168 (Div. 2) C. k-Multiple Free Set
// Codeforces Round #168 (Div. 2) C. k-Multiple Free Set
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int K = in.nextInt();
int ans;
int[] a = new int[N];
for (int i = 0; i < N; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
long max = a[N - 1];
boolean[] used = new boolean[N];
for (int i = 0; i < N; i++) {
used[i] = true;
}
ans = 0;
for (int i = 0; i < N; i++) {
if (used[i]) {
ans++;
long mul = K * (long) a[i];
if (mul <= max) {
int index = Arrays.binarySearch(a, (int) mul);
if (index >= 0)
{
used[index] = false;
}
}
}
}
if (K == 1)
ans = N;
out.println(ans);
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int K = in.nextInt();
int ans;
int[] a = new int[N];
for (int i = 0; i < N; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
long max = a[N - 1];
boolean[] used = new boolean[N];
for (int i = 0; i < N; i++) {
used[i] = true;
}
ans = 0;
for (int i = 0; i < N; i++) {
if (used[i]) {
ans++;
long mul = K * (long) a[i];
if (mul <= max) {
int index = Arrays.binarySearch(a, (int) mul);
if (index >= 0)
{
used[index] = false;
}
}
}
}
if (K == 1)
ans = N;
out.println(ans);
}
// =====================================
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();
}
}
}
Codeforces Round #165 (Div. 2) A. Fancy Fence
// Codeforces Round #165 (Div. 2) A. Fancy Fence
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int T = in.nextInt();
for (int i = 0; i < T; i++) {
int a = in.nextInt();
int x = 180 - a;
if (360 % x == 0)
out.println("YES");
else
out.println("NO");
}
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int T = in.nextInt();
for (int i = 0; i < T; i++) {
int a = in.nextInt();
int x = 180 - a;
if (360 % x == 0)
out.println("YES");
else
out.println("NO");
}
}
// =====================================
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();
}
}
}
Saturday, February 16, 2013
CodeEval Following Integer
// CodeEval Following Integer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.*;
class MainCodeEval {
public static void main(String[] args) {
next_number.main(args);
}
}
class next_number {
private static PrintStream out;
public static void main(String[] args) {
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST)
CodeEvalGetInput("e:\\zin2.txt");
else
CodeEvalGetInput(args[0]);
}
public static void CodeEvalGetInput(String arg) {
try {
File file = new File(arg);
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
// Process line of input Here
Process(line);
}
} catch (IOException e) {
System.out.println("File Read Error: " + e.getMessage());
}
}
private static void Process(String line) {
String s = line;
int N = Integer.valueOf(s);
int[] cnt = new int[10];
int m = N;
while (m > 0) {
int r = m % 10;
cnt[r] += 1;
m = m / 10;
}
int ans = 0;
for (int i = N + 1; i < 10000000; i++) {
boolean valid = true;
int[] cntAns = new int[10];
int X = i;
for (int j = 1; j <= 9; j++) {
while (X > 0) {
int r = X % 10;
cntAns[r] += 1;
X = X / 10;
}
if (cntAns[j] != cnt[j]) {
valid = false;
break;
}
}
if (valid) {
ans = i;
break;
}
}
out.println(ans);
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.*;
class MainCodeEval {
public static void main(String[] args) {
next_number.main(args);
}
}
class next_number {
private static PrintStream out;
public static void main(String[] args) {
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST)
CodeEvalGetInput("e:\\zin2.txt");
else
CodeEvalGetInput(args[0]);
}
public static void CodeEvalGetInput(String arg) {
try {
File file = new File(arg);
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
// Process line of input Here
Process(line);
}
} catch (IOException e) {
System.out.println("File Read Error: " + e.getMessage());
}
}
private static void Process(String line) {
String s = line;
int N = Integer.valueOf(s);
int[] cnt = new int[10];
int m = N;
while (m > 0) {
int r = m % 10;
cnt[r] += 1;
m = m / 10;
}
int ans = 0;
for (int i = N + 1; i < 10000000; i++) {
boolean valid = true;
int[] cntAns = new int[10];
int X = i;
for (int j = 1; j <= 9; j++) {
while (X > 0) {
int r = X % 10;
cntAns[r] += 1;
X = X / 10;
}
if (cntAns[j] != cnt[j]) {
valid = false;
break;
}
}
if (valid) {
ans = i;
break;
}
}
out.println(ans);
}
}
Codeforces Round #164 (Div. 2) A. Games
// Codeforces Round #164 (Div. 2) A. Games
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int[] h = new int[N];
int[] a = new int[N];
for (int i = 0; i < N; i++) {
h[i] = in.nextInt();
a[i] = in.nextInt();
}
int match = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j)
continue;
if (h[i] == a[j])
match++;
}
}
out.println(match);
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int[] h = new int[N];
int[] a = new int[N];
for (int i = 0; i < N; i++) {
h[i] = in.nextInt();
a[i] = in.nextInt();
}
int match = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j)
continue;
if (h[i] == a[j])
match++;
}
}
out.println(match);
}
// =====================================
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();
}
}
}
Friday, February 15, 2013
Codeforces Round #163 (Div. 2) B. Queue at the School
// Codeforces Round #163 (Div. 2) B. Queue at the School
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int T = in.nextInt();
String sq = in.nextString();
char[] q = sq.toCharArray();
for (int i = 0; i < T; i++) {
int p = 0;
while (p < q.length - 1) {
if (q[p] == 'B' && q[p + 1] == 'G') {
q[p] = 'G';
q[p + 1] = 'B';
p += 2;
}
else
p += 1;
}
}
out.println(new String(q));
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int T = in.nextInt();
String sq = in.nextString();
char[] q = sq.toCharArray();
for (int i = 0; i < T; i++) {
int p = 0;
while (p < q.length - 1) {
if (q[p] == 'B' && q[p + 1] == 'G') {
q[p] = 'G';
q[p + 1] = 'B';
p += 2;
}
else
p += 1;
}
}
out.println(new String(q));
}
// =====================================
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();
}
}
}
Codeforces Round #163 (Div. 2) A. Stones on the Table
// Codeforces Round #163 (Div. 2) A. Stones on the Table
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
String s = in.nextString();
int min = 0;
char lastc = s.charAt(0);
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == lastc) {
min++;
}
else
lastc = s.charAt(i);
}
out.println(min);
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
String s = in.nextString();
int min = 0;
char lastc = s.charAt(0);
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == lastc) {
min++;
}
else
lastc = s.charAt(i);
}
out.println(min);
}
// =====================================
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();
}
}
}
Codeforces Round #162 (Div. 2) B. Roadside Trees (Simplified Edition)
// Codeforces Round #162 (Div. 2) B. Roadside Trees (Simplified Edition)
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int[] h = new int[N];
for (int i = 0; i < h.length; i++) {
h[i] = in.nextInt();
}
long sec = 0;
for (int i = 0; i < N; i++) {
if (i > 0) {
if (h[i - 1] > h[i]) {
sec += h[i - 1] - h[i];
sec += 1;
}
else {
sec += 1;
sec += h[i] - h[i - 1];
}
}
else {
sec += h[0];
}
sec += 1;
}
out.println(sec);
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int[] h = new int[N];
for (int i = 0; i < h.length; i++) {
h[i] = in.nextInt();
}
long sec = 0;
for (int i = 0; i < N; i++) {
if (i > 0) {
if (h[i - 1] > h[i]) {
sec += h[i - 1] - h[i];
sec += 1;
}
else {
sec += 1;
sec += h[i] - h[i - 1];
}
}
else {
sec += h[0];
}
sec += 1;
}
out.println(sec);
}
// =====================================
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();
}
}
}
Codeforces Round #162 (Div. 2) A. Colorful Stones (Simplified Edition)
// Codeforces Round #162 (Div. 2) A. Colorful Stones (Simplified Edition)
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
String s = in.nextString();
String t = in.nextString();
int pos = 0;
for (int i = 0; i < t.length(); i++) {
if (t.charAt(i) == s.charAt(pos))
pos++;
}
out.println(pos + 1);
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
String s = in.nextString();
String t = in.nextString();
int pos = 0;
for (int i = 0; i < t.length(); i++) {
if (t.charAt(i) == s.charAt(pos))
pos++;
}
out.println(pos + 1);
}
// =====================================
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();
}
}
}
Codeforces Round #161 (Div. 2) B. Squares
// Codeforces Round #161 (Div. 2) B. Squares
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int K = in.nextInt();
Integer[] a = new Integer[N];
for (int i = 0; i < N; i++) {
a[i] = in.nextInt();
}
if (K > N) {
out.println(-1);
return;
}
Arrays.sort(a, Collections.reverseOrder());
int x = a[K - 1];
out.println(x + " " + x);
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int K = in.nextInt();
Integer[] a = new Integer[N];
for (int i = 0; i < N; i++) {
a[i] = in.nextInt();
}
if (K > N) {
out.println(-1);
return;
}
Arrays.sort(a, Collections.reverseOrder());
int x = a[K - 1];
out.println(x + " " + x);
}
// =====================================
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();
}
}
}
Codeforces Round #160 (Div. 2) B. Roma and Changing Signs
// Codeforces Round #160 (Div. 2) B. Roma and Changing Signs
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int K = in.nextInt();
Integer[] a = new Integer[N];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
int j = 0;
while (j < N && a[j] < 0)
{
a[j] *= -1;
j++;
K--;
if (K == 0)
break;
}
Arrays.sort(a);
if (K > 0) {
if (K % 2 == 1)
a[0] = -a[0];
}
long sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
out.println(sum);
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int K = in.nextInt();
Integer[] a = new Integer[N];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
int j = 0;
while (j < N && a[j] < 0)
{
a[j] *= -1;
j++;
K--;
if (K == 0)
break;
}
Arrays.sort(a);
if (K > 0) {
if (K % 2 == 1)
a[0] = -a[0];
}
long sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
out.println(sum);
}
// =====================================
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();
}
}
}
Codeforces Round #161 (Div. 2) A. Beautiful Matrix
// Codeforces Round #161 (Div. 2) A. Beautiful Matrix
import java.io.*;
import java.math.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
//codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int mov = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
int x = in.nextInt();
if (x == 1) {
mov = Math.abs(2 - i) + Math.abs(2 - j);
break;
}
}
}
out.println(mov);
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
//codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int mov = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
int x = in.nextInt();
if (x == 1) {
mov = Math.abs(2 - i) + Math.abs(2 - j);
break;
}
}
}
out.println(mov);
}
// =====================================
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();
}
}
}
Wednesday, February 13, 2013
Codeforces Round #160 (Div. 2) A. Roma and Lucky Numbers
// Codeforces Round #160 (Div. 2) A. Roma and Lucky Numbers
import java.io.*;
import java.math.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
//codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int K = in.nextInt();
int x;
int c = 0;
for (int i = 0; i < N; i++) {
x = in.nextInt();
int lucky = 0;
while (x > 0) {
int y = x % 10;
if (y == 4 || y == 7)
lucky++;
x = x / 10;
}
if (lucky <= K)
c++;
}
out.println(c);
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
//codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int K = in.nextInt();
int x;
int c = 0;
for (int i = 0; i < N; i++) {
x = in.nextInt();
int lucky = 0;
while (x > 0) {
int y = x % 10;
if (y == 4 || y == 7)
lucky++;
x = x / 10;
}
if (lucky <= K)
c++;
}
out.println(c);
}
// =====================================
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();
}
}
}
Wednesday, February 6, 2013
Codeforces Round #159 (Div. 2) A - Sockets
// Codeforces Round #159 (Div. 2) A - Sockets
import java.io.*;
import java.math.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
//codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int M = in.nextInt();
int K = in.nextInt();
Integer[] A = new Integer[N];
for (int i = 0; i < A.length; i++) {
A[i] = in.nextInt();
}
Arrays.sort(A, Collections.reverseOrder());
int nsockets = K;
if (nsockets >= M)
{
out.println(0);
return;
}
for (int i = 0; i < N; i++) {
nsockets += A[i] - 1;
if (nsockets >= M)
{
out.println(i + 1);
return;
}
}
out.println(-1);
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
//codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int N = in.nextInt();
int M = in.nextInt();
int K = in.nextInt();
Integer[] A = new Integer[N];
for (int i = 0; i < A.length; i++) {
A[i] = in.nextInt();
}
Arrays.sort(A, Collections.reverseOrder());
int nsockets = K;
if (nsockets >= M)
{
out.println(0);
return;
}
for (int i = 0; i < N; i++) {
nsockets += A[i] - 1;
if (nsockets >= M)
{
out.println(i + 1);
return;
}
}
out.println(-1);
}
// =====================================
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();
}
}
}
Saturday, February 2, 2013
Codeforces Round #158 (Div. 2) B - Ancient Prophesy
// Codeforces Round #158 (Div. 2) B - Ancient Prophesy
import java.io.*;
import java.math.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
//codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
HashMap<String, Integer> dict = new HashMap<>();
String s = in.nextString();
int nmax = 0;
for (int i = 0; i < s.length(); i++) {
String sub = s.substring(i, Math.min(i + 10, s.length()));
if (IsValid(sub))
{
if (!dict.containsKey(sub)) {
dict.put(sub, 1);
}
else {
dict.put(sub, dict.get(sub) + 1);
}
nmax = Math.max(nmax, dict.get(sub));
}
}
for (String key : dict.keySet()) {
if (dict.get(key) == nmax)
{
out.println(key);
return;
}
}
}
private static boolean IsValid(String sub) {
Date dt1 = new Date(2013 - 1900, 1 - 1, 1);
Date dt2 = new Date(2015 - 1900, 12 - 1, 31);
try
{
if (sub.charAt(2) != '-' || sub.charAt(5) != '-')
throw new Exception();
int d = Integer.valueOf(sub.substring(0, 2));
int m = Integer.valueOf(sub.substring(3, 5));
int y = Integer.valueOf(sub.substring(6, 10));
if (d < 1 || m < 1 || y < 1)
throw new Exception();
Date dt = new Date(y - 1900, m - 1, d);
if (IsDateValid(y, m, d) && !dt.after(dt2) && !dt.before(dt1))
return true;
} catch (Exception e)
{
}
return false;
}
public static boolean IsDateValid(int y, int m, int d)
{
if (y < 0 || m < 0 || d < 0)
return false;
int[] ndays = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (d <= ndays[m - 1])
return true;
if (y % 4 == 0 && m == 2 && d == 29)
return true;
return false;
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
//codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
HashMap<String, Integer> dict = new HashMap<>();
String s = in.nextString();
int nmax = 0;
for (int i = 0; i < s.length(); i++) {
String sub = s.substring(i, Math.min(i + 10, s.length()));
if (IsValid(sub))
{
if (!dict.containsKey(sub)) {
dict.put(sub, 1);
}
else {
dict.put(sub, dict.get(sub) + 1);
}
nmax = Math.max(nmax, dict.get(sub));
}
}
for (String key : dict.keySet()) {
if (dict.get(key) == nmax)
{
out.println(key);
return;
}
}
}
private static boolean IsValid(String sub) {
Date dt1 = new Date(2013 - 1900, 1 - 1, 1);
Date dt2 = new Date(2015 - 1900, 12 - 1, 31);
try
{
if (sub.charAt(2) != '-' || sub.charAt(5) != '-')
throw new Exception();
int d = Integer.valueOf(sub.substring(0, 2));
int m = Integer.valueOf(sub.substring(3, 5));
int y = Integer.valueOf(sub.substring(6, 10));
if (d < 1 || m < 1 || y < 1)
throw new Exception();
Date dt = new Date(y - 1900, m - 1, d);
if (IsDateValid(y, m, d) && !dt.after(dt2) && !dt.before(dt1))
return true;
} catch (Exception e)
{
}
return false;
}
public static boolean IsDateValid(int y, int m, int d)
{
if (y < 0 || m < 0 || d < 0)
return false;
int[] ndays = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (d <= ndays[m - 1])
return true;
if (y % 4 == 0 && m == 2 && d == 29)
return true;
return false;
}
// =====================================
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();
}
}
}
Codeforces Round #158 (Div. 2) A - Adding Digits
// Codeforces Round #158 (Div. 2) A - Adding Digits
import java.io.*;
import java.math.*;
import java.util.*;
//codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
boolean ok = false;
for (int j = 0; j <= 9; j++)
{
int newa = a * 10 + j;
if (newa % b == 0)
{
a = newa;
ok = true;
break;
}
}
if (!ok)
{
out.println(-1);
return;
}
StringBuffer sa = new StringBuffer(String.valueOf(a));
for (int i = 1; i < n; i++) {
sa.append("0");
}
out.println(sa);
}
// =====================================
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();
}
}
}
import java.io.*;
import java.math.*;
import java.util.*;
//codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
boolean ok = false;
for (int j = 0; j <= 9; j++)
{
int newa = a * 10 + j;
if (newa % b == 0)
{
a = newa;
ok = true;
break;
}
}
if (!ok)
{
out.println(-1);
return;
}
StringBuffer sa = new StringBuffer(String.valueOf(a));
for (int i = 1; i < n; i++) {
sa.append("0");
}
out.println(sa);
}
// =====================================
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();
}
}
}
Subscribe to:
Posts (Atom)