Optimal Algorithms for Energy Markets: Allocation and Scheduling of Demand in DeregulatedEnergy Markets