我们发现其实题目的需求就是若干个键值对,将相同键的值合并
可以直接使用stl的map,很方便的实现了上述功能
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;
int __t = 1, n;
void solve() {
cin >> n;
map<int, int> mp;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
mp[x] += y;
}
for (auto [x, y] : mp) {
cout << x << " " << y << "\n";
}
return;
}
int32_t main() {
#ifdef ONLINE_JUDGE
ios::sync_with_stdio(false);
cin.tie(0);
#endif
// cin >> __t;
while (__t--)
solve();
return 0;
}

京公网安备 11010502036488号