Friday, July 26, 2013

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

No comments:

Post a Comment