Lighter The Weight Better The Gain Hackerearth coderinme

Lighter The Weight Better The Gain

Union Elections were held in Aligarh Muslim University last week. This time the “Intezamia” bought up a catch in polling procedure. Usually the candidate with maximum number of votes wins and each vote has same worth. But this time, this wasn,t the case. For simplicity assume that there is only one position for which students are contesting. Now a voter goes to the ballot box, picks a piece of paper and writes two things: his weight and the name of the person to which he wants to cast his vote and drops into ballot.
There are three operaions that can be performed on ballot:
1. Add new vote with X weight for Y candidate.
2. Remove the latest casted vote.(if bollot is empty, this operation will not do anything).
3. Print the name whose weight is currently minimum of all the votes in the ballot.
After all these operations the candidate for whom minimum votes were reported (as by operation 3) wins.
You have to tell who wins.
If the ballot is empty,print “Empty”(without quote).

Input Format:
First Line: ‘N’ Number of Operations
Next N lines correspond to an operation.
Operarion 1 has format :
Operation_type Name_of_candidate weight_of_voter

Constraint:
1<=N<=100000 1<=weight_of_voter<=100000000

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
 
int main()
{
map<string,int> mp;
vector<pair<string,int>> v;
int a,n;
cin>>n;
while(n–) {
cin>>a;
string str;
int num;
if(a==1) {
//cin.ignore();
cin>>str>>num;
v.push_back(pair<string,int>(str,num));
}
else if(a==2) {
if(!v.empty()) v.pop_back();
}
else if(a==3) {
int sm=999999;
string stt;
 
for(auto i: v) { 
if(sm>get<1>(i)) {
sm=get<1>(i);
stt=get<0>(i);
}
}
if(sm==999999) cout<<“Empty”<<endl;
else cout<<stt<<endl;
}
}
return 0;
}

untitled

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.