Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- bool cmp(pair<int, int>&p1, pair<int, int>&p2)
- {
- if (p1.first == p2.first) return (p1.second >= p2.second);
- return (p1.first < p2.first);
- }
- int main()
- {
- //sorting using functions
- //lower_bound and upper_bound
- //
- // maps and sets
- // vector<int> v;
- // int n; cin >> n;
- // for (int i = 0; i < n; i++)
- // {
- // int x; cin >> x;
- // v.push_back(x);
- // }
- //v n
- // cout << v[0] << endl;
- //iterators
- // cout << *(v.begin()) << endl;//0th index
- // cout << *(v.begin() + 2) << endl;
- //end iterator
- // cout << *(v.end()) << endl;
- // sort(v.begin(), v.end());
- //l=0,r=(n-1)->for whole vector
- // sort(v.begin() + l, v.begin() + r + 1);
- //sort(v.begin(),v.end())->sorted whole vector
- // for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
- //you for (1<=i<=n)
- // int l, r; cin >> l >> r; //1 indexing
- // l = l - 1; r = r - 1;//0 indexing
- // sort(v.begin() + l, v.begin() + r + 1);
- //O(nlogn)->heap sort and quick sort (T.c)
- // for (int i = 0; i < v.size(); i++)
- // {
- // cout << v[i] << " ";
- // }
- // vector<pair<int, int>> v;
- // int n; cin >> n;
- // for (int i = 0; i < n; i++)
- // {
- // int x, y; cin >> x >> y;
- // v.push_back(make_pair(x, y));
- // }
- // for (int i = 0; i < n; i++)
- // {
- // cout << v[i].first << " " << v[i].second << endl;
- // }
- // int l, r; cin >> l >> r;//0 indexing
- // sort(v.begin() + l, v.begin() + r + 1);
- // cout << "Vector After Sorting" << endl;
- // for (int i = 0; i < n; i++)
- // {
- // cout << v[i].first << " " << v[i].second << endl;
- // }
- //upper-Bound and lower-bound
- vector<pair<int, int>> v;
- int n; cin >> n;
- for (int i = 0; i < n; i++)
- {
- int x, y; cin >> x >> y;
- v.push_back(make_pair(x, y));
- }
- //sorted
- // sort(v.begin(), v.end());
- //lower_bound->first occurence of x (if x is present)
- // int x; cin >> x;
- // auto itr = lower_bound(v.begin(), v.end(), x) - v.begin();
- // cout << itr << endl;
- // int itr = upper_bound(v.begin(), v.end(), x) - v.begin();
- // cout << itr << endl;
- // for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
- // cout << endl;
- // for (auto i : v)
- // {
- // cout << i << " ";
- // }
- sort(v.begin(), v.end());
- for (auto i : v)
- {
- cout << i.first << " " << i.second << " " << endl;
- }
- cout << "AFTER USING COMPARATOR FUNCTION" << endl;
- sort(v.begin(), v.end(), cmp);
- for (auto i : v)
- {
- cout << i.first << " " << i.second << " " << endl;
- }
- // 4
- // 0 -1
- // 1 2
- // 0 -2
- // 1 3
- // 0 -2
- // 0 -1
- // 1 2
- // 1 3
- // AFTER USING COMPARATOR FUNCTION
- // 0 -1
- // 0 -2
- // 1 3
- // 1 2
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement