Thứ Ba, 22 tháng 1, 2019

Một bài sử sụng Map hay

Đề bài:
A simple question again..!
These are the people from nirmaLand who have shown utmost dedication towards Competitve Programming..!
You will be provided with name of the coder and no. of hours he/she spent on coding.
You need to output top 3 names of the coders who spent most time in coding.
Note: Time (no. of hours) given for all coders will be unique. More technically, no two coders will have same amount of time.
See the sample case for better understanding.

Input format:
First line consist of total number of coders, let's say 'n'.
Each of the next n lines consist of a string, let's say 'N' (name of coder) and a number let's say, 'T' (time).

Output format:
Output names of top 3 coders that spent maximum time in coding.

Constraints:
3 <= n <= 100
1 <= |N| <= 10
1 <= T <= 1000
SAMPLE INPUT
7
Darshan 78
Harshad 90
Jaimin 87
Nirav 88
Hardik 1
Fenil 70
Lovlin 5
SAMPLE OUTPUT
Harshad
Nirav
Jaimin
Explanation
Harshad spent 90 hours in coding, which is maximum, so his name is at first.
After Harshad, Nirav spent 88 hours in coding, which is maximum, so his name is second.
After Nirav, Jaimin spent 87 hours in coding, which is maximum, so his name comes third.

Lời giải 1:
#include<bits/stdc++.h>
using namespace std;
string s,kq1;
int d,n,maax;
int main(){
   cin>>n;
   map<string,int> m;
   while(n--){
      cin>>s>>d;
      m[s]=d;
   }
   map<string,int>::iterator it;
   for(it=m.begin();it!=m.end();it++){
      if(it->second>maax) {
        maax=it->second;
        kq1=it->first;
      }
   }
   cout<<kq1<<'\n';
   maax=0;
   m[kq1]=0;
   for(it=m.begin();it!=m.end();it++){
      if(it->second>maax) {
        maax=it->second;
        kq1=it->first;
      }
   }
   cout<<kq1<<'\n';
   maax=0;
   m[kq1]=0;
   for(it=m.begin();it!=m.end();it++){
      if(it->second>maax) {
        maax=it->second;
        kq1=it->first;
      }
   }
   cout<<kq1<<'\n';
   return 0;
}

Lời giải 2:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,flag=0;
cin>>n;
map<int,string>a;
for(int i=0;i<n;i++)
{
string s;
int d;
cin>>s>>d;
a.insert(pair<int,string>(d,s));
}
map<int,string>::reverse_iterator itr;
for (itr=a.rbegin(); itr!=a.rend(); ++itr)
{
if(flag==3)
break;
flag++;
cout<<itr->second << '\n';

}
}

Không có nhận xét nào:

Đăng nhận xét

Bài G - Educatioal Round 62

Đề bài: Bạn được cho 1 đồ thị vô hướng đặc biệt. Nó bao gồm $2n$ đỉnh được đánh số từ 1 đến 2n. Dưới đây là một số đặc tính của đồ thị: + ...