Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. package ru.itmo.degtiarenko;
  2.  
  3. import java.io.PrintWriter;
  4. import java.math.BigDecimal;
  5. import java.math.RoundingMode;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Scanner;
  9. import java.util.function.Function;
  10.  
  11. public class HTask {
  12.     public static void main(String[] args) {
  13.         Scanner in = new Scanner(System.in);
  14.         PrintWriter out = new PrintWriter(System.out);
  15.  
  16.         Integer dim = in.nextInt();
  17.         List<Double> minimumPoint = new ArrayList<>();
  18.         for (int i = 0; i < dim; i++) {
  19.             minimumPoint.add(findDimMinimumPoint(i, dim, in, out));
  20.         }
  21.         Double minValue = askFunctionValue(minimumPoint, in, out);
  22.         System.out.println(String.format("minimum %f", minValue));
  23.         in.close();
  24.         out.close();
  25.     }
  26.  
  27.     private static Double askFunctionValue(List<Double> point, Scanner in, PrintWriter out) {
  28.         StringBuilder builder = new StringBuilder();
  29.         for (Double val : point) {
  30.             final double roundedVal = new BigDecimal(val)
  31.                 .setScale(10, RoundingMode.HALF_UP)
  32.                 .doubleValue();
  33.             builder.append(roundedVal);
  34.             builder.append(' ');
  35.         }
  36.         builder.append('\n');
  37.         out.write(builder.toString());
  38.         out.flush();
  39.         return in.nextDouble();
  40.     }
  41.  
  42.     private static Double findDimMinimumPoint(int dimension, int dimensionAmount,
  43.         Scanner in, PrintWriter out)
  44.     {
  45.         List<Double> point = new ArrayList<>(dimensionAmount);
  46.         Function<Double, Double> function = val -> {
  47.             point.set(dimension, val);
  48.             return askFunctionValue(point, in, out);
  49.         };
  50.         List<Double> possibleMins = new ArrayList<>();
  51.         for (double s = 0.0; s < 1.0; s += 0.025) {
  52.             point.clear();
  53.             for (int i = 0; i < dimensionAmount; i++) {
  54.                 point.add(0.0);
  55.             }
  56.             possibleMins.add(findMinPointOnInterval(function, s, s + 0.025, 10e-6));
  57.         }
  58.         return possibleMins
  59.                 .stream()
  60.                 .mapToDouble(d -> d)
  61.                 .min()
  62.                 .getAsDouble();
  63.     }
  64.  
  65.     private static double findMinPointOnInterval(Function<Double, Double> f, double left,
  66.         double right, double eps)
  67.     {
  68.         while (right - left > eps) {
  69.             double a = (left * 2 + right) / 3;
  70.             double b = (left + right * 2) / 3;
  71.             if (f.apply(a) < f.apply(b)) {
  72.                 right = b;
  73.             } else {
  74.                 left = a;
  75.             }
  76.         }
  77.         return (left + right) / 2;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement