fork download
  1. #include <bits/stdc++.h>
  2. #include <cstdint>
  3. #include <iomanip>
  4. #define PowerUp ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(0);
  5. #define BattleMode ll t;cin>>t;while(t--)
  6. #define Thor solve();
  7. #define Thunder if(t>0){cout<<line;}
  8. #define ll long long
  9. #define line "\n"
  10. #define line3 cout<<line;
  11. #define stop int abnmj; cin>>abnmj;
  12. #define tap "\t"
  13. #define loop(end,char) for(ll char=0;char<end;char++)
  14. #define loopr(end,char) for(int char=end-1;char>=0;char--)
  15. #define arrin(name) loop(name.size(),p){cin>>name[p];}
  16. #define print(name) for(auto ik:name){cout<<ik<<" ";}
  17. #define print2(name) for(auto ik:name){cout<<ik.first<<" "<<ik.second<<line;}
  18. #define temple template<typename t,typename v>
  19. #define all(x) x.begin(),x.end()
  20. #define allr(x) x.rbegin(),x.rend()
  21. using namespace std;
  22. const double pi = acos(-1);
  23. const ll MOD = 1e9 + 7;
  24. const double EPS = 1e-7;
  25.  
  26. void solve()
  27. {
  28. ll n, x;
  29. cin >> n >> x;
  30. ll y=abs(x);
  31. vector<ll> vec(n);
  32. arrin(vec)
  33. multiset<ll> sett;
  34. for (int i = 0; i < n; i++)
  35. sett.insert(vec[i]);
  36. ll mini = LLONG_MAX;
  37. ll num1, num2;
  38. ll sub;
  39.  
  40. for (int i = 0; i < n; i++)
  41. {
  42. sett.erase(sett.find(vec[i]));
  43. ll value=vec[i];
  44. ll key1=value-y;
  45. ll key2=value+y;
  46. auto it1 = sett.lower_bound(key1);
  47. if (it1!=sett.end())
  48. {
  49. sub=abs(value- *it1);
  50. sub=abs(sub-y);
  51. if ((sub==mini && num1+num2> value+ *it1 ) || sub<mini)
  52. {
  53. mini=sub;
  54. num1=value;
  55. num2= * it1;
  56. }
  57. }
  58. if (it1!=sett.begin())
  59. {
  60. it1--;
  61. sub=abs(value- *it1);
  62. sub=abs(sub-y);
  63. if ((sub==mini && num1+num2> value+ *it1 ) || sub<mini)
  64. {
  65. mini=sub;
  66. num1=value;
  67. num2= * it1;
  68. }
  69. }
  70. it1 = sett.lower_bound(key2);
  71. if (it1!=sett.end())
  72. {
  73. sub=abs(value- *it1);
  74. sub=abs(sub-y);
  75. if ((sub==mini && num1+num2> value+ *it1 ) || sub<mini)
  76. {
  77. mini=sub;
  78. num1=value;
  79. num2= * it1;
  80. }
  81. }
  82. if (it1!=sett.begin())
  83. {
  84. it1--;
  85. sub=abs(value- *it1);
  86. sub=abs(sub-y);
  87. if ((sub==mini && num1+num2> value+ *it1 ) || sub<mini)
  88. {
  89. mini=sub;
  90. num1=value;
  91. num2= * it1;
  92. }
  93. }
  94. sett.insert(value);
  95. }
  96. if (x>=0)
  97. cout<<max(num1,num2)<<" "<<min(num1,num2);
  98. else
  99. cout<<min(num1,num2)<<" "<<max(num1,num2);
  100.  
  101. }
  102.  
  103. int main()
  104. {
  105. #ifndef ONLINE_JUDGE
  106. freopen("input.txt", "r", stdin);
  107. freopen("output.txt", "w", stdout);
  108. freopen("error.txt", "w", stderr);
  109. #endif
  110. PowerUp
  111. Thor
  112. }
  113.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
94510855752000 0