(UVA) Homer Simpson - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1406
The solution below used Dynamic Programming to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public int m;
public int n;
public int t;
public Info[] array;
public Info rec(int time) throws CloneNotSupportedException {
if (time < 0) {
return new Info(0, 100000);
}
if (time == 0) {
return new Info(0, 0);
}
if (array[time] != null) {
return array[time];
}
Info eatFirst = (Info) rec(time-m).clone();
eatFirst.numBurguers += 1;
Info eatSecond = (Info) rec(time-n).clone();
eatSecond.numBurguers += 1;
Info consumeBeer = (Info) rec(time-1).clone();
consumeBeer.numBeers += 1;
Info i = eatFirst;
if (eatFirst.numBeers < eatSecond.numBeers) {
i = eatFirst;
}
else if (eatFirst.numBeers > eatSecond.numBeers) {
i = eatSecond;
}
else {
if (eatFirst.numBurguers < eatSecond.numBurguers) {
i = eatSecond;
}
else if (eatFirst.numBurguers > eatSecond.numBurguers) {
i = eatFirst;
}
}
Info i2 = i;
if (i.numBeers < consumeBeer.numBeers) {
i2 = i;
}
else if (i.numBeers > consumeBeer.numBeers) {
i2 = consumeBeer;
}
else {
if (i.numBurguers > consumeBeer.numBurguers) {
i2 = i;
}
else if (i.numBurguers < consumeBeer.numBurguers) {
i2 = consumeBeer;
}
}
array[time] = i2;
return array[time];
}
public void process() throws NumberFormatException, IOException, CloneNotSupportedException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
while (sc.hasNext()) {
m = sc.nextInt();
n = sc.nextInt();
t = sc.nextInt();
array = new Info[t+1];
Arrays.fill(array, null);
Info answer = rec(t);
String s = (answer.numBeers == 0) ? (answer.numBurguers+"\n") : (answer.numBurguers+" "+answer.numBeers+"\n");
bw.write(s);
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException, CloneNotSupportedException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Info implements Cloneable {
int numBurguers;
int numBeers;
public Info(int numBurguers, int numBeers) {
this.numBurguers = numBurguers;
this.numBeers = numBeers;
}
@Override
public Info clone() throws CloneNotSupportedException {
return (Info) super.clone();
}
}
The solution below used Dynamic Programming to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public int m;
public int n;
public int t;
public Info[] array;
public Info rec(int time) throws CloneNotSupportedException {
if (time < 0) {
return new Info(0, 100000);
}
if (time == 0) {
return new Info(0, 0);
}
if (array[time] != null) {
return array[time];
}
Info eatFirst = (Info) rec(time-m).clone();
eatFirst.numBurguers += 1;
Info eatSecond = (Info) rec(time-n).clone();
eatSecond.numBurguers += 1;
Info consumeBeer = (Info) rec(time-1).clone();
consumeBeer.numBeers += 1;
Info i = eatFirst;
if (eatFirst.numBeers < eatSecond.numBeers) {
i = eatFirst;
}
else if (eatFirst.numBeers > eatSecond.numBeers) {
i = eatSecond;
}
else {
if (eatFirst.numBurguers < eatSecond.numBurguers) {
i = eatSecond;
}
else if (eatFirst.numBurguers > eatSecond.numBurguers) {
i = eatFirst;
}
}
Info i2 = i;
if (i.numBeers < consumeBeer.numBeers) {
i2 = i;
}
else if (i.numBeers > consumeBeer.numBeers) {
i2 = consumeBeer;
}
else {
if (i.numBurguers > consumeBeer.numBurguers) {
i2 = i;
}
else if (i.numBurguers < consumeBeer.numBurguers) {
i2 = consumeBeer;
}
}
array[time] = i2;
return array[time];
}
public void process() throws NumberFormatException, IOException, CloneNotSupportedException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
while (sc.hasNext()) {
m = sc.nextInt();
n = sc.nextInt();
t = sc.nextInt();
array = new Info[t+1];
Arrays.fill(array, null);
Info answer = rec(t);
String s = (answer.numBeers == 0) ? (answer.numBurguers+"\n") : (answer.numBurguers+" "+answer.numBeers+"\n");
bw.write(s);
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException, CloneNotSupportedException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Info implements Cloneable {
int numBurguers;
int numBeers;
public Info(int numBurguers, int numBeers) {
this.numBurguers = numBurguers;
this.numBeers = numBeers;
}
@Override
public Info clone() throws CloneNotSupportedException {
return (Info) super.clone();
}
}
Comments
Post a Comment