fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5.  
  6. #define for0(i,m,n) for(int i=m;i<n;i++)
  7. #define for1(i,m,n) for(int i=m;i<=n;i++)
  8.  
  9. #define vll(v) v.begin(),v.end()
  10. #define fi first
  11. #define se second
  12. #define el '\n'
  13.  
  14. const int N=1e7;
  15. const int mod=1e9+7;
  16. int n,k,p;
  17. // bai 2
  18.  
  19. signed main(){
  20. ios_base::sync_with_stdio(0);
  21. cin.tie(0);
  22. cout.tie(0);
  23. // freopen("CAKE.INP","r",stdin);
  24. // freopen("CAKE.OUT","w",stdout);
  25. cin>>n>>k>>p;
  26. vector<int>a(n),vt(k);
  27.  
  28. for(auto &x:a ) cin>>x;
  29.  
  30. for(auto &x:vt ) cin>>x;
  31. // a.resize(n+1);
  32. //vt.resize(k+1);
  33. vt.push_back(0);a.push_back(0);
  34.  
  35.  
  36.  
  37. sort(vll(a));
  38. sort(vll(vt));
  39. // for(auto x:a) cout<<x<<el;
  40. auto check=[&](int sum){
  41.  
  42. bool ok=1;
  43. int le_n=1,ri_n=n,le_b=1,ri_b=k;
  44. while(le_n<=ri_n){
  45. // cout<<le_b<<' '<<ri_b<<el;
  46. if(abs(a[le_n]-p)>sum||abs(a[ri_n]-p)>sum){
  47. // cout<<a[le_n]<<' '<<a[ri_n]<<el;
  48. return 0;
  49. }
  50. if(ok){
  51. for1(i,le_b,k){
  52. if(i>ri_b) return 0;
  53. int s;
  54. if((p<=vt[i]&&vt[i]<=a[le_n])||(a[le_n]<=vt[i]&&vt[i]<=p)) s=abs(p-a[le_n]);
  55. else s=abs(vt[i]-a[le_n])+abs(vt[i]-p);
  56. if(s<=sum){
  57. le_b=i+1;
  58. le_n++;
  59. ok=0;
  60. break;
  61. }
  62. }
  63. }
  64. else {
  65. for(int i=ri_b;i>=1;i--){
  66. if(le_b>i) return 0;
  67. int s;
  68. if((p<=vt[i]&&vt[i]<=a[ri_n])||(a[ri_n]>=vt[i]&&vt[i]>=p)) s=abs(p-a[ri_n]);
  69. else s=abs(vt[i]-a[ri_n])+abs(vt[i]-p);
  70. if(s<=sum){
  71. ri_b=i-1;
  72. ri_n--;
  73. ok=1;
  74. break;
  75. }
  76. }
  77. }
  78.  
  79. }
  80. return 1;
  81. };
  82.  
  83. int l=1,r=1e8,ans;
  84. while(l<=r){
  85. // cout<<l<<' '<<r<<el;
  86. int m=l+r>>1;
  87. // cout<<m<<el;
  88. if(check(m)){
  89. ans=m;
  90. r=m-1;
  91. }
  92. else l=m+1;
  93. }
  94. cout<<ans;
  95. return 0;
  96. }
  97. /*
  98. 2 4 50
  99. 20 100
  100. 60 10 40 80
  101.  
  102. 1 2 10
  103. 11
  104. 15 7
  105. */
  106.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
1