fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pb push_back
  5. #define all(x) x.begin(), x.end()
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace __gnu_pbds;
  9. template <typename T> using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  10. template <typename T, typename R> using o_map = tree<T, R, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  11. typedef long long ll;
  12.  
  13. #define inf 1e9
  14. #define MOD 1000000007
  15. #define vint vector<int>
  16. #define vll vector<ll>
  17. #define no cout << "NO" << endl;
  18.  
  19. void solve() {
  20. ll m,n;
  21. cin>>m>>n;
  22. vector<vector<ll>>a(m,vector<ll>(n));
  23. set<ll>shops;
  24. map<ll,ll>mp;
  25. for (int i=0;i<m;i++) {
  26. for (int j=0;j<n;j++) {
  27. cin>>a[i][j];
  28. }
  29. }
  30. for (int i=0;i<n;i++) {
  31. ll mx=0;
  32. ll index=-1;
  33. for (int j=0;j<m;j++) {
  34. if (a[j][i]>mx) {
  35. mx=a[j][i];
  36. index=j;
  37. }
  38. }
  39.  
  40. // ala kol foulen chno indice mte3o
  41. mp[i]=index;
  42. }
  43.  
  44. // kol 3ABD chno index mte3 shop mte3o
  45.  
  46. // 5arjna les indices
  47. set<ll>st;
  48. for (auto i:mp) {
  49. st.insert(i.second);
  50. }
  51. // 5arjna answer
  52. multiset<ll>ans;
  53. for (auto i:mp) {
  54. ans.insert(a[i.second][i.first]);
  55. }
  56. if (st.size()==n) {
  57.  
  58.  
  59. for (auto i:mp) {
  60. cout<<i.first<<" "<<i.second<<endl;
  61. }
  62. ll ans2=0;
  63. for (auto i:mp) {
  64. // i.first foulen numero i
  65. st.erase(i.second);
  66. ans.erase( ans.find(a[i.second][i.first]) );
  67. ll mx=0;
  68. for (auto j:st) {
  69. mx=max(mx, a[j][i.first] );
  70. }
  71.  
  72. ans2=max(ans2,min(mx,*(ans.begin())) );
  73. ans.insert(a[i.second][i.first]);
  74. st.insert(i.second);
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
  81. cout<<ans2 <<endl;
  82.  
  83.  
  84. }
  85. else {
  86.  
  87. cout<<(*ans.begin())<<endl;
  88.  
  89. }
  90.  
  91.  
  92.  
  93. }
  94.  
  95.  
  96.  
  97. int main() {
  98. ios::sync_with_stdio(false);
  99. cin.tie(nullptr);
  100.  
  101. #ifndef ONLINE_JUDGE
  102. freopen("input.txt", "r", stdin);
  103. freopen("output.txt", "w", stdout);
  104. #endif
  105.  
  106.  
  107. int t = 1 ;
  108. cin >> t;
  109. while (t--) {
  110. solve();
  111. }
  112.  
  113. return 0;
  114. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
0