根據ID統計分數求最高分-E

現請你根據比賽結果統計出技術最強的那個學校。
輸入描述:
輸入在第1行給出不超過105的正整數N,即參賽人數。隨後N行,每行給出一位參賽者的信息和成績,包括其所代表的學校的編號(從1開始

連續編號)、及其比賽成績(百分制),中間以空格分隔。

輸出描述:
在一行中給出總得分最高的學校的編號、及其總分,中間以空格分隔。題目保證答案唯一,沒有並列。

輸入例子:
6
3 65
2 80
1 100
2 70
3 40
3 0
輸出例子:
2 150

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

int arr[100000+1]={0};

void solve_1(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int shoolNo;
        int score;
        cin>>shoolNo>>score;
        arr[shoolNo]+=score;
    }
    int maxIndex=0;
    int maxValue=0;
    for(int i=0;i<n;i++){
        if(maxValue<arr[i]){
            maxValue=arr[i];
            maxIndex=i;
        }
    }
    cout<<maxIndex<<" "<<maxValue<<endl;
    return;
}

void solve_2(){
    int n;
    map<int,int> map_count;
    cin>>n;
    map<int,int>::iterator  p;
    for(int i=0;i<n;i++){
        int schoolNo;
        int score;
        cin>>schoolNo>>score;
        p = map_count.find(schoolNo);
        if(p==map_count.end())
            map_count[schoolNo]=score;
        else
            map_count[schoolNo]+=score;
    }
    int maxIndex=0;
    int maxValue=0;
    p = map_count.begin();
    while(p!=map_count.end()){
        if(p->second>maxValue){
            maxValue=p->second;
            maxIndex=p->first;
        }
        p++;
    }
    cout<<maxIndex<<" "<<maxValue<<endl;
}

int main()
{
    //solve_1();
    solve_2();
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章