-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeForces-2A.cpp
More file actions
114 lines (88 loc) · 1.78 KB
/
CodeForces-2A.cpp
File metadata and controls
114 lines (88 loc) · 1.78 KB
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
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MOD = 1001;
const ll B = 23;
ll pot[34];
string nome[1000];
ll pontos[1000];
pair<string, ll> table[1001];
bool venc[1001];
void pre() {
pot[0] = 1;
for (int i = 1; i < 34; ++i) {
pot[i] = (pot[i - 1] * B) % MOD;
}
}
ll mh(string z) {
ll id = 0;
for (int i = 0; z[i] != '\0'; ++i) {
ll x = (pot[i] * (z[i] - 'a')) % MOD;
id = (id + x) % MOD;
}
return id;
}
int main (){
ios::sync_with_stdio(0); cin.tie(nullptr);
pre();
int n; cin >> n;
for (int i = 0; i < MOD; ++i) {
table[i] = make_pair("", -1);
}
for (int i = 0; i < n; ++i) {
cin >> nome[i] >> pontos[i];
ll hID = mh(nome[i]);
while (table[hID].first != "" && table[hID].first != nome[i]) {
hID++;
hID %= MOD;
}
if (table[hID].first == "") {
table[hID] = make_pair(nome[i], pontos[i]);
} else {
table[hID].second += pontos[i];
}
}
ll ans = INT_MIN;
for (int i = 0; i < MOD; ++i) {
if (table[i].first != "") {
ans = max(ans, table[i].second);
}
}
int cnt = 0;
for (int i = 0; i < MOD; ++i) {
if (table[i].first != "") {
if (table[i].second == ans) {
venc[i] = 1;
++cnt;
}
}
}
if (cnt == 1) {
for (int i = 0; i < MOD; ++i) {
if (table[i].second == ans) {
cout << table[i].first << endl;
return 0;
}
}
}
for (int i = 0; i < MOD; ++i) {
table[i] = make_pair("", -1);
}
for (int i = 0; i < n; ++i) {
ll hID = mh(nome[i]);
while (table[hID].first != "" && table[hID].first != nome[i]) {
hID++;
hID %= MOD;
}
if (table[hID].first == "") {
table[hID] = make_pair(nome[i], pontos[i]);
} else {
table[hID].second += pontos[i];
}
if (table[hID].second >= ans && venc[hID]) {
cout << table[hID].first << endl;
return 0;
}
}
return 0;
}