Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. package ALGO_LABA_DINAMIC;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.StringTokenizer;
  9.  
  10. public class Cafe {
  11.     private static int n;
  12.     private static int[] costs;
  13.     private static long money[][];
  14.     private static ArrayList<Integer> path = new ArrayList<>();
  15.     private static long min;
  16.     private static int index;
  17.     private static final int inf = 300_002;
  18.  
  19.     public static void main(String[] args) {
  20.         FastReader reader = new FastReader();
  21.         n = reader.nextInt();
  22.         if (n == 0) {
  23.             System.out.println(0);
  24.             System.out.println(0 + " " + 0);
  25.         } else {
  26.             costs = new int[n + 1];
  27.             for (int i = 1; i <= n; i++) {
  28.                 costs[i] = reader.nextInt();
  29.             }
  30.             makeMoney();
  31.             System.out.println(min);
  32.             findPath();
  33.             System.out.println(index + " " + path.size());
  34.             Collections.reverse(path);
  35.             for (int day : path) {
  36.                 System.out.println(day);
  37.             }
  38.         }
  39.     }
  40.  
  41.     private static void findPath() {
  42.         int i = n;
  43.         int j = index;
  44.         while (i > 1) {
  45.             if (costs[i] > 100) {
  46.                 if (j > 0 && j < n) {
  47.                     if (money[i - 1][j - 1] + costs[i] <= money[i - 1][j + 1]) {
  48.                         j--;
  49.                     } else {
  50.                         path.add(i);
  51.                         j++;
  52.                     }
  53.                     i--;
  54.                     continue;
  55.                 }
  56.                 if (j == 0) {
  57.                     path.add(i);
  58.                     j++;
  59.                     i--;
  60.                     continue;
  61.                 }
  62.                 if (j == n) {
  63.                     j--;
  64.                     i--;
  65.                 }
  66.             } else {
  67.                 if (j == n) {
  68.                     i--;
  69.                 } else {
  70.                     if (money[i - 1][j] + costs[i] > money[i - 1][j + 1]) {
  71.                         path.add(i);
  72.                         j++;
  73.                     }
  74.                     i--;
  75.                 }
  76.             }
  77.         }
  78.     }
  79.  
  80.     private static void makeMoney() {
  81.         money = new long[n + 1][n + 1];
  82.         for (int i = 2; i <= n; i++) {
  83.             money[1][i] = inf;
  84.         }
  85.         if (costs[1] <= 100) {
  86.             money[1][0] = costs[1];
  87.             money[1][1] = inf;
  88.         } else {
  89.             money[1][0] = inf;
  90.             money[1][1] = costs[1];
  91.         }
  92.         for (int i = 2; i <= n; i++) {
  93.             for (int j = 0; j <= n; j++) {
  94.                 if (costs[i] > 100) {
  95.                     if (j == n) {
  96.                         money[i][n] = money[i - 1][n - 1] + costs[i];
  97.                     }
  98.                     if (j == 0) {
  99.                         money[i][0] = money[i - 1][1];
  100.                     }
  101.                     if (j > 0 && j < n) {
  102.                         money[i][j] = Math.min(money[i - 1][j - 1] + costs[i], money[i - 1][j + 1]);
  103.                     }
  104.                 } else {
  105.                     if (j == n) {
  106.                         money[i][n] = inf;
  107.                     } else {
  108.                         money[i][j] = Math.min(money[i - 1][j] + costs[i], money[i - 1][j + 1]);
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.         index = 0;
  114.         min = money[n][0];
  115.         for (int i = 1; i <= n; i++) {
  116.             if (min >= money[n][i]) {
  117.                 min = money[n][i];
  118.                 index = i;
  119.             }
  120.         }
  121.     }
  122.  
  123.     private static class FastReader {
  124.         BufferedReader br;
  125.         StringTokenizer st;
  126.  
  127.         FastReader() {
  128.             br = new BufferedReader(new InputStreamReader(System.in));
  129.         }
  130.  
  131.         String next() {
  132.             while (st == null || !st.hasMoreElements()) {
  133.                 try {
  134.                     st = new StringTokenizer(br.readLine());
  135.                 } catch (IOException e) {
  136.                     e.printStackTrace();
  137.                 }
  138.             }
  139.             return st.nextToken();
  140.         }
  141.  
  142.         int nextInt() {
  143.             return Integer.parseInt(next());
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement