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

}