Advertisement
Guest User

Untitled

a guest
May 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class StepikAlgorithms {
  4.     public static void main(String[] args){
  5.        
  6.         Scanner input = new Scanner(System.in);
  7.        
  8.         int thingsNumber = input.nextInt();
  9.         int bagCapacity = input.nextInt();
  10.        
  11.         Bag bag = new Bag(bagCapacity);
  12.                
  13.         ArrayList<Thing> things = new ArrayList<>();
  14.         for(int i = 0; i < thingsNumber; i++ ){
  15.             int cost = input.nextInt();
  16.             int weight = input.nextInt();
  17.             things.add(new Thing(cost, weight));
  18.         }      
  19.         Collections.sort(things, new Comparator<Thing>(){
  20.             public int compare(Thing t1, Thing t2){
  21.                 return Float.compare(t2.specificCost, t1.specificCost);
  22.             }
  23.         });
  24.                
  25.         for(Thing thing:things){
  26.             if(bag.freeCapacity > thing.weight){
  27.                 bag.freeCapacity -= thing.weight;
  28.                 bag.summaryCost += thing.cost;
  29.             }else{
  30.                 bag.summaryCost += bag.freeCapacity * thing.specificCost;
  31.                 break;
  32.             }
  33.         }
  34.         System.out.printf("%.3f",bag.summaryCost);
  35.     }
  36. }
  37.  
  38. class Thing{   
  39.     int cost;
  40.     int weight;
  41.     float specificCost;
  42.    
  43.     Thing(int cost, int weight){
  44.         this.cost = cost;
  45.         this.weight = weight;
  46.         specificCost = (float) cost / weight;
  47.     }
  48. }
  49.  
  50. class Bag{
  51.     int freeCapacity;
  52.     double summaryCost;
  53.    
  54.     Bag(int capacity){
  55.         this.freeCapacity = capacity;
  56.         summaryCost = 0;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement