#include <iostream>
#include <map>
using namespace std;
int main() {
int n;
cin>>n;
map<int, int> mp;
while(n--){
int k,v;
cin>>k>>v;
mp[k]+=v;
}
for(auto & it : mp)
cout<<it.first<<" "<<it.second<<endl;
return 0;
}
// 64 位输出请用 printf("%lld")
使用map存储键值对,因为map是用红黑树实现的,自动按照key值排序,时间复杂度为o(nlogn)

京公网安备 11010502036488号