// Topcoder Single Round Match 591 Round 1 - Division II, Level One
using System;
using System.Collections.Generic;
using System.Linq;
public class TheArithmeticProgression
{
public double minimumChange ( int a, int b, int c )
{
double bb = ( a + c ) / 2.0;
double aa = 2 * b - c;
double cc = 2 * b - a;
double ans = double.MaxValue;
double r = Math.Abs ( bb - b );
ans = Math.Min ( ans, r );
r = Math.Abs ( aa - a );
ans = Math.Min ( ans, r );
r = Math.Abs ( cc - c );
ans = Math.Min ( ans, r );
return ans;
}
static void Main ( string[] args ) { }
}
Some solution examples of problems in topcoder, codeeval, google code jam, interviewstreet, onlinejudge-uva, codeforces, projecteuler, etc
Showing posts with label topcoder. Show all posts
Showing posts with label topcoder. Show all posts
Sunday, December 14, 2014
Topcoder Single Round Match 590 Round 1 - Division II, Level One
// Topcoder Single Round Match 590 Round 1 - Division II, Level One
using System;
using System.Collections.Generic;
using System.Linq;
public class FoxAndGomoku
{
public String win ( String[] board )
{
int cx = board[0].Length;
int cy = board.Length;
for ( int y = 0; y < cy; y++ )
{
for ( int x = 0; x < cx; x++ )
{
int cnt1=0;
int cnt2=0;
int cnt3=0;
int cnt4=0;
for ( int i = 0; i < 5; i++ )
{
if ( ( y + i ) >= 0 && ( y + i ) < cy )
if ( board[y + i][x] == 'o' )
cnt1++;
if ( ( x + i ) >= 0 && ( x + i ) < cx )
if ( board[y][x + i] == 'o' )
cnt2++;
if ( ( ( x + i ) >= 0 && ( x + i ) < cx ) && ( ( y
+ i ) >= 0 && ( y + i ) < cy ) )
if ( board[y + i][x + i] == 'o' )
cnt3++;
if ( ( ( x - i ) >= 0 && ( x - i ) < cx ) && ( ( y
+ i ) >= 0 && ( y + i ) < cy ) )
if ( board[y + i][x - i] == 'o' )
cnt4++;
}
if ( cnt1 >= 5 || cnt2 >= 5 || cnt3 >= 5 || cnt4 >= 5 )
return "found";
}
}
return "not found";
}
static void Main ( string[] args ) { }
}
using System;
using System.Collections.Generic;
using System.Linq;
public class FoxAndGomoku
{
public String win ( String[] board )
{
int cx = board[0].Length;
int cy = board.Length;
for ( int y = 0; y < cy; y++ )
{
for ( int x = 0; x < cx; x++ )
{
int cnt1=0;
int cnt2=0;
int cnt3=0;
int cnt4=0;
for ( int i = 0; i < 5; i++ )
{
if ( ( y + i ) >= 0 && ( y + i ) < cy )
if ( board[y + i][x] == 'o' )
cnt1++;
if ( ( x + i ) >= 0 && ( x + i ) < cx )
if ( board[y][x + i] == 'o' )
cnt2++;
if ( ( ( x + i ) >= 0 && ( x + i ) < cx ) && ( ( y
+ i ) >= 0 && ( y + i ) < cy ) )
if ( board[y + i][x + i] == 'o' )
cnt3++;
if ( ( ( x - i ) >= 0 && ( x - i ) < cx ) && ( ( y
+ i ) >= 0 && ( y + i ) < cy ) )
if ( board[y + i][x - i] == 'o' )
cnt4++;
}
if ( cnt1 >= 5 || cnt2 >= 5 || cnt3 >= 5 || cnt4 >= 5 )
return "found";
}
}
return "not found";
}
static void Main ( string[] args ) { }
}
Saturday, April 19, 2014
TCO 2014 Round 1A Div 1 L2 EllysScrabble
// TCO 2014 Round 1A Div 1 L2 EllysScrabble
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class EllysScrabble {
public static void main(String[] args) {
EllysScrabble obj = new EllysScrabble();
System.out.println(
obj.getMin(
"TOPCODER"
, 3
));
}
public String getMin(String letters, int maxDistance) {
char[] c = letters.toCharArray();
char[] dest = new char[c.length];
for (int i = 0; i < dest.length; i++) {
dest[i] = ' ';
}
boolean[] moved = new boolean[c.length];
for (int i = 0; i < c.length; i++) {
char minChar = c[i];
if (moved[i])
minChar = Character.MAX_VALUE;
int idxMinChar = i;
for (int j = maxDistance; j >= -maxDistance; j--) {
if (i + j < 0)
continue;
if (i + j >= c.length)
continue;
if (moved[i + j])
continue;
if (j == -maxDistance && !moved[i + j]) {
idxMinChar = i + j;
break;
}
char cj = c[i + j];
if (cj <= minChar || (cj == minChar && j < 0)) {
minChar = cj;
idxMinChar = i + j;
}
}
if (idxMinChar != i) {
dest[i] = c[idxMinChar];
moved[idxMinChar] = true;
}
else {
dest[i] = c[i];
moved[i] = true;
}
c[i] = c[i];
}
return new String(dest);
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class EllysScrabble {
public static void main(String[] args) {
EllysScrabble obj = new EllysScrabble();
System.out.println(
obj.getMin(
"TOPCODER"
, 3
));
}
public String getMin(String letters, int maxDistance) {
char[] c = letters.toCharArray();
char[] dest = new char[c.length];
for (int i = 0; i < dest.length; i++) {
dest[i] = ' ';
}
boolean[] moved = new boolean[c.length];
for (int i = 0; i < c.length; i++) {
char minChar = c[i];
if (moved[i])
minChar = Character.MAX_VALUE;
int idxMinChar = i;
for (int j = maxDistance; j >= -maxDistance; j--) {
if (i + j < 0)
continue;
if (i + j >= c.length)
continue;
if (moved[i + j])
continue;
if (j == -maxDistance && !moved[i + j]) {
idxMinChar = i + j;
break;
}
char cj = c[i + j];
if (cj <= minChar || (cj == minChar && j < 0)) {
minChar = cj;
idxMinChar = i + j;
}
}
if (idxMinChar != i) {
dest[i] = c[idxMinChar];
moved[idxMinChar] = true;
}
else {
dest[i] = c[i];
moved[i] = true;
}
c[i] = c[i];
}
return new String(dest);
}
}
TCO 2014 Round 1A Div 1 L1 EllysSortingTrimmer
// TCO 2014 Round 1A Div 1 L1 EllysSortingTrimmer
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class EllysSortingTrimmer {
public static void main(String[] args) {
EllysSortingTrimmer obj = new EllysSortingTrimmer();
System.out.println(
obj.getMin(
"TOPCODER"
, 3
));
}
public String getMin(String S, int L) {
while (true) {
String left = S.substring(0, S.length() - L);
String right = S.substring(S.length() - L);
char[] r = right.toCharArray();
Arrays.sort(r);
right = new String(r);
if (left.length() == 0)
return right;
else
S = left + right.substring(0, right.length() - 1);
}
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class EllysSortingTrimmer {
public static void main(String[] args) {
EllysSortingTrimmer obj = new EllysSortingTrimmer();
System.out.println(
obj.getMin(
"TOPCODER"
, 3
));
}
public String getMin(String S, int L) {
while (true) {
String left = S.substring(0, S.length() - L);
String right = S.substring(S.length() - L);
char[] r = right.toCharArray();
Arrays.sort(r);
right = new String(r);
if (left.length() == 0)
return right;
else
S = left + right.substring(0, right.length() - 1);
}
}
}
Sunday, March 30, 2014
Topcoder SRM 612 DIV 2 L2 EmoticonsDiv2
// Topcoder SRM 612 DIV 2 L2 EmoticonsDiv2
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class EmoticonsDiv2 {
public static void main(String[] args) {
EmoticonsDiv2 obj = new EmoticonsDiv2();
System.out
.println(
obj.printSmiles(
1000
));
}
public int printSmiles(int smiles) {
int[][] dp = new int[1001][1001];
int[] minSecs = new int[1001];
for (int i = 0; i < 1001; i++) {
minSecs[i] = Integer.MAX_VALUE;
}
for (int i = 0; i < 1001; i++) {
for (int j = 0; j < 1001; j++) {
dp[i][j] = -1;
}
}
dp[1][2] = 2;
minSecs[1] = 2;
for (int j = 2; j < 1001; j++) {
dp[1][j] = j;
minSecs[j] = j;
}
for (int i = 2; i < 1001; i++) {
for (int j = i; j < 1001; j += i) {
if (j == i) {
dp[i][j] = minSecs[j] + 1;
}
else {
dp[i][j] = dp[i][j - i] + 1;
}
minSecs[j] = Math.min(minSecs[j], dp[i][j]);
}
}
return minSecs[smiles];
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class EmoticonsDiv2 {
public static void main(String[] args) {
EmoticonsDiv2 obj = new EmoticonsDiv2();
System.out
.println(
obj.printSmiles(
1000
));
}
public int printSmiles(int smiles) {
int[][] dp = new int[1001][1001];
int[] minSecs = new int[1001];
for (int i = 0; i < 1001; i++) {
minSecs[i] = Integer.MAX_VALUE;
}
for (int i = 0; i < 1001; i++) {
for (int j = 0; j < 1001; j++) {
dp[i][j] = -1;
}
}
dp[1][2] = 2;
minSecs[1] = 2;
for (int j = 2; j < 1001; j++) {
dp[1][j] = j;
minSecs[j] = j;
}
for (int i = 2; i < 1001; i++) {
for (int j = i; j < 1001; j += i) {
if (j == i) {
dp[i][j] = minSecs[j] + 1;
}
else {
dp[i][j] = dp[i][j - i] + 1;
}
minSecs[j] = Math.min(minSecs[j], dp[i][j]);
}
}
return minSecs[smiles];
}
}
Thursday, March 27, 2014
Topcoder SRM 612 DIV 2 L1 LeftAndRightHandedDiv2
// Topcoder SRM 612 DIV 2 L1 LeftAndRightHandedDiv2
import java.util.*;
import java.math.*;
public class LeftAndRightHandedDiv2 {
public static void main(String[] args) {
LeftAndRightHandedDiv2 obj = new LeftAndRightHandedDiv2();
System.out
.println(
obj.count(
"LR"
));
}
public int count(String S) {
int c = 0;
for (int i = 1; i < S.length(); i++) {
if (S.charAt(i) == 'L' && S.charAt(i - 1) == 'R')
c++;
}
return c;
}
}
import java.util.*;
import java.math.*;
public class LeftAndRightHandedDiv2 {
public static void main(String[] args) {
LeftAndRightHandedDiv2 obj = new LeftAndRightHandedDiv2();
System.out
.println(
obj.count(
"LR"
));
}
public int count(String S) {
int c = 0;
for (int i = 1; i < S.length(); i++) {
if (S.charAt(i) == 'L' && S.charAt(i - 1) == 'R')
c++;
}
return c;
}
}
Friday, March 21, 2014
Topcoder SRM 613 DIV 2 L1 TaroString
// Topcoder SRM 613 DIV 2 L1 TaroString
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class TaroString {
public static void main(String[] args) {
TaroString obj = new TaroString();
System.out.println(
obj.getAnswer(
""
));
}
public String getAnswer(String S) {
String x = "";
for (int i = 0; i < S.length(); i++) {
if (S.charAt(i) == 'C')
x += "C";
if (S.charAt(i) == 'A')
x += "A";
if (S.charAt(i) == 'T')
x += "T";
}
if (x.equals("CAT"))
return "Possible";
else
return "Impossible";
}
public static int GetPrice(char c) {
int price;
if (c >= '0' && c <= '9')
price = c - '0';
else if (c >= 'A' && c <= 'Z')
price = 10 + c - 'A';
else
price = 36 + c - 'a';
return price;
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class TaroString {
public static void main(String[] args) {
TaroString obj = new TaroString();
System.out.println(
obj.getAnswer(
""
));
}
public String getAnswer(String S) {
String x = "";
for (int i = 0; i < S.length(); i++) {
if (S.charAt(i) == 'C')
x += "C";
if (S.charAt(i) == 'A')
x += "A";
if (S.charAt(i) == 'T')
x += "T";
}
if (x.equals("CAT"))
return "Possible";
else
return "Impossible";
}
public static int GetPrice(char c) {
int price;
if (c >= '0' && c <= '9')
price = c - '0';
else if (c >= 'A' && c <= 'Z')
price = 10 + c - 'A';
else
price = 36 + c - 'a';
return price;
}
}
Topcoder SRM 512 DIV 2 L2 MysteriousRestaurant
// Topcoder SRM 512 DIV 2 L2 MysteriousRestaurant
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class MysteriousRestaurant {
public static void main(String[] args) {
MysteriousRestaurant obj = new MysteriousRestaurant();
System.out.println(
obj.maxDays(
new String[]
{}, 0
));
}
public int maxDays(String[] prices, int budget) {
int days = 0;
int[] minps = new int[7];
for (int i = 0; i < 7; i++) {
if (i == prices.length)
break;
int minp = Integer.MAX_VALUE;
for (int j = 0; j < prices[0].length(); j++) {
char c = prices[i].charAt(j);
int price = GetPrice(c);
minp = Math.min(minp, price);
}
minps[i] = minp;
if (budget >= minp) {
budget -= minp;
days++;
}
else
return days;
}
for (int i = 7; i < prices.length; i++) {
int minp = Integer.MAX_VALUE;
for (int j = 0; j < prices[0].length(); j++) {
int price = 0;
for (int k = i % 7; k <= i; k += 7) {
char c1 = prices[k].charAt(j);
price += GetPrice(c1);
}
if (price < minp) {
minp = price;
}
}
if (budget + minps[i % 7] - minp >= 0) {
days++;
budget = budget - minp + minps[i % 7];
minps[i % 7] = minp;
}
else
break;
}
return days;
}
public static int GetPrice(char c) {
int price;
if (c >= '0' && c <= '9')
price = c - '0';
else if (c >= 'A' && c <= 'Z')
price = 10 + c - 'A';
else
price = 36 + c - 'a';
return price;
}
}
Friday, September 20, 2013
Topcoder SRM 589 DIV 2 L1 GooseTattarrattatDiv2
// Topcoder SRM 589 DIV 2 L1 GooseTattarrattatDiv2
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class GooseTattarrattatDiv2 {
public static void main(String[] args) {
GooseTattarrattatDiv2 obj = new GooseTattarrattatDiv2();
System.out.println(
obj.getmin(
""
));
}
public int getmin(String S) {
int[] cnt = new int[26];
for (int i = 0; i < S.length(); i++) {
int c = S.charAt(i) - 'a';
cnt[c] += 1;
}
int maxCnt = 0;
for (int i = 0; i < cnt.length; i++) {
maxCnt = Math.max(maxCnt, cnt[i]);
}
return S.length() - maxCnt;
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class GooseTattarrattatDiv2 {
public static void main(String[] args) {
GooseTattarrattatDiv2 obj = new GooseTattarrattatDiv2();
System.out.println(
obj.getmin(
""
));
}
public int getmin(String S) {
int[] cnt = new int[26];
for (int i = 0; i < S.length(); i++) {
int c = S.charAt(i) - 'a';
cnt[c] += 1;
}
int maxCnt = 0;
for (int i = 0; i < cnt.length; i++) {
maxCnt = Math.max(maxCnt, cnt[i]);
}
return S.length() - maxCnt;
}
}
Thursday, September 19, 2013
Topcoder SRM 588 DIV 2 L1 KeyDungeonDiv2
// Topcoder SRM 588 DIV 2 L1 KeyDungeonDiv2
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class KeyDungeonDiv2 {
public static void main(String[] args) {
KeyDungeonDiv2 obj = new KeyDungeonDiv2();
System.out.println(
obj.countDoors(
null, null, null
));
}
public int countDoors(int[] doorR, int[] doorG, int[] keys) {
int n = doorR.length;
int cnt = 0;
for (int i = 0; i < n; i++) {
int kwhite = keys[2];
if (keys[0] < doorR[i]) {
kwhite -= doorR[i] - keys[0];
}
if (keys[1] < doorG[i]) {
kwhite -= doorG[i] - keys[1];
}
if (kwhite >= 0)
cnt++;
}
return cnt;
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class KeyDungeonDiv2 {
public static void main(String[] args) {
KeyDungeonDiv2 obj = new KeyDungeonDiv2();
System.out.println(
obj.countDoors(
null, null, null
));
}
public int countDoors(int[] doorR, int[] doorG, int[] keys) {
int n = doorR.length;
int cnt = 0;
for (int i = 0; i < n; i++) {
int kwhite = keys[2];
if (keys[0] < doorR[i]) {
kwhite -= doorR[i] - keys[0];
}
if (keys[1] < doorG[i]) {
kwhite -= doorG[i] - keys[1];
}
if (kwhite >= 0)
cnt++;
}
return cnt;
}
}
Monday, September 16, 2013
Topcoder SRM 590 DIV 2 L1 FoxAndGomoku
// Topcoder SRM 590 DIV 2 L1 FoxAndGomoku
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class FoxAndGomoku {
public static void main(String[] args) {
FoxAndGomoku obj = new FoxAndGomoku();
System.out.println(
obj.win(
null
));
}
public String win(String[] board) {
int n = board.length;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
// check hor
if (x + 5 <= n) {
int no = 0;
for (int i = x; i < x + 5; i++) {
if (board[y].charAt(i) == 'o')
no++;
}
if (no == 5)
return "found";
}
// check ver
if (y + 5 <= n) {
int no = 0;
for (int i = y; i < y + 5; i++) {
if (board[i].charAt(x) == 'o')
no++;
}
if (no == 5)
return "found";
}
// check right
if (x + 5 <= n && y + 5 <= n) {
int no = 0;
for (int i = 0; i < 5; i++) {
if (board[x + i].charAt(y + i) == 'o')
no++;
}
if (no == 5)
return "found";
}
// check left
if (x + 5 <= n && y + 5 <= n) {
int no = 0;
for (int i = 0; i < 5; i++) {
if (board[x + i].charAt(y + 4 - i) == 'o')
no++;
}
if (no == 5)
return "found";
}
}
}
return "not found";
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class FoxAndGomoku {
public static void main(String[] args) {
FoxAndGomoku obj = new FoxAndGomoku();
System.out.println(
obj.win(
null
));
}
public String win(String[] board) {
int n = board.length;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
// check hor
if (x + 5 <= n) {
int no = 0;
for (int i = x; i < x + 5; i++) {
if (board[y].charAt(i) == 'o')
no++;
}
if (no == 5)
return "found";
}
// check ver
if (y + 5 <= n) {
int no = 0;
for (int i = y; i < y + 5; i++) {
if (board[i].charAt(x) == 'o')
no++;
}
if (no == 5)
return "found";
}
// check right
if (x + 5 <= n && y + 5 <= n) {
int no = 0;
for (int i = 0; i < 5; i++) {
if (board[x + i].charAt(y + i) == 'o')
no++;
}
if (no == 5)
return "found";
}
// check left
if (x + 5 <= n && y + 5 <= n) {
int no = 0;
for (int i = 0; i < 5; i++) {
if (board[x + i].charAt(y + 4 - i) == 'o')
no++;
}
if (no == 5)
return "found";
}
}
}
return "not found";
}
}
Sunday, September 15, 2013
Topcoder SRM 587 DIV 2 L1 InsertZ
// Topcoder SRM 587 DIV 2 L1 InsertZ
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class InsertZ {
public static void main(String[] args) {
InsertZ obj = new InsertZ();
System.out.println(
obj.canTransform(
"", ""
));
}
public String canTransform(String init, String goal) {
String s = goal.replaceAll("z", "");
if (s.equals(init))
return "Yes";
return "No";
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class InsertZ {
public static void main(String[] args) {
InsertZ obj = new InsertZ();
System.out.println(
obj.canTransform(
"", ""
));
}
public String canTransform(String init, String goal) {
String s = goal.replaceAll("z", "");
if (s.equals(init))
return "Yes";
return "No";
}
}
Topcoder SRM 586 DIV 2 L1 TeamsSelection
// Topcoder SRM 586 DIV 2 L1 TeamsSelection
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class TeamsSelection {
public static void main(String[] args) {
TeamsSelection obj = new TeamsSelection();
System.out.println(
obj.simulate(
null,
null
));
}
public String simulate(int[] preference1, int[] preference2) {
String result = "";
HashSet<Integer> selected = new HashSet<Integer>();
HashSet<Integer> selectedBy1 = new HashSet<Integer>();
int N = preference1.length;
for (int i = 0; i < N; i++) {
for (int j1 = 0; j1 < N; j1++) {
if (!selected.contains(preference1[j1])) {
selected.add(preference1[j1]);
selectedBy1.add(preference1[j1]);
break;
}
}
for (int j2 = 0; j2 < N; j2++) {
if (!selected.contains(preference2[j2])) {
selected.add(preference2[j2]);
break;
}
}
}
for (int i = 1; i <= N; i++) {
if (selectedBy1.contains(i))
result += "1";
else
result += "2";
}
return result;
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class TeamsSelection {
public static void main(String[] args) {
TeamsSelection obj = new TeamsSelection();
System.out.println(
obj.simulate(
null,
null
));
}
public String simulate(int[] preference1, int[] preference2) {
String result = "";
HashSet<Integer> selected = new HashSet<Integer>();
HashSet<Integer> selectedBy1 = new HashSet<Integer>();
int N = preference1.length;
for (int i = 0; i < N; i++) {
for (int j1 = 0; j1 < N; j1++) {
if (!selected.contains(preference1[j1])) {
selected.add(preference1[j1]);
selectedBy1.add(preference1[j1]);
break;
}
}
for (int j2 = 0; j2 < N; j2++) {
if (!selected.contains(preference2[j2])) {
selected.add(preference2[j2]);
break;
}
}
}
for (int i = 1; i <= N; i++) {
if (selectedBy1.contains(i))
result += "1";
else
result += "2";
}
return result;
}
}
Saturday, September 14, 2013
Topcoder SRM 585 DIV 2 L1 LISNumberDivTwo
// Topcoder SRM 585 DIV 2 L1 LISNumberDivTwo
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class LISNumberDivTwo {
public static void main(String[] args) {
LISNumberDivTwo obj = new LISNumberDivTwo();
System.out.println(
obj.calculate(
null
));
}
public int calculate(int[] seq) {
int cnt = 1;
for (int i = 1; i < seq.length; i++) {
if (seq[i] <= seq[i - 1])
cnt++;
}
return cnt;
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class LISNumberDivTwo {
public static void main(String[] args) {
LISNumberDivTwo obj = new LISNumberDivTwo();
System.out.println(
obj.calculate(
null
));
}
public int calculate(int[] seq) {
int cnt = 1;
for (int i = 1; i < seq.length; i++) {
if (seq[i] <= seq[i - 1])
cnt++;
}
return cnt;
}
}
Sunday, August 4, 2013
Topcoder SRM 584 DIV 2 L1 TopFox
// Topcoder SRM 584 DIV 2 L1 TopFox
import java.util.*;
//rename the class name before submitting
public class TopFox {
public static void main(String[] args) {
TopFox obj = new TopFox();
System.out.println(obj.possibleHandles(
"ab",
"cd"
));
}
public int possibleHandles(String familyName, String givenName) {
HashSet<String> names = new HashSet<String>();
for (int i = 1; i <= familyName.length(); i++) {
String s1 = familyName.substring(0, i);
for (int j = 1; j <= givenName.length(); j++) {
String s2 = givenName.substring(0, j);
String s = s1 + s2;
names.add(s);
}
}
return names.size();
}
}
import java.util.*;
//rename the class name before submitting
public class TopFox {
public static void main(String[] args) {
TopFox obj = new TopFox();
System.out.println(obj.possibleHandles(
"ab",
"cd"
));
}
public int possibleHandles(String familyName, String givenName) {
HashSet<String> names = new HashSet<String>();
for (int i = 1; i <= familyName.length(); i++) {
String s1 = familyName.substring(0, i);
for (int j = 1; j <= givenName.length(); j++) {
String s2 = givenName.substring(0, j);
String s = s1 + s2;
names.add(s);
}
}
return names.size();
}
}
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);
}
}
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;
}
}
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";
}
}
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";
}
}
Friday, June 28, 2013
Topcoder SRM 581 DIV 2 L1 BlackAndWhiteSolitaire
// Topcoder SRM 581 DIV 2 L1 BlackAndWhiteSolitaire
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class BlackAndWhiteSolitaire {
public static void main(String[] args) {
BlackAndWhiteSolitaire obj = new BlackAndWhiteSolitaire();
System.out.println(
obj.minimumTurns(
"BW"
));
}
public int minimumTurns(String cardFront) {
int nbfirst = 0;
int nwfirst = 0;
for (int i = 0; i < cardFront.length(); i++) {
if (i % 2 == 0) {
if (cardFront.charAt(i) == 'B')
nwfirst++;
else
nbfirst++;
}
else {
if (cardFront.charAt(i) == 'B')
nbfirst++;
else
nwfirst++;
}
}
return Math.min(nwfirst, nbfirst);
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class BlackAndWhiteSolitaire {
public static void main(String[] args) {
BlackAndWhiteSolitaire obj = new BlackAndWhiteSolitaire();
System.out.println(
obj.minimumTurns(
"BW"
));
}
public int minimumTurns(String cardFront) {
int nbfirst = 0;
int nwfirst = 0;
for (int i = 0; i < cardFront.length(); i++) {
if (i % 2 == 0) {
if (cardFront.charAt(i) == 'B')
nwfirst++;
else
nbfirst++;
}
else {
if (cardFront.charAt(i) == 'B')
nbfirst++;
else
nwfirst++;
}
}
return Math.min(nwfirst, nbfirst);
}
}
Monday, June 24, 2013
Topcoder SRM 580 DIV 2 L2 EelAndRabbit
// Topcoder SRM 580 DIV 2 L2 EelAndRabbit
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class EelAndRabbit {
public static void main(String[] args) {
EelAndRabbit obj = new EelAndRabbit();
System.out.println(
obj.getmax(
new int[] { 2, 4, 3, 2, 2, 1, 10 },
new int[] { 2, 6, 3, 7, 0, 2, 0 }
));
}
public int getmax(int[] l, int[] t) {
HashSet<Integer> times = new HashSet<Integer>();
for (int i = 0; i < t.length; i++) {
times.add(t[i]);
times.add(t[i] + l[i]);
}
ArrayList<Integer> T = new ArrayList<Integer>(times);
Collections.sort(T);
int maxEels = 0;
for (int i = 0; i < T.size(); i++) {
int t1 = T.get(i);
for (int j = i + 1; j < T.size(); j++) {
int t2 = T.get(j);
int numEels = 0;
for (int k = 0; k < l.length; k++) {
if (t1 >= t[k] && t1 <= t[k] + l[k]) {
numEels++;
}
else if (t2 >= t[k] && t2 <= t[k] + l[k]) {
numEels++;
}
}
maxEels = Math.max(maxEels, numEels);
}
}
return maxEels;
}
}
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class EelAndRabbit {
public static void main(String[] args) {
EelAndRabbit obj = new EelAndRabbit();
System.out.println(
obj.getmax(
new int[] { 2, 4, 3, 2, 2, 1, 10 },
new int[] { 2, 6, 3, 7, 0, 2, 0 }
));
}
public int getmax(int[] l, int[] t) {
HashSet<Integer> times = new HashSet<Integer>();
for (int i = 0; i < t.length; i++) {
times.add(t[i]);
times.add(t[i] + l[i]);
}
ArrayList<Integer> T = new ArrayList<Integer>(times);
Collections.sort(T);
int maxEels = 0;
for (int i = 0; i < T.size(); i++) {
int t1 = T.get(i);
for (int j = i + 1; j < T.size(); j++) {
int t2 = T.get(j);
int numEels = 0;
for (int k = 0; k < l.length; k++) {
if (t1 >= t[k] && t1 <= t[k] + l[k]) {
numEels++;
}
else if (t2 >= t[k] && t2 <= t[k] + l[k]) {
numEels++;
}
}
maxEels = Math.max(maxEels, numEels);
}
}
return maxEels;
}
}
Subscribe to:
Posts (Atom)