Advertisement
Guest User

Citire rapida si sortare de pair-uri

a guest
Jun 24th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cstdio>
  5. using namespace std;
  6.  
  7. typedef pair<int, int> pereche;
  8. vector< pereche > v;
  9.  
  10. /// Ordine crescatoare dupa primul camp si
  11. /// descrescatoare dupa al II-lea camp
  12. bool comparator( pereche A, pereche B )
  13. {
  14. if( A.first == B.first )
  15. return A.second > B.second;
  16. return A.first < B.first;
  17. }
  18.  
  19. void afiseaza()
  20. {
  21. for( int i = 0; i < v.size(); ++i )
  22. cout << "(" << v[i].first << ", " << v[i].second << ") ";
  23. cout << endl << endl;
  24. }
  25.  
  26.  
  27. int citeste_rapid_pozitiv()
  28. {
  29. int s = 0;
  30. char lit;
  31. while( (lit = getchar()) && lit >= '0' && lit <='9' )
  32. {
  33. s = ( s << 1 ) + ( s << 3 ) + (lit-'0');
  34. }
  35. return s;
  36. }
  37.  
  38. int main()
  39. {
  40. int n, i, a, b;
  41.  
  42. ios::sync_with_stdio( false );
  43. cin.tie();
  44.  
  45. n = citeste_rapid_pozitiv();
  46. cout << "Am citit rapid: " << n << endl;
  47.  
  48. for( i = 0; i < n; ++i )
  49. {
  50. a = citeste_rapid_pozitiv();
  51. b = citeste_rapid_pozitiv();
  52.  
  53. //pereche ceva;
  54. //ceva = make_pair( a, b );
  55. //v.push_back( ceva );
  56.  
  57. v.push_back( make_pair(a, b) );
  58. }
  59.  
  60. afiseaza();
  61. sort( v.begin(), v.end(), comparator );
  62. afiseaza();
  63.  
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement