chaosagent

sCTF kelvin solution

Mar 1st, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.FileNotFoundException;
  2. import java.io.FileReader;
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. class Problem implements Comparable<Problem>{
  7.     int p, d, t;
  8.     public Problem(int p, int d, int t) {
  9.         this.p = p;
  10.         this.d = d;
  11.         this.t = t;
  12.     }
  13.    
  14.     int pointsLeft(int t1) {
  15.         return p - d*(t+t1);
  16.     }
  17.    
  18.     double ratio() {
  19.         return ((double)t)/((double)d);
  20.     }
  21.    
  22.     public String toString() {
  23.         return ""+p+' '+d+' '+t;
  24.     }
  25.  
  26.     @Override
  27.     public int compareTo(Problem o) {
  28.         return new Integer(t*o.d).compareTo(o.t*d);
  29.     }
  30. }
  31.  
  32. public class Kelvin {
  33.    
  34.     // N is total number of problems
  35.     // T is bigger than the maximum time any problem can last
  36.     static int N, T = 40000;
  37.    
  38.     static Problem[] a;
  39.    
  40.     // arrays to store values for dynamic programming
  41.     static int[] dp = new int[T];
  42.     static int[] dp2 = new int[T];
  43.    
  44.     @SuppressWarnings("resource")
  45.     public static void main(String[] args) throws FileNotFoundException {
  46.         Scanner s = new Scanner(new FileReader("kelvin.txt"));
  47.         N = s.nextInt();
  48.         a = new Problem[N+1];
  49.         for(int i = 1; i <= N; i++)
  50.             a[i] = new Problem(s.nextInt(),s.nextInt(),s.nextInt());
  51.  
  52.         // add a place holder for problem at position 0
  53.         a[0] = new Problem(0, 1, 0);
  54.        
  55.         // sorted based on t/d
  56.         Arrays.sort(a);
  57.        
  58.         for(int t = 0; t < T; t++) {
  59.                 dp[t] = 0;
  60.                 dp2[t] = 0;
  61.         }
  62.         for(int i = a.length-1; i >= 1; i--) {
  63.            
  64.             // update dp values
  65.             for(int t = T-1; t >= 0; t--)
  66.                 if(a[i].pointsLeft(t) > 0)
  67.                     dp2[t] = Math.max(dp[t], dp[t+a[i].t]+a[i].pointsLeft(t));
  68.            
  69.             // shift new dp values back to first array
  70.             for(int t = 0; t < T; t++)
  71.                 dp[t] = dp2[t];
  72.         }
  73.         System.out.println(dp[0]);
  74.     }
  75. }
Add Comment
Please, Sign In to add comment