fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<vector<int>> a;
  31.  
  32. vector<int> consistency(int n){
  33. //* {next minimum, row, col}
  34. priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>> > minHp;
  35. int maxi = INT32_MIN;
  36. for(int i=0; i<n; i++){
  37. maxi = max(maxi, a[i][0]);
  38. minHp.push({a[i][0], i, 0});
  39. }
  40. int s = -1;
  41. int e = -1;
  42. int dist = INT32_MAX;
  43. while(!minHp.empty()){
  44. auto top = minHp.top();
  45. minHp.pop();
  46. int mini = top[0];
  47. int r = top[1];
  48. int c = top[2];
  49. if(maxi-mini < dist){
  50. dist = maxi - mini;
  51. s = mini;
  52. e = maxi;
  53. }
  54. if(c+1 < a[r].size()) {
  55. maxi = max(maxi, a[r][c+1]);
  56. minHp.push({a[r][c+1], r, c+1});
  57. }
  58. else break;
  59.  
  60. }
  61.  
  62. return {s, e};
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. vector<int> practice(int n){
  80.  
  81.  
  82.  
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. void solve() {
  90.  
  91. int n;
  92. cin>> n;
  93.  
  94. a.resize(n);
  95. for(int i=0; i<n; i++){
  96. int m;
  97. cin >> m;
  98. for(int j=0; j<m; j++) {
  99. int x;
  100. cin >> x;
  101. a[i].push_back(x);
  102. }
  103. }
  104.  
  105. auto ans = consistency(n);
  106.  
  107. cout << ans[0] << " " << ans[1] << endl;
  108.  
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. int32_t main() {
  117. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  118.  
  119. int t = 1;
  120. // cin >> t;
  121. while (t--) {
  122. solve();
  123. }
  124.  
  125. return 0;
  126. }
Success #stdin #stdout 0.01s 5316KB
stdin
3
5
4 10 15 24 26
4
0 9 12 20 
4
5 18 22 30
stdout
20 24