Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submissão do problema 01 #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions submissions/dvgba/Problema-1/Problema_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.HashMap;

public class Problema_1 {

static class Tuple {
int weight;
int profit;

Tuple(int weight, int profit) {
this.weight = weight;
this.profit = profit;
}
}

public static void main(String[] args) {
String[] files = {"Problema-1/Resources/exemplo_01.txt", "Problema-1/Resources/exemplo_02.txt", "Problema-1/Resources/exemplo_03.txt"};

for (String fileName : files) {
try {
File file = new File(fileName);
Scanner scanner = new Scanner(file);

int n = scanner.nextInt();
int s = scanner.nextInt();
Tuple[] tuples = new Tuple[n];

for (int i = 0; i < n; i++) {
int weight = scanner.nextInt();
int profit = scanner.nextInt();
tuples[i] = new Tuple(weight, profit);
}

Arrays.sort(tuples, (a, b) -> Double.compare((double) b.profit / b.weight, (double) a.profit / a.weight));
tuples = Arrays.stream(tuples).filter(tup -> tup.weight <= s).toArray(Tuple[]::new);

long startTime = System.nanoTime();
int result = calcProfit(tuples, n, s, new HashMap<>());
long endTime = System.nanoTime();

long durationInMillis = (endTime - startTime) / 1_000_000;
System.out.println("Resultado para " + fileName + ": " + result);
System.out.println("Tempo de execução de " + fileName + " " + durationInMillis + " ms");

scanner.close();
} catch (FileNotFoundException e) {
System.out.println("Arquivo não encontrado: " + fileName);
e.printStackTrace();
}
}
}

private static int calcProfit(Tuple[] tuples, int n, int s, HashMap<String, Integer> memorization) {
String key = n + "," + s;
if (memorization.containsKey(key)) {
return memorization.get(key);
}

if (n == 0 || s == 0) {
return 0;
}

if (tuples[n - 1].weight > s) {
memorization.put(key, calcProfit(tuples, n - 1, s, memorization));
} else {
int profit1 = tuples[n - 1].profit + calcProfit(tuples, n - 1, s - tuples[n - 1].weight, memorization);
int profit2 = calcProfit(tuples, n - 1, s, memorization);
memorization.put(key, Math.max(profit1, profit2));
}

return memorization.get(key);
}
}
23 changes: 23 additions & 0 deletions submissions/dvgba/Problema-1/Resources/exemplo_01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
22 36
3 86
1 80
3 71
2 39
2 53
3 47
2 46
1 32
1 69
2 89
4 58
3 86
3 54
3 53
1 64
4 82
1 46
2 48
4 53
1 68
3 94
1 87
10 changes: 10 additions & 0 deletions submissions/dvgba/Problema-1/Resources/exemplo_02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
9 10
3 44
1 78
2 64
1 49
4 69
1 87
3 78
2 45
3 84
26 changes: 26 additions & 0 deletions submissions/dvgba/Problema-1/Resources/exemplo_03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
25 45
1 38
1 99
3 78
3 60
2 69
1 32
2 100
4 59
1 67
3 80
4 44
2 96
2 48
3 46
1 74
3 65
3 32
1 30
4 87
4 90
1 40
2 71
1 86
1 98
1 99