题解 | #合并表记录#

合并表记录

http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

用set和map配合实现

#include<iostream>
#include<set>
#include<map>
using namespace std;

int main(){
    int N;
    cin >> N;
    set<int> key;
    map<int,int> num;
    while(N--){
        int k, v;
        cin >> k >> v;
        set<int>::iterator it = key.insert(k).first;
        if(key.insert(k).second)
            num[*it] = v;
        else
            num[*it] += v;
    }
    for(set<int>::iterator it = key.begin(); it != key.end(); ++it){
        cout << *it << ' ' << num[*it] << endl;
    }
}
全部评论

相关推荐

karis_aqa:和hr没关系,都是打工的
点赞 评论 收藏
分享
帅宇殿下:佬,简历写的什么
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务