Переписать программу с Java на Pascal, СРОЧНО, 50 баллов
import java.util.*;
import java.io.*;

public class sol_ik {

private final int UNIT = 1000;
private final int DISCOUNT_PER_UNIT = 500;
private final double MAX_DISCOUNT = 0.2;

private double getTotalCost(long firstCost, long secondCost,
long fullUnits) {
long couponSum = fullUnits * DISCOUNT_PER_UNIT;
double secondCostWithDiscount = secondCost
- Math.min(MAX_DISCOUNT * secondCost, couponSum);
return firstCost + secondCostWithDiscount;
}

long[] solveKnapsack(long[] weights, long totalWeight) {
int maxUnits = (int) (totalWeight / UNIT + 1);
long[] old = new long[maxUnits + 1];
Arrays.fill(old, totalWeight);
old[0] = 0;
long[] cur = new long[maxUnits + 1];
int n = weights.length;
for (int pos = 0; pos < n; pos++) {
Arrays.fill(cur, totalWeight);
for (int units = 0; units <= maxUnits; units++) {
cur[units] = Math.min(cur[units], old[units]);
int add = (int) weights[pos] / UNIT;
if (units - add >= 0) {
cur[units] = Math.min(cur[units],
old[units - add] + weights[pos]);
}
}
System.arraycopy(cur, 0, old, 0, cur.length);
}
return old;
}

public double getSolution(long[] costs) {
int n = costs.length;
long totalCost = 0;
for (int i = 0; i < n; i++) {
totalCost += costs[i];
}
long[] minForUnits = solveKnapsack(costs, totalCost);
double res = totalCost;
long maxUnits = totalCost / UNIT + 1;
for (int units = 0; units <= maxUnits; units++) {
double cur = minForUnits[units];
res = Math.min(res,
getTotalCost(minForUnits[units],
totalCost - minForUnits[units],
units));
}
return res;
}

public void solve(Scanner in, PrintWriter out) {
int n = in.nextInt();
long[] costs = new long[n];
for (int i = 0; i < n; i++) {
costs[i] = in.nextInt();
}
out.printf("%.2f%n", getSolution(costs));
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
new sol_ik().solve(in, out);
out.flush();
}
}
Please enter comments
Please enter your name.
Please enter the correct email address.
You must agree before submitting.

Answers & Comments


Copyright © 2024 SCHOLAR.TIPS - All rights reserved.