problem z przekroczeniem dozwolonej wartości

0

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

mała poprawka, bo znalazłem innego buga szukajace tego :D

class Player {
	static int truckNr = 0;
	static float [] truckLoad = new float [100]; // volume in each truck
	static float [] truckWeight = new float [100]; // weight in each truck
	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; //add volume to truck
        			truckWeight[truckNr] = packages[i][0]; //add weight to truck
        			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
        	float tempMaxWeightPackage = 0f, tempMinWeightPackage = 0f; //weight 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)
        		{
        			tempMaxWeightPackage = packages[i][0]; //get package weight
        			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)
        		{
        			tempMinWeightPackage = packages[i][0]; //get package weight
        			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;
        		//change weight
        		truckWeight[tempMaxWeightTruckNr] = truckWeight[tempMaxWeightTruckNr] + tempMinWeightPackage - tempMaxWeightPackage;
        		truckWeight[tempMinWeightTruckNr] = truckWeight[tempMinWeightTruckNr] + tempMaxWeightPackage - tempMinWeightPackage;
        	}
        	
        	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;
        		truckWeight[tempMaxWeightTruckNr] = truckWeight[tempMaxWeightTruckNr] + tempMaxWeightPackage - tempMinWeightPackage;
        		truckWeight[tempMinWeightTruckNr] = truckWeight[tempMinWeightTruckNr] + tempMinWeightPackage - tempMaxWeightPackage;
        		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 = truckWeight[0];
		maxWeight = truckWeight[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(truckWeight[i] < truckWeight[i+1])
    		{
    			if(minWeight > truckWeight[i])
            	{
            		minWeight = truckWeight[i];
            		minWeightTruckNr = i;
            	}
    			
    			if(maxWeight < truckWeight[i+1])
            	{
            		maxWeight = truckWeight[i+1];
            		maxWeightTruckNr = i + 1;
            	}
    		}else
    		{
    			if(minWeight > truckWeight[i+1])
            	{
            		minWeight = truckWeight[i+1];
            		minWeightTruckNr = i + 1;
            	}
    			
    			if(maxWeight < truckWeight[i])
            	{
            		maxWeight = truckWeight[i];
            		maxWeightTruckNr = i;
            	}
    		}
        }
    }
}
0

problem był z dodawaniem floatów po zmnianie jest ok

static boolean checkLoad(int truckNr, float volumePlus, float volumeMinus)
{
int temp = (int)(truckLoad[truckNr]10e5) + (int)(volumePlus10e5) - (int)(volumeMinus*10e5);
if(temp <= 10000000)
return true;
else
return false;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1