Advertisement
coder0687

Untitled

Jun 6th, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.73 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Truck {
  5.     int st,f;
  6.     public Truck(int st, int f) {
  7.         this.st=st; this.f=f;
  8.     }
  9.     int getST(){ return st; }
  10.     int getF() { return f; }
  11. }
  12. class TCmp implements Comparator<Truck> {
  13.     public int compare(Truck t1, Truck t2) {
  14.         if(t1.st<t2.st) return -1;
  15.         else if(t1.st>t2.st) return 1;
  16.         else return 0;
  17.     }
  18. }
  19. class Code
  20. {
  21.     static class Reader
  22.     {
  23.         final private int BUFFER_SIZE = 1 << 32;
  24.         private DataInputStream din;
  25.         private byte[] buffer;
  26.         private int bufferPointer, bytesRead;
  27.  
  28.         public Reader()
  29.         {
  30.             din = new DataInputStream(System.in);
  31.             buffer = new byte[BUFFER_SIZE];
  32.             bufferPointer = bytesRead = 0;
  33.         }
  34.  
  35.         public Reader(String file_name) throws IOException
  36.         {
  37.             din = new DataInputStream(new FileInputStream(file_name));
  38.             buffer = new byte[BUFFER_SIZE];
  39.             bufferPointer = bytesRead = 0;
  40.         }
  41.  
  42.         public String readLine() throws IOException
  43.         {
  44.             byte[] buf = new byte[64]; // line length
  45.             int cnt = 0, c;
  46.             while ((c = read()) != -1)
  47.             {
  48.                 if (c == '\n')
  49.                     break;
  50.                 buf[cnt++] = (byte) c;
  51.             }
  52.             return new String(buf, 0, cnt);
  53.         }
  54.  
  55.         public int nextInt() throws IOException
  56.         {
  57.             int ret = 0;
  58.             byte c = read();
  59.             while (c <= ' ')
  60.                 c = read();
  61.             boolean neg = (c == '-');
  62.             if (neg)
  63.                 c = read();
  64.             do
  65.             {
  66.                 ret = ret * 10 + c - '0';
  67.             }  while ((c = read()) >= '0' && c <= '9');
  68.  
  69.             if (neg)
  70.                 return -ret;
  71.             return ret;
  72.         }
  73.  
  74.         public long nextLong() throws IOException
  75.         {
  76.             long ret = 0;
  77.             byte c = read();
  78.             while (c <= ' ')
  79.                 c = read();
  80.             boolean neg = (c == '-');
  81.             if (neg)
  82.                 c = read();
  83.             do {
  84.                 ret = ret * 10 + c - '0';
  85.             }
  86.             while ((c = read()) >= '0' && c <= '9');
  87.             if (neg)
  88.                 return -ret;
  89.             return ret;
  90.         }
  91.  
  92.         public double nextDouble() throws IOException
  93.         {
  94.             double ret = 0, div = 1;
  95.             byte c = read();
  96.             while (c <= ' ')
  97.                 c = read();
  98.             boolean neg = (c == '-');
  99.             if (neg)
  100.                 c = read();
  101.  
  102.             do {
  103.                 ret = ret * 10 + c - '0';
  104.             }
  105.             while ((c = read()) >= '0' && c <= '9');
  106.  
  107.             if (c == '.')
  108.             {
  109.                 while ((c = read()) >= '0' && c <= '9')
  110.                 {
  111.                     ret += (c - '0') / (div *= 10);
  112.                 }
  113.             }
  114.  
  115.             if (neg)
  116.                 return -ret;
  117.             return ret;
  118.         }
  119.  
  120.         private void fillBuffer() throws IOException
  121.         {
  122.             bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  123.             if (bytesRead == -1)
  124.                 buffer[0] = -1;
  125.         }
  126.  
  127.         private byte read() throws IOException
  128.         {
  129.             if (bufferPointer == bytesRead)
  130.                 fillBuffer();
  131.             return buffer[bufferPointer++];
  132.         }
  133.  
  134.         public void close() throws IOException
  135.         {
  136.             if (din == null)
  137.                 return;
  138.             din.close();
  139.         }
  140.     }
  141.    
  142.     static void display(List<Truck> t) {
  143.         for(int i=0;i<t.size();i++) out.println(t.get(i).getST()+" "+t.get(i).getF());
  144.         out.println();
  145.     }
  146.    
  147.     static PrintStream out = new PrintStream(System.out);
  148.     public static void main(String[] args)throws IOException {
  149.         //try {
  150.             Reader sc = new Reader();
  151.             PrintWriter out = new PrintWriter(System.out);
  152.             //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  153.             int tc=sc.nextInt();//Integer.valueOf(br.readLine());
  154.             while(tc-->0) {
  155.                 int n=sc.nextInt(),stp[]=new int[n],fuel[]=new int[n];
  156.                 List<Truck> list = new ArrayList<>();
  157.                 for(int i=n-1;i>=0;i--) {
  158.                     stp[i]=sc.nextInt();
  159.                     fuel[i]=sc.nextInt();
  160.                 }
  161.                 int L=sc.nextInt(),P=sc.nextInt();
  162.                 if(P>=L) { out.println(0); continue; }
  163.                 for(int i=0;i<n;i++) list.add(new Truck(L-stp[i],fuel[i]));
  164.                 Collections.sort(list,new TCmp());
  165.                 //display(list);
  166.                 PriorityQueue<Integer> pQ=new PriorityQueue<>(Collections.reverseOrder());
  167.                 long cfl=P;int ct=0,i=0;
  168.                 boolean b=false;
  169.                 for(;cfl<L;) {
  170.                     if(list.get(i).getST()>cfl) {
  171.                         while(!pQ.isEmpty() && list.get(i).getST()>cfl) { cfl+=pQ.poll(); ct++; }
  172.                         if(list.get(i).getST()>cfl || cfl>=L) break;
  173.                     }
  174.                     while(i<n && list.get(i).getST()<=cfl) pQ.add(list.get(i++).getF());
  175.                     cfl+=pQ.poll();
  176.                     ct++;
  177.                     //out.println(cfl+" "+ct);
  178.                     //if(cfl>=L) break;
  179.                 }
  180.                 out.println((cfl<L)?-1:ct);
  181.             }
  182.             //out.println("");
  183.             out.flush();
  184.             out.close();
  185.            
  186.         //} catch(Exception e) {}
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement