此题用map,直接码:
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
int main(){
int n,s;
char c;
int x,y;
// 默认按键升序升序排,这样写降序排
map<int,int,greater<int> > smap,bmap;
while(scanf("%d %d",&n,&s)!=EOF)
{
// map 删除所有元素
smap.clear();
bmap.clear();
// map 是否为空 返回true or false
//smap.empty() ? printf("true\n") : printf("false\n");
for(int i=0 ;i<n ;i++)
{
getchar();
scanf("%c %d %d",&c,&x,&y);
if(c == 'S')
smap[x] += y;
if(c == 'B')
bmap[x] += y;
}
//printf("smap 中元素的个数:%d\n",smap.size());
//返回一个迭代器对象
map<int,int,greater<int> >::iterator it;
//查找一个指定元素
//find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。
//it = smap.find(12);
// 如果要删除的话
//smap.erase(it);
int scount = smap.size() > s ? s : smap.size();
int bcount = bmap.size() > s ? s : bmap.size();
for(it = smap.begin() ; it != smap.end(); it++)
{
printf("S %d %d\n",it->first,it->second);
scount--;
if(scount == 0)
break;
}
for(it = bmap.begin() ; it != bmap.end(); it++)
{
printf("B %d %d\n",it->first,it->second);
bcount--;
if(bcount == 0)
break;
}
}
}
重新学习下map的用法