Guest User

RENT

a guest
Feb 17th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5.  
  6. import static java.lang.Integer.max;
  7.  
  8. /**
  9.  * Created by bugkiller on 17/02/18.
  10.  */
  11. public class RENT {
  12.  
  13.     static Triple a[] = new Triple[10000];
  14.     static int dp[] = new int[10000];
  15.  
  16.     public static void main(String[] args) throws IOException {
  17.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  18.         for (int i = 0; i < 10000; i++) {
  19.             a[i] = new Triple();
  20.         }
  21.         int t = Integer.parseInt(br.readLine()), n;
  22.         String[] s;
  23.         while (t-- > 0) {
  24.             n = Integer.parseInt(br.readLine());
  25.             for (int i = 0; i < n; i++) {
  26.                 s = br.readLine().split("\\s");
  27.                 a[i].setValues(Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer.parseInt(s[2]));
  28.             }
  29.             System.out.println(solve(a,n));
  30.         }
  31.     }
  32.  
  33.     private static int solve(Triple[] a, int n) {
  34.         Arrays.sort(a, 0, n);
  35.         return maxProfit(a, n);
  36.     }
  37.  
  38.     private static int maxProfit(Triple[] a, int n) {
  39.         int ans;
  40.         dp[0] = a[0].profit;
  41.         ans = dp[0];
  42.         for (int i = 1; i < n; i++) {
  43.             dp[i] = a[i].profit;
  44.             for (int j = i - 1; j >= 0; j--) {
  45.                 if (a[j].end <= a[i].start) {
  46.                     dp[i] = max(dp[i], dp[j] + a[i].profit);
  47.                 }
  48.             }
  49.             ans = max(dp[i], ans);
  50.         }
  51.         return ans;
  52.     }
  53.  
  54.     static class Triple implements Comparable<Triple> {
  55.  
  56.         int start;
  57.         int end;
  58.         int profit;
  59.  
  60.         Triple() { }
  61.  
  62.         @Override
  63.         public int compareTo(Triple o) {
  64.             return Integer.compare(this.end, o.end);
  65.         }
  66.  
  67.         void setValues(int start, int end, int profit) {
  68.             this.start = start;
  69.             this.end = end;
  70.             this.profit = profit;
  71.         }
  72.  
  73.         @Override
  74.         public String toString() {
  75.             return "Tupple{" +
  76.                     "start=" + start +
  77.                     ", end=" + end +
  78.                     ", profit=" + profit +
  79.                     '}';
  80.         }
  81.     }
  82. }
Add Comment
Please, Sign In to add comment