Advertisement
_Alpha_

614-A

Jan 19th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. /* package codechef; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. public final class Codechef
  9. {
  10.     public static int sol(int s,int n,HashSet<Integer> hs){
  11.         int ans=0;
  12.         if(!hs.contains(s))
  13.             return ans;
  14.         int p1=s,p2=s;
  15.         while(p1>0 && p2<=n && ans<1001){
  16.             if(!hs.contains(p1)){
  17.                 return ans;
  18.             }
  19.             p1--;
  20.             if(!hs.contains(p2)){
  21.                 return ans;
  22.             }
  23.             p2++;
  24.             ans++;
  25.         }
  26.         while(p1>0 && ans<1001){
  27.             if(!hs.contains(p1)){
  28.                 return ans;
  29.             }
  30.             p1--;
  31.             ans++;
  32.         }
  33.         while(p2<=n && ans<1001){
  34.             if(!hs.contains(p2)){
  35.                 return ans;
  36.             }
  37.             p2++;
  38.             ans++;
  39.         }
  40.         return ans;
  41.     }
  42.     public static void main (String[] args) throws java.lang.Exception
  43.     {
  44.         Scanner sc=new Scanner(System.in);
  45.         int t=sc.nextInt();
  46.         while(t>0){
  47.             int n=sc.nextInt();
  48.             int s=sc.nextInt();
  49.             int k=sc.nextInt();
  50.             HashSet<Integer> hs=new HashSet<>();
  51.             while(k>0){
  52.                 hs.add(sc.nextInt());
  53.                 k--;
  54.             }
  55.             int ans=sol(s,n,hs);
  56.             System.out.println(ans);
  57.             t--;
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement