NCST--HBCPC2021选拔赛B
Biochemistry
来源:(ncst内网)http://acm.ncst.edu.cn/problem.php?cid=1001&pid=1
标签:【map】【优先队列
题目简述
生物化学是研究生物体内部及其相关化学过程的科学分支。在现代魔法世界里,魔药终于和魔药结合在一起了。现在,比尔想用生物化学的方法测试一种新的药剂。他会反复地往锅里倒药草,而且每次都要知道药草的最大用量。请帮帮他.
Input
将有𝑇个测试用例。
对于每个测试用例,第一行将有一个整数𝑛(1≤𝑛≤106),表示倒草药的次数。
对于接下来的𝑛行,将有一个字符串𝑠和一个整数w(w<103),表示药草的名称和数量。保证药草的名称长度不超过15,并且没有空格。
Output
对于每次投放,输出当前锅内最多的一种药草的名称和最大数量
Sample Input
1
3
fluxweed 10
moly 20
fluxweed 30
Sample Output
fluxweed 10
moly 20
fluxweed 40
题目思路
比赛时理解错了题意,这里用map<string,int>把名称和数量连接起来,每次输入,改变数量后,放入优先队列(大根堆);别忘了重载运算符。
代码(附注释)
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <cstring>
#include <map>
using namespace std;
#define endl '\n'
#define ll long long
int n,t;
struct node
{
string name;
int amount;
friend bool operator < (const node &a,const node &b){//重载 <
if(a.amount==b.amount) return a.name<b.name;
return a.amount<=b.amount;
}
};
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>t;
while(t--){
map<string,int>mp;
priority_queue<node>q;
int x;
string s;
cin>>n;
while(n--){
cin>>s>>x;
mp[s]+=x;//更新值
q.push((node){s,mp[s]});
node p=q.top();//队首元素
cout<<p.name<<' '<<p.amount<<endl;
}
}
return 0;
}