Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. //#include <ext/pb_ds/assoc_container.hpp>
  3. //#include <ext/pb_ds/tree_policy.hpp>
  4. //#include <Rescue.h>
  5.  
  6.  
  7. using namespace std;
  8. //using namespace __gnu_pbds;
  9.  
  10.  
  11. #define INF (1<<30)
  12. #define INFll (1ll<<62)
  13. #define F first
  14. #define S second
  15. #define MOD 1000000007
  16. #define mkp(a, b) make_pair(a, b)
  17. #define MAX(a, b) (((a) >= (b)) ? (a) : (b))
  18. #define MIN(a, b) (((a) <= (b)) ? (a) : (b))
  19.  
  20.  
  21.  
  22. typedef long long ll;
  23. typedef unsigned long long ull;
  24. typedef long double ld;
  25.  
  26. #ifndef LOCAL
  27. #define cerr if(false) cerr
  28. #endif // LOCAL
  29.  
  30. #define FOR(I, A, B) for(int (I) = (A); (I) < (B); (I)++)
  31. #define ROF(I, A, B) for(int (I) = (A); (I) >= (B); (I)--)
  32. #define SQR(A) 1ll*(A)*(A)
  33.  
  34.  
  35. //typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
  36.  
  37.  
  38. const char array_sep[] = " ";
  39. const char array_end[] = "";
  40.  
  41.  
  42. const char pair_sep[] = " ";
  43. const char pair_end[] = "";
  44.  
  45.  
  46. const char map_sep[] = "->";
  47. const char map_end[] = "\n";
  48.  
  49.  
  50. const int dx[] = {-1, 0, 1, 0};
  51. const int dy[] = {0, 1, 0, -1};
  52. const int ddx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
  53. const int ddy[] = {0, 1, 1, 1, 0, -1, -1, -1};
  54.  
  55.  
  56. template<typename A> ostream & operator<<(ostream & os, const vector<A> & x)
  57. {
  58. for(int i = 0; i < x.size(); i++)
  59. os << x[i] << array_sep;
  60. os << array_end;
  61. return os;
  62. }
  63.  
  64.  
  65. template<typename A, typename B> ostream & operator<<(ostream & os, const pair<A, B> & x)
  66. {
  67. os << x.first << pair_sep << x.second << pair_end;
  68. return os;
  69. }
  70.  
  71.  
  72. template<typename A> istream & operator>>(istream & is, vector<A> & x)
  73. {
  74. for(int i = 0; i < x.size(); i++)
  75. is >> x[i];
  76. return is;
  77. }
  78.  
  79.  
  80. template<typename A, typename B> istream & operator>>(istream & is, pair<A, B> & x)
  81. {
  82. is >> x.first >> x.second;
  83. return is;
  84. }
  85.  
  86. //#define cerr if(0)cerr
  87.  
  88. //
  89. //template<typename _key, typename _val> ostream & operator<<(ostream & os, map<_key, _val> & mp)
  90. //{
  91. // for(auto it : mp) // not for C++98 or earlier
  92. // os << it->F << map_sep << it->S << map_end;
  93. // return os;
  94. //}
  95. //
  96. //template<typename _key, typename _val> ostream & operator<<(ostream & os, unordered_map<_key, _val> & mp)
  97. //{
  98. // for(auto it : mp) // not for C++98 or earlier
  99. // os << it->F << map_sep << it->S << map_end;
  100. // return os;
  101. //}
  102. //
  103. //template<typename _key, typename _val> ostream & operator<<(ostream & os, set<_key> & st)
  104. //{
  105. // for(auto& it: st) // not for C++98 or earlier
  106. // os << it << endl;
  107. // return os;
  108. //}
  109. //
  110. //template<typename _key, typename _val> ostream & operator<<(ostream & os, unordered_set<_key> & st)
  111. //{
  112. // for(auto& it: st) // not for C++98 or earlier
  113. // os << it << endl;
  114. // return os;
  115. //}
  116.  
  117. template<typename _response> void die(_response ans)
  118. {
  119. cout << ans << endl;
  120. exit(0);
  121. }
  122.  
  123. int main()
  124. {
  125. #ifdef LOCAL
  126. freopen("input.txt", "r", stdin);
  127. // freopen("output.txt", "w", stdout);
  128. // freopen("errlog.log", "w", stderr);
  129. ios_base::sync_with_stdio(0);
  130. // cout << fixed << setprecision(3);
  131. #else // LOCAL
  132. // freopen("gcd-xor.in", "r", stdin);
  133. // freopen("gcd-xor.out", "w", stdout);
  134. ios_base::sync_with_stdio(0);
  135. // cout << fixed << setprecision(3);
  136. #endif // LOCAL
  137. int n;
  138. vector<int> a;
  139. vector<pair<int, int > > ans;
  140.  
  141. cin >> n;
  142. a.resize(n << 1);
  143. cin >> a;
  144.  
  145. set<pair<pair<int, int >, pair<int, int > > > st;
  146. vector<int> l_lnk(n << 1, -1);
  147. vector<int> r_lnk(n << 1, -1);
  148.  
  149. sort(a.begin(), a.end());
  150.  
  151. for(int i = 0; i < (n << 1); i++)
  152. {
  153. if(i) l_lnk[i] = i-1;
  154. if(i != (n << 1) - 1) r_lnk[i] = i+1;
  155. if(i) st.insert({{a[i] - a[i-1], -a[i]-a[i-1]}, {i-1, i}});
  156. }
  157.  
  158. while(true)
  159. {
  160. // cerr << "# " << i << " " << st.size() << " ";
  161. if(st.empty()) break;
  162. auto top = *st.begin(); st.erase(st.begin());
  163. int l = top.S.F;
  164. int r = top.S.S;
  165. cerr << top << endl;
  166. cerr << "\t#" << l_lnk[l] << " " << r_lnk[r] << endl;
  167. ans.push_back(mkp(a[top.S.F], a[top.S.S]));
  168.  
  169.  
  170. if(l_lnk[l] == -1 && r_lnk[r] == -1) continue;
  171. if(l_lnk[l] == -1 && r_lnk[r] != -1)
  172. {
  173. l_lnk[r_lnk[r]] = -1;
  174. st.erase({{a[r_lnk[r]] - a[r], -a[r]-a[r_lnk[r]]}, {r, r_lnk[r]}});
  175. continue;
  176. }
  177. if(l_lnk[l] != -1 && r_lnk[r] == -1)
  178. {
  179. r_lnk[l_lnk[l]] = -1;
  180. st.erase({{a[l] - a[l_lnk[l]], -a[l]-a[l_lnk[l]]}, {l_lnk[l], l}});
  181. continue;
  182. }
  183. if(l_lnk[l] != -1 && r_lnk[r] != -1)
  184. {
  185. r_lnk[l_lnk[l]] = r_lnk[r];
  186. l_lnk[r_lnk[r]] = l_lnk[l];
  187. st.erase({{a[l] - a[l_lnk[l]], -a[l]-a[l_lnk[l]]}, {l_lnk[l], l}});
  188. st.erase({{a[r_lnk[r]] - a[r], -a[r]-a[r_lnk[r]]}, {r, r_lnk[r]}});
  189. st.insert({{a[r_lnk[r]] - a[l_lnk[l]], -a[l_lnk[l]]-a[r_lnk[r]]}, {l_lnk[l], r_lnk[r]}});
  190. continue;
  191. }
  192. // cout << st.size() << endl;
  193. }
  194.  
  195. for(int i = 0; i < n; i++)
  196. cout << max(ans[i].F, ans[i].S) << " ";
  197. cout << endl;
  198. for(int i = 0; i < n; i++)
  199. cout << min(ans[i].F, ans[i].S) << " ";
  200.  
  201. return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement