CodeForces 2A Winner(map:映射)

map:映射

#include <map>

map是从键得到值的映射,由于重载了[ ]运算符,所以可以像数组取下标一样去使用它。
在map中,数据是默认以键的从小到大来排序的(类似于集合)。
如果要定义一个map,可以这样定义:map<string, int> a;
注意string类的地方可以用字符串的字符数组来代替

map的一些操作:
插入元素

#include <map>
map<string, int> a;
插入元素方法1:
a["str1"]=1;
a["str2"]=2;

插入元素方法2:
a.insert(pair<string, int>("str3", 3));

插入元素方法3:
a.insert(map<string, int>::value_type("str4", 4));

查找元素

与迭代器一起用
当所查找的关键key出现时,它返回数据所在对象的位置,如果沒有,返回iter与end函数的值相同。
// find 返回迭代器指向当前查找元素的位置否则返回map::end()位置
iter = a.find("str2");
if(iter != a.end())
       cout<<"Find"<<endl;
else
   cout<<"Do not Find"<<endl;

刪除与清空元素

关键字刪除
a.erase("str1"); 

清空操作
a.clear();
迭代器刪除
iter = a.find("str1");
a.erase(iter);
 
用迭代器范围刪除 : 把整个map清空
a.erase(a.begin(), a.end());

其他操作

begin()         返回指向map头部的迭代器
clear()        	删除所有元素
count()         返回指定元素出现的次数
empty()         如果map为空则返回true
end()           返回指向map末尾的迭代器
erase()         删除一个元素
find()          查找一个元素
insert()        插入元素
lower_bound()   返回键值>=给定元素的第一个位置
size()          返回map中元素的个数
swap()          交换两个map

应用:

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

int main()
{
	map<int, string> a;
	a[1]="abc";
	a[2]="def";
	a[3]="ghi";
	map<int, string>::iterator iter=a.begin();
	for (; iter!=a.end(); iter++)
		cout<<iter->first<<"  "<<iter->second<<endl;
	//->first是iter的key	->second是iter的value 
	return 0;
}

CodeForces 2A Winner
The winner of the card game popular in Berland “Berlogging” is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line “name score”, where name is a player’s name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It’s guaranteed that at the end of the game at least one player has a positive number of points.
Input
The first line contains an integer number n (1  ≤  n  ≤  1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in “name score” format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.
Output
Print the name of the winner.
Examples
Input
3
mike 3
andrew 5
mike 2
Output
andrew
Input
3
andrew 3
andrew 2
mike 5
Output
andrew

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

map<string, int> a, b;
int main()
{
	string str[1005];
	int score[1005];
	int n;
	scanf("%d", &n);
	for (int i=1; i<=n; i++){
		cin>>str[i];
		scanf("%d", &score[i]);
		a[str[i]]+=score[i];
	}
	int ans=0;
	map<string, int>::iterator iter=a.begin();
	for (; iter!=a.end(); iter++){
		if (iter->second>ans)
			ans=iter->second;
	}
	for (int i=1; i<=n; i++){
		b[str[i]]+=score[i];
		if (b[str[i]]>=ans && a[str[i]]>=ans){
			cout<<str[i];
			break;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

H4ppyD0g

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值