(SPOJ) Telemarketing - Solution 1

Link to the problem: http://br.spoj.com/problems/TELEMAR7/

The solution below tries to relate a call to each seller whenever the seller is available. If there is no seller available, the time of the calls is decreased until a seller becomes available, and until there is no more calls to be done.


import java.io.*;
import java.util.*;

class Main {
    public void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
       
        int numSellers = sc.nextInt();
        int numCalls = sc.nextInt();
        int numAvailableSellers = numSellers;
        int[] sellers = new int[numSellers]; // keep the time of the current call
        int[] sellersCalls = new int[numSellers]; // keep how many calls each seller did
        for (int i = 0; i < numCalls; i++) {
            for (int j = 0; j < numSellers; j++) { // relate a call to an available seller
                if (sellers[j] != 0) {
                    continue;
                }   
                numAvailableSellers--;
                sellersCalls[j]++;
                sellers[j] = sc.nextInt();
                break;
            }
            while (numAvailableSellers == 0) { // make a seller available
                for (int j = 0; j < numSellers; j++) {
                    sellers[j]--;
                    if (sellers[j] == 0) {
                        numAvailableSellers++;
                    }
                }
            }
        }
       
        for (int i = 0; i < numSellers; i++) {
            System.out.println((i+1) + " " + sellersCalls[i]);
        }
                          
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}


Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução