Friday, July 26, 2013

Topcoder SRM 583 DIV 2 L1 SwappingDigits

// Topcoder SRM 583 DIV 2 L1 SwappingDigits

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

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

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

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

Topcoder SRM 582 DIV 2 L2 SpaceWarDiv2

// Topcoder SRM 582 DIV 2 L2 SpaceWarDiv2

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

//tc; rename the class name before submitting
public class SpaceWarDiv2 {
public static void main(String[] args) {
SpaceWarDiv2 obj = new SpaceWarDiv2();
System.out.println(
obj.minimalFatigue(
new int[] { 1, 8, 5, 2, 10, 8, 2 },
new int[] { 1, 7, 1, 1, 5, 3, 1 },
new int[] { 20, 20, 20, 20, 20, 20, 20 }
));
}

public int minimalFatigue(int[] magicalGirlStrength, int[] enemyStrength,
int[] enemyCount) {
HashMap<Integer, Integer> enemyCnt = new HashMap<Integer, Integer>();
for (int i = 0; i < enemyStrength.length; i++) {
int strength = enemyStrength[i];
int cnt = enemyCount[i];
if (enemyCnt.containsKey(strength))
enemyCnt.put(strength, enemyCnt.get(strength) + cnt);
else
enemyCnt.put(strength, cnt);
}
ArrayList<Integer> strengthList = new ArrayList<Integer>(enemyCnt.keySet());
Collections.sort(strengthList, Collections.reverseOrder());
int N = magicalGirlStrength.length;
Arrays.sort(magicalGirlStrength);

int[] fatigue = new int[N];
if (strengthList.get(0) > magicalGirlStrength[N - 1])
return -1;

int maxFatigue = -1;
for (int i = 0; i < strengthList.size(); i++) {
int st = strengthList.get(i);
int cnt = enemyCnt.get(st);
while (cnt > 0) {
boolean found = false;
for (int j = N - 1; j >= 0; j--) {
if (cnt == 0)
break;
if (st > magicalGirlStrength[j])
continue;
else {
if (fatigue[j] < maxFatigue || maxFatigue == -1) {
found = true;
fatigue[j]++;
cnt--;
maxFatigue = Math.max(maxFatigue, fatigue[j]);
}
}
}
if (!found && cnt > 0) {
for (int j = N - 1; j >= 0; j--) {
if (cnt == 0)
break;
if (st > magicalGirlStrength[j])
continue;
else {
if (fatigue[j] == maxFatigue || maxFatigue == -1) {
found = true;
fatigue[j]++;
cnt--;
maxFatigue = Math.max(maxFatigue, fatigue[j]);
break;
}
}
}

}
}
}
return maxFatigue;
}
}

Topcoder SRM 582 DIV 2 L1 SemiPerfectSquare

// Topcoder SRM 582 DIV 2 L1 SemiPerfectSquare

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

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

public String check(int N) {
int n = (int) Math.sqrt(N);
boolean yes = false;
for (int i = 2; i <= n; i++) {
int i2 = i * i;
for (int j = 1; j < i; j++) {
if (j * i2 == N)
yes = true;
}
}
if (yes)
return "Yes";
else
return "No";
}

}

Saturday, July 6, 2013

Codeforces Round #191 (Div. 2) B Hungry Sequence

// Codeforces Round #191 (Div. 2) B Hungry Sequence

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 x = 5000001;
StringBuilder sb = new StringBuilder("");
sb.append(String.valueOf(x));
for (int i = 1; i < n; i++) {
x += 2;
sb.append(" " + String.valueOf(x));
}
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 #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();
}
}

}

Codeforces Round #190 (Div. 2) C Ciel and Robot

// Codeforces Round #190 (Div. 2) C Ciel and Robot

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();
String s = in.nextString();
int dx = 0;
int dy = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'L')
dx--;
else if (c == 'R')
dx++;
else if (c == 'D')
dy--;
else if (c == 'U')
dy++;
}
int repx = 0;
if (dx != 0)
repx = a / dx;
int repy = 0;
if (dy != 0)
repy = b / dy;
int rep = Math.max(repx, repy) - 100;
if (rep < 0)
rep = 0;
int px = rep * dx;
int py = rep * dy;
boolean located = false;
for (int k = 0; k < 200; k++) {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'L')
px--;
else if (c == 'R')
px++;
else if (c == 'D')
py--;
else if (c == 'U')
py++;
if (px == a && py == b)
located = true;
}
}
if (a == 0 && b == 0)
located = true;
if (located)
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, July 5, 2013

Codeforces Round #190 (Div. 2) B Ciel and Flowers

// Codeforces Round #190 (Div. 2) B Ciel and Flowers

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

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

private static void solve() throws IOException
{
int r = in.nextInt();
int g = in.nextInt();
int b = in.nextInt();
int[] rgb = new int[] { r, g, b };
Arrays.sort(rgb);
int cnt = rgb[0];
for (int j = 0; j < rgb.length; j++) {
rgb[j] -= cnt;
}
if (rgb[1] % 3 == 2 && rgb[2] % 3 == 2 && cnt > 0) {
cnt -= 1;
rgb[1] += 1;
rgb[2] += 1;
}
cnt += rgb[1] / 3;
cnt += rgb[2] / 3;
out.println(cnt);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Codeforces Round #190 (Div. 2) A Ciel and Dancing

// Codeforces Round #190 (Div. 2) A Ciel and Dancing

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 k = n;
k += m - 1;
out.println(k);
for (int i = 1; i <= n; i++) {
out.println(i + " 1");
}
for (int i = 2; i <= m; i++) {
out.println("1 " + i);
}
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Wednesday, July 3, 2013

Codeforces Round #189 (Div. 2) A Magic Numbers

// Codeforces Round #189 (Div. 2) A Magic Numbers

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.nextString();
while (!s.equals("")) {
if (s.startsWith("144"))
s = s.substring(3);
else if (s.startsWith("14"))
s = s.substring(2);
else if (s.startsWith("1"))
s = s.substring(1);
else
{
out.println("NO");
return;
}
}
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();
}
}

}