Poniższy kod ma za zadanie wczytać wagę i objętość paczek i rozlokować je do 100 ciężarówek tak żeby waga paczek w każdej ciężarówce była możliwie do siebie zbliżona. Objetość paczki w jednej ciężarówce nie może przekroczyć 100. Na początku paczki są przydzielana po kolei i to działa, ale jak próbuje zamieniać paczki miejscami to mi przekracza ładowność ciężarówki.
class Player {
static int truckNr = 0;
static float [] truckLoad = new float [100];
static long startTime = System.nanoTime();
static float maxWeight = 0f;
static float minWeight = 0f;
static float delta = 0f;
static int minWeightTruckNr = 0, maxWeightTruckNr = 0;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int boxCount = in.nextInt();
float [][] packages = new float [boxCount][3];
Arrays.fill(truckLoad, 0);
for (int i = 0; i < boxCount; i++) {
float weight = in.nextFloat();
float volume = in.nextFloat();
packages[i][0] = weight;
packages[i][1] = volume;
packages[i][2] = (float)i; //nr of package
}
Arrays.sort(packages, Comparator.comparingDouble(a -> a[0]));
int [] answer = new int [boxCount]; //table with answers
//initial fill
for (int i = 0; i < packages.length; i++)
{
float v1 = packages[i][1];
for(int j = 0; j < 101; j++)
{
if(checkLoad(truckNr, v1))
{
answer[(int)packages[i][2]] = truckNr;
truckLoad[truckNr] += v1;
truckNr++;
if(truckNr == 100)
truckNr = 0;
break;
}
truckNr++;
if(truckNr == 100)
truckNr = 0;
}
}
changeMaxMin();
delta = maxWeight - minWeight;
//loop responsible for packages swapping
while ((System.nanoTime() - startTime) < 49900000000L) //check if time for calculation passed
{
float tempMinWeightVolume = 0f, tempMaxWeightVolume = 0f; //volume of packages from min and max weight truck
boolean maxVFound = false, minVFound = false;
int tempMinWeightTruckNr = minWeightTruckNr;
int tempMaxWeightTruckNr = maxWeightTruckNr;
int minAnswerNr = 0, maxAnswerNr = 0;
// find packages to swap
for(int i = 0; i < boxCount; i++)
{
//find package from max weight truck
if(!maxVFound && answer[i] == maxWeightTruckNr)
{
tempMaxWeightVolume = packages[i][1]; //get package volume
maxAnswerNr = (int)packages[i][2]; //get package nr
maxVFound = true;
}
//find package from min weight truck
if(!minVFound && answer[i] == minWeightTruckNr)
{
tempMinWeightVolume = packages[i][1]; //get package volume
minAnswerNr = (int)packages[i][2]; //get package nr
minVFound = true;
}
//break loop if packages found
if(minVFound && maxVFound)
break;
}
if(checkLoad(tempMaxWeightTruckNr, tempMinWeightVolume, tempMaxWeightVolume) &&
checkLoad(tempMinWeightTruckNr, tempMaxWeightVolume, tempMinWeightVolume)) //check if packages can be swapped
{
//change load of trucks
truckLoad[tempMaxWeightTruckNr] = truckLoad[tempMaxWeightTruckNr] + tempMinWeightVolume - tempMaxWeightVolume;
truckLoad[tempMinWeightTruckNr] = truckLoad[tempMinWeightTruckNr] + tempMaxWeightVolume - tempMinWeightVolume;
//change answer
answer[minAnswerNr] = tempMaxWeightTruckNr;
answer[maxAnswerNr] = tempMinWeightTruckNr;
}
changeMaxMin();
float newDelta = maxWeight - minWeight; //calculate new delta
if(newDelta < delta)
{
delta = newDelta;
}else
{
//reverse changes
truckLoad[tempMaxWeightTruckNr] = truckLoad[tempMaxWeightTruckNr] + tempMaxWeightVolume - tempMinWeightVolume;
truckLoad[tempMinWeightTruckNr] = truckLoad[tempMinWeightTruckNr] + tempMinWeightVolume - tempMaxWeightVolume;
answer[minAnswerNr] = tempMinWeightTruckNr;
answer[maxAnswerNr] = tempMaxWeightTruckNr;
changeMaxMin();
}
if(delta < 20f)
break;
}
// Write an action using System.out.println()
// To debug: System.err.println("Debug messages...");
for (int i = 0; i < answer.length; i++)
{
if(i < answer.length - 1)
System.out.print(answer[i] + " ");
else
System.out.print(answer[i] + "\n");
}
}
// check if added load exceed truck's limit
static boolean checkLoad(int truckNr, float volume1)
{
if(truckLoad[truckNr] + volume1 <= 100)
return true;
else
return false;
}
//check if swapped load exceed truck's limit
static boolean checkLoad(int truckNr, float volume1, float volume2)
{
if(truckLoad[truckNr] + volume1 - volume2 <= 100)
return true;
else
return false;
}
// find max and min weight and corresponding trucks
static void changeMaxMin()
{
minWeight = truckLoad[0];
maxWeight = truckLoad[0];
minWeightTruckNr = 0; //nr of truck that has min weight
maxWeightTruckNr = 0; //nr of truck that has max weight
for (int i = 0; i < 100; i+=2)
{
if(truckLoad[i] < truckLoad[i+1])
{
if(minWeight > truckLoad[i])
{
minWeight = truckLoad[i];
minWeightTruckNr = i;
}
if(maxWeight < truckLoad[i+1])
{
maxWeight = truckLoad[i+1];
maxWeightTruckNr = i + 1;
}
}else
{
if(minWeight > truckLoad[i+1])
{
minWeight = truckLoad[i+1];
minWeightTruckNr = i + 1;
}
if(maxWeight < truckLoad[i])
{
maxWeight = truckLoad[i];
maxWeightTruckNr = i;
}
}
}
}
}