Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class Truck {
- int st,f;
- public Truck(int st, int f) {
- this.st=st; this.f=f;
- }
- int getST(){ return st; }
- int getF() { return f; }
- }
- class TCmp implements Comparator<Truck> {
- public int compare(Truck t1, Truck t2) {
- if(t1.st<t2.st) return -1;
- else if(t1.st>t2.st) return 1;
- else return 0;
- }
- }
- class Code
- {
- static class Reader
- {
- final private int BUFFER_SIZE = 1 << 32;
- private DataInputStream din;
- private byte[] buffer;
- private int bufferPointer, bytesRead;
- public Reader()
- {
- din = new DataInputStream(System.in);
- buffer = new byte[BUFFER_SIZE];
- bufferPointer = bytesRead = 0;
- }
- public Reader(String file_name) throws IOException
- {
- din = new DataInputStream(new FileInputStream(file_name));
- buffer = new byte[BUFFER_SIZE];
- bufferPointer = bytesRead = 0;
- }
- public String readLine() throws IOException
- {
- byte[] buf = new byte[64]; // line length
- int cnt = 0, c;
- while ((c = read()) != -1)
- {
- if (c == '\n')
- break;
- buf[cnt++] = (byte) c;
- }
- return new String(buf, 0, cnt);
- }
- public int nextInt() throws IOException
- {
- int ret = 0;
- byte c = read();
- while (c <= ' ')
- c = read();
- boolean neg = (c == '-');
- if (neg)
- c = read();
- do
- {
- ret = ret * 10 + c - '0';
- } while ((c = read()) >= '0' && c <= '9');
- if (neg)
- return -ret;
- return ret;
- }
- public long nextLong() throws IOException
- {
- long ret = 0;
- byte c = read();
- while (c <= ' ')
- c = read();
- boolean neg = (c == '-');
- if (neg)
- c = read();
- do {
- ret = ret * 10 + c - '0';
- }
- while ((c = read()) >= '0' && c <= '9');
- if (neg)
- return -ret;
- return ret;
- }
- public double nextDouble() throws IOException
- {
- double ret = 0, div = 1;
- byte c = read();
- while (c <= ' ')
- c = read();
- boolean neg = (c == '-');
- if (neg)
- c = read();
- do {
- ret = ret * 10 + c - '0';
- }
- while ((c = read()) >= '0' && c <= '9');
- if (c == '.')
- {
- while ((c = read()) >= '0' && c <= '9')
- {
- ret += (c - '0') / (div *= 10);
- }
- }
- if (neg)
- return -ret;
- return ret;
- }
- private void fillBuffer() throws IOException
- {
- bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
- if (bytesRead == -1)
- buffer[0] = -1;
- }
- private byte read() throws IOException
- {
- if (bufferPointer == bytesRead)
- fillBuffer();
- return buffer[bufferPointer++];
- }
- public void close() throws IOException
- {
- if (din == null)
- return;
- din.close();
- }
- }
- static void display(List<Truck> t) {
- for(int i=0;i<t.size();i++) out.println(t.get(i).getST()+" "+t.get(i).getF());
- out.println();
- }
- static PrintStream out = new PrintStream(System.out);
- public static void main(String[] args)throws IOException {
- //try {
- Reader sc = new Reader();
- PrintWriter out = new PrintWriter(System.out);
- //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int tc=sc.nextInt();//Integer.valueOf(br.readLine());
- while(tc-->0) {
- int n=sc.nextInt(),stp[]=new int[n],fuel[]=new int[n];
- List<Truck> list = new ArrayList<>();
- for(int i=n-1;i>=0;i--) {
- stp[i]=sc.nextInt();
- fuel[i]=sc.nextInt();
- }
- int L=sc.nextInt(),P=sc.nextInt();
- if(P>=L) { out.println(0); continue; }
- for(int i=0;i<n;i++) list.add(new Truck(L-stp[i],fuel[i]));
- Collections.sort(list,new TCmp());
- //display(list);
- PriorityQueue<Integer> pQ=new PriorityQueue<>(Collections.reverseOrder());
- long cfl=P;int ct=0,i=0;
- boolean b=false;
- for(;cfl<L;) {
- if(list.get(i).getST()>cfl) {
- while(!pQ.isEmpty() && list.get(i).getST()>cfl) { cfl+=pQ.poll(); ct++; }
- if(list.get(i).getST()>cfl || cfl>=L) break;
- }
- while(i<n && list.get(i).getST()<=cfl) pQ.add(list.get(i++).getF());
- cfl+=pQ.poll();
- ct++;
- //out.println(cfl+" "+ct);
- //if(cfl>=L) break;
- }
- out.println((cfl<L)?-1:ct);
- }
- //out.println("");
- out.flush();
- out.close();
- //} catch(Exception e) {}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement