I tried to sort a vector<pair<int, int>>
by using STL sort with my own boolean function.
#include <bits/stdc++.h>
using namespace std;
bool comp(pair<int, int> u1, pair<int, int> u2){
if(u1.first != u2.first) return u1.first < u2.first;
return u2.second > u2.second;
}
int main(void){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int univs, day, pay, max_income = 0, spent = 0;
vector<pair<int, int>> day_pay;
cin >> univs;
for(int u = 0; u < univs; u++){
cin >> pay >> day;
day_pay.push_back(make_pair(day, pay));
}
for(int i = 0; i < univs; i++) cout << day_pay[i].first << " " << day_pay[i].second << endl;
cout << endl;
sort(day_pay.begin(), day_pay.end(), comp);
for(int i = 0; i < univs; i++) cout << day_pay[i].first << " " << day_pay[i].second << endl;
for(int u = 0; u < univs; u++){
if(day_pay[u].first <= spent) continue;
max_income += day_pay[u].second;
spent++;
}
cout << max_income;
}
this is test case:
4
50 2
10 1
20 2
30 1
I want to sort this case as
30 1
10 1
50 2
20 2
What should I do for solving this problem?