Advertisement
najimCseJu

Loj 1084 - Winter

Sep 24th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.01 KB | None | 0 0
  1. import java.io.*;
  2. import java.lang.reflect.Array;
  3. import java.nio.charset.StandardCharsets;
  4. import java.nio.file.StandardOpenOption;
  5. import java.util.Arrays;
  6. import java.util.Collections;
  7. import java.util.StringTokenizer;
  8.  
  9.  
  10. public class Main {
  11.     static private int MaxN = 100001;
  12.     static private int Inf = 10000000;
  13.     static class Tree
  14.     {
  15.         private int dp[] = new int[MaxN<<2];
  16.         public void init(int n)
  17.         {
  18.             Arrays.fill(dp,0,n<<2,Inf);
  19.         }
  20.  
  21.         public int getMin(int cn,int st,int ed,int l,int r)
  22.         {
  23.             if(st==l && ed==r)
  24.             {
  25.                 return dp[cn];
  26.             }
  27.             int mid = (st+ed)>>1;
  28.             int lChild = cn<<1;
  29.             int rChild  = 1 | lChild;
  30.             if(r<=mid)return getMin(lChild,st,mid,l,r);
  31.             if(l>mid)return getMin(rChild,mid+1,ed,l,r);
  32.             int lmin = getMin(lChild,st,mid,l,mid);
  33.             int rmin = getMin(rChild,mid+1,ed,mid+1,r);
  34.             return Math.min(lmin,rmin);
  35.         }
  36.  
  37.         public void setMin(int cn,int st,int ed,int pos,int val)
  38.         {
  39.             if(st==ed)
  40.             {
  41.                 dp[cn]=val;
  42.                 return ;
  43.             }
  44.             int mid =(st+ed)>>1;
  45.             int lChild = cn<<1;
  46.             int rChild  = 1 | lChild;
  47.             if(pos<=mid)setMin(lChild,st,mid,pos,val);
  48.             else setMin(rChild,mid+1,ed,pos,val);
  49.             dp[cn]=Math.min(dp[lChild],dp[rChild]);
  50.         }
  51.     }
  52.  
  53.     public static void main(String[] args) throws IOException {
  54.  
  55.        Reader reader = new Reader().InitConsoleReading();
  56.         //Reader reader = new Reader().InitFileReading("C:\\Users\\najim\\OneDrive\\Documents\\in_large.txt");
  57.         Writer writer = new Writer().InitConsoleWriting();
  58.         //Writer writer = new Writer().InitFileWriting("C:\\Users\\najim\\OneDrive\\Documents\\out_java.txt");
  59.         int totalNumberOfTestCases = reader.nextInt();
  60.         int n,k;
  61.         int[] arr = new int[MaxN];
  62.         Tree tree = new Tree();
  63.         for(int cs=1;cs<=totalNumberOfTestCases;cs++)
  64.         {
  65.             //long beforeUsedMem=Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory();
  66.             n = reader.nextInt();
  67.             k = reader.nextInt();
  68.             k<<=1;
  69.  
  70.             for(int i=1;i<=n;i++)
  71.             {
  72.                 arr[i] = reader.nextInt();
  73.             }
  74.             Arrays.sort(arr,1,n+1);
  75.             tree.init(n);
  76.  
  77.             for(int i=1;i<=n;i++)
  78.             {
  79.                 int l = Arrays.binarySearch(arr,1,i+1,arr[i]-k);
  80.                 if(l<0)l=-l-1;
  81.                 --l;
  82.                 int r = i-3;
  83.                 if(l==0)
  84.                 {
  85.                     if(i>=3)
  86.                     {
  87.                         tree.setMin(1,1,n,i,1);
  88.                     }
  89.                 }
  90.                 else if(l<=r)
  91.                 {
  92.                     if(i==8)
  93.                     {
  94.                         int p = 0;
  95.                     }
  96.                     int min = tree.getMin(1,1,n,l,r);
  97.                     tree.setMin(1,1,n,i,min+1);
  98.                 }
  99.             }
  100.             int res =  tree.getMin(1,1,n,n,n);
  101.             res = res>=Inf? -1 : res;
  102.             writer.WriteCase("Case ",cs,": ").WriteInt(res).WriteNewLine();
  103.             System.gc();
  104.         }
  105.         writer.Dispose();
  106.     }
  107.  
  108.     static class Reader {
  109.         public BufferedReader reader;
  110.         public StringTokenizer tokenizer;
  111.  
  112.         public Reader() {
  113.         }
  114.         public Reader InitConsoleReading()
  115.         {
  116.             reader = new BufferedReader(new InputStreamReader(System.in), 32768);
  117.             tokenizer = null;
  118.             return this;
  119.         }
  120.         public Reader InitFileReading(String filePath) throws FileNotFoundException {
  121.             reader = new BufferedReader(new FileReader(filePath), 32768);
  122.             tokenizer = null;
  123.             return this;
  124.         }
  125.         public String next() {
  126.             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  127.                 try {
  128.                     tokenizer = new StringTokenizer(reader.readLine());
  129.                 } catch (IOException e) {
  130.                     throw new RuntimeException(e);
  131.                 }
  132.             }
  133.             return tokenizer.nextToken();
  134.         }
  135.  
  136.         public int nextInt() {
  137.             return Integer.parseInt(next());
  138.         }
  139.  
  140.         public long nextLong() {
  141.             return Long.parseLong(next());
  142.         }
  143.     }
  144.     static class Writer {
  145.         private BufferedWriter log;
  146.  
  147.         public Writer InitConsoleWriting()
  148.         {
  149.             log = new BufferedWriter(new OutputStreamWriter(System.out));
  150.             return this;
  151.         }
  152.         public Writer InitFileWriting(String filePath) throws IOException {
  153.             log = new BufferedWriter(new FileWriter(filePath));
  154.             return this;
  155.         }
  156.  
  157.         public Writer WriteInt(int inp) throws IOException {
  158.             this.log.write(String.valueOf(inp));
  159.             return this;
  160.         }
  161.  
  162.         public Writer WriteChar(char inp) throws IOException {
  163.             this.log.write(inp);
  164.             return this;
  165.         }
  166.         public Writer WriteNewLine() throws IOException {
  167.             return this.WriteChar('\n');
  168.         }
  169.         public Writer WriteStr(String str) throws IOException {
  170.             this.log.write(str);
  171.             return this;
  172.         }
  173.  
  174.         public Writer WriteIntArray(int inp[], char delimChar) throws IOException {
  175.             int len = inp.length;
  176.             if(len>0)
  177.             {
  178.                 log.write(String.valueOf(inp[0]));
  179.                 for(int i=1;i<len;i++)
  180.                 {
  181.                     log.write(String.format("%c%s",delimChar,String.valueOf(inp[i])));
  182.                 }
  183.             }
  184.             return this;
  185.         }
  186.         public Writer WriteCharArray(char inp[], char delimChar) throws IOException {
  187.             int len = inp.length;
  188.             if(len>0)
  189.             {
  190.                 log.write(String.valueOf(inp[0]));
  191.                 for(int i=1;i<len;i++)
  192.                 {
  193.                     log.write(String.format("%c%s",delimChar,String.valueOf(inp[i])));
  194.                 }
  195.             }
  196.             return this;
  197.         }
  198.         //
  199.  
  200.         public Writer WriteStrArray(String inp[], char delimChar) throws IOException {
  201.             int len = inp.length;
  202.             if(len>0)
  203.             {
  204.                 log.write(inp[0]);
  205.                 for(int i=1;i<len;i++)
  206.                 {
  207.                     log.write(String.format("%c%s",delimChar,inp[i]));
  208.                 }
  209.             }
  210.             return this;
  211.         }
  212.         public Writer WriteCase(String prefix,int caseNo,String suffix) throws IOException {
  213.             return this.WriteStr(String.format("%s%d%s",prefix,caseNo,suffix));
  214.         }
  215.  
  216.         public Writer Dispose() throws IOException {
  217.             this.log.flush();
  218.             this.log.close();
  219.             return this;
  220.         }
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement