Saleh127

Oct replesx CC

Oct 7th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. int main()
  6. {
  7. ios_base::sync_with_stdio(0);
  8. cin.tie(0);
  9. cout.tie(0);
  10.  
  11. test
  12. {
  13. ll n,x,p,k,a[100000],c,d=0,e,f,i,j=0,s=1000000,l=0,ans=0;
  14.  
  15. cin>>n>>x>>p>>k;
  16.  
  17. for(i=1; i<=n; i++)
  18. {
  19. cin>>a[i];
  20. if(a[i]==x) d=1;
  21. }
  22. if(d==0)
  23. {
  24. sort(a+1,a+n+1);
  25. a[k]=x;
  26. l=1;
  27. }
  28. sort(a+1,a+n+1);
  29. if(a[p]==x)
  30. {
  31. cout<<l<<endl;
  32. }
  33. else if(a[k]==x)
  34. {
  35. cout<<-1<<endl;
  36. }
  37. else
  38. {
  39. for(i=1; i<=n; i++)
  40. {
  41. if(a[i]==x)
  42. {
  43. j=max(j,i);
  44. s=min(s,i);
  45. }
  46. }
  47. if(p<j)
  48. {
  49. if(k<=p)
  50. {
  51. ans=min(abs(p-j)+l,abs(p-s)+l);
  52. cout<<ans<<endl;
  53. }
  54. else cout<<-1<<endl;
  55. }
  56. else
  57. {
  58. if(k>=p)
  59. {
  60. ans=min(abs(p-j)+l,abs(p-s)+l);
  61. cout<<ans<<endl;
  62. }
  63. else cout<<-1<<endl;
  64. }
  65. }
  66. }
  67. return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment