codeforces Winner

qingyezhu發表於2013-10-13
/*
 * Winner.cpp
 *
 *  Created on: 2013-10-13
 *      Author: wangzhu
 */

/**
 * 先找出所有選手的分數和中最大的分數和,之後在所有選手的分數和中看有幾個是和最大的分數和相等,
 * 1)、若有多個,則比較誰在比賽結束錢分數先達到最大分數和,則是贏家;
 * 2)、若只有一個,則直接輸出。
 */
#include<cstdio>
#include<iostream>
#include<map>
#include<string.h>
using namespace std;
#define NMAX 1010
struct Node {
    int index, val;
    char name[33];
};
Node node[NMAX];
map<string, int> myMap;
map<string, int> myMap1;
map<string, int>::iterator mapIterator;

void find(int n, int nmax) {
    int m;
    for (int i = 0; i < n; i++) {
        for (mapIterator = myMap1.begin(); mapIterator != myMap1.end();
                mapIterator++) {
            if (0 == strcmp(node[i].name, mapIterator->first.c_str())) {
                //    printf("%d %d %s\n", node[i].index, node[i].val, node[i].name);
                m = myMap1[mapIterator->first];
                m += node[i].val;
                myMap1[mapIterator->first] = m;
                if (m >= nmax) {
                    printf("%s\n", node[i].name);
                    return;
                }
            }
        }
    }
}

int main() {
    freopen("data.in", "r", stdin);
    int n, m,nmax;
    string nmaxStr;
    while (~scanf("%d", &n)) {
        myMap.clear();
        myMap1.clear();

        for (int i = 0; i < n; i++) {
            scanf("%s%d", node[i].name, &node[i].val);
            node[i].index = i;
            myMap[node[i].name] +=node[i].val;
        }
        nmax = -1;
        for(mapIterator = myMap.begin();mapIterator!=myMap.end();mapIterator++){
            if(nmax < mapIterator->second){
                nmax = mapIterator->second;
            }
        }
        m = 0;
        for(mapIterator = myMap.begin();mapIterator!=myMap.end();mapIterator++){
            if(nmax == mapIterator->second){
                nmaxStr = mapIterator->first;
                myMap1[mapIterator->first] = 0;
                m ++;
            }
        }
        //printf("%d %d\n",m,nmax);
        if(m != 1){
             find(n,nmax);
        }else{
            printf("%s\n",nmaxStr.c_str());
        }
    }
    return 0;
}

 

相關文章