-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort marks in descending order.txt
141 lines (99 loc) · 2.57 KB
/
sort marks in descending order.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Monk is a multi-talented person, and prepares results for his college in his free time. (Yes, he is still in love with his old college!) He gets a list of students with their marks. The maximum marks which can be obtained in the exam is 100.
The Monk is supposed to arrange the list in such a manner that the list is sorted in decreasing order of marks. And if two students have the same marks, they should be arranged in lexicographical manner.
Help Monk prepare the same!
Input format:
On the first line of the standard input, there is an integer N, denoting the number of students. N lines follow, which contain a string and an integer, denoting the name of the student and his marks.
Output format:
You must print the required list.
Constraints:
1 <= N <= 105
1 <= | Length of the name | <= 100
1 <= Marks <= 100
Sample Input
3
Eve 78
Bob 99
Alice 78
Sample Output
Bob 99
Alice 78
Eve 78
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<int, multiset<string>> marks_map;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int marks;
string name;
cin >> name >> marks;
marks_map[marks].insert(name);
}
auto cur_it = --marks_map.end(); // to reverse the map
while (true)
{
auto &students = (*cur_it).second;
auto &name = (*cur_it).first;
for(auto student : students)
{
cout << student << marks << endl;
}
}
if(cur_it == marks_map.begin()) break;
cur_it--;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
map<int, multiset<string>> marks_map;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int marks;
string name;
cin >> name >> marks;
marks_map[-1*marks].insert(name);
}
for(auto &marks_student_pr : marks_map)
{
auto &students = marks_student_pr.second;
int marks = marks_student_pr.second;
for(auto student: students)
{
cout << student << " " << -1*marks << endl;
}
}
}
#include <bits/stdc++.h>
using namespace std;
bool compare(pair<string,int>p , pair<string,int> q)
{
if(p.second == q.second)
{
return p.first < q.first;
}
return p.second > q.second;
}
int main()
{
int t;
cin >> t;
vector < pair<string,int> >v;
while(t--)
{
int n;
string s;
cin >> s >> n ;
v.push_back({s,n});
}
sort(v.begin() , v.end() ,compare);
for(int i = 0 ; i< v.size(); i++)
{
cout << v[i].first << " " << v[i].second << endl;
}
}