Can anyone help me in identifying my error in CVOTE

my code:CodeChef: Practical coding for everyone
the problem:CodeChef: Practical coding for everyone

my logic was this:
1.make 3 maps;one to store names and country,one for chefs and votes and another for country and votes.
i then iterate starting from the last element in the map and check if the 2nd last element has the same number of votes and so forth as the last one as we consider the country with the lexicographically smaller name among them to be a winner.
It passes all example cases however it fails when i submit it.

Using comparator will make it much more easier. Just sort the maps and print. Here’s my code-

#include<bits/stdc++.h>
#define ll long long int
#define br break
#define pb push_back
#define endl "\n"
using namespace std;  
bool comp1(const pair<string,ll> &a, const pair<string,ll> &b){ 
	  if(a.second==b.second){
		  return a.first<b.first;
	  }
	  return a.second>b.second;
} 
void solve(){
     //input
    ll n,m;
    cin>>n>>m;
    map<string,string>sem;
    while(n--){
		string s,s1;
		cin>>s>>s1;
		sem.insert({s,s1});  
	}
	map<string,ll>mp,mp1;
	while(m--){
		string str;
		cin>>str;
		mp[str]++; 
		mp1[sem[str]]++;
	}
	vector<pair<string,ll>>vp;
	for(auto it:mp1){
		vp.pb({it.first,it.second});
	}
	sort(vp.begin(),vp.end(),comp1);
	vector<pair<string,ll>>v1;
	for(auto it:mp){
		v1.pb({it.first,it.second});
	}
    sort(v1.begin(),v1.end(),comp1);
	cout<<vp[0].first<<endl<<v1[0].first<<endl;
}
int32_t main()
{	
   ll t=1;
   //cin>>t;
   while(t--){
   	  solve();
   }
}
1 Like

what does the “<” do,is it just a comparison?

Yes, it returns the lexicographically smaller string, i.e. by simple comparision

1 Like

thankyou :slight_smile: