-
Notifications
You must be signed in to change notification settings - Fork 0
/
practice2.cpp
511 lines (453 loc) · 10.7 KB
/
practice2.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <iomanip>
using namespace std;
#define int long long
typedef long long ll;
#define FOR(a, b, c) for (int a = b; a < (int)c; ++a)
#define FORN(a, b, c) for (int a = b; a >= (int)c; --a)
#define MAX INT_MAX
#define MIN INT_MIN
#define MAXll LLONG_MAX
#define ii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define print(arr) \
for (auto i : arr) \
cout << i << " ";
#define all(a) a.begin(), a.end()
#define rev(a) a.rbegin(), a.rend()
const ll MOD = 1e9 + 7;
class EmployeeData
{
private:
int employee_id;
int employee_salary;
int timestamp_of_joining;
int timestamp_of_leaving;
string employee_name;
string employee_job;
public:
EmployeeData(int &id, string &name, string &job, int &salary, int &join_time, int &leave_time)
: employee_id(id),
employee_name(name),
employee_job(job),
employee_salary(salary),
timestamp_of_joining(join_time),
timestamp_of_leaving(leave_time)
{
}
int getEmployeeId()
{
return employee_id;
}
string getEmployeeName()
{
return employee_name;
}
string getEmployeeJob()
{
return employee_job;
}
int getEmployeeSalary()
{
return employee_salary;
}
int getEmployeeJoinTime()
{
return timestamp_of_joining;
}
int getEmployeeLeaveTime()
{
return timestamp_of_leaving;
}
};
struct parsed_data
{
vector<EmployeeData *> employees;
};
// file: employee_salary
// cols: id,name,job,salary,join_time,leave_time
// types: int,str,str,int,int,int
parsed_data parse_stdin()
{
struct parsed_data employee_data;
string line;
while (getline(cin, line))
{
stringstream ss(line);
int idx = 0;
string field;
int id, salary, join_time, leave_time;
string name, job;
while (getline(ss, field, ','))
{
switch (idx)
{
case 0:
id = stoi(field);
break;
case 1:
name = field;
break;
case 2:
job = field;
break;
case 3:
salary = stoi(field);
break;
case 4:
join_time = field == "" ? 0 : stoi(field);
break;
case 5:
leave_time = field == "" ? time(nullptr) : stoi(field);
break;
}
idx++;
}
EmployeeData *employee = new EmployeeData(id, name, job, salary, join_time, leave_time);
employee_data.employees.push_back(employee);
}
return employee_data;
};
int earned_more_than_30K(parsed_data const &data)
{
int salaries_more_than_30K = 0;
for (EmployeeData *employee : data.employees)
{
int salary = employee->getEmployeeSalary();
if (30000 < salary)
{
salaries_more_than_30K++;
}
}
return salaries_more_than_30K;
}
string held_longest_job(parsed_data const &data)
{
string longest_job_employee;
int longest_job_duration = 0;
for (EmployeeData *employee : data.employees)
{
int time_of_join = employee->getEmployeeJoinTime();
int time_of_leave = employee->getEmployeeLeaveTime();
int duration_of_job = time_of_leave - time_of_join;
if (longest_job_duration < duration_of_job)
{
longest_job_employee = employee->getEmployeeName();
longest_job_duration = duration_of_job;
}
}
return longest_job_employee;
}
bool salary_comparator(EmployeeData *employee_1, EmployeeData *employee_2)
{
int salary_1 = employee_1->getEmployeeSalary();
int salary_2 = employee_2->getEmployeeSalary();
return salary_1 > salary_2;
}
string second_highest_salary(parsed_data const &data)
{
vector<EmployeeData *> employee_data = data.employees;
sort(employee_data.begin(), employee_data.end(), salary_comparator);
EmployeeData *second_highest_earner = employee_data[1];
string employee_name = second_highest_earner->getEmployeeName();
return employee_name;
}
// ---------------------------------------------------------------------------------------------
int convert_to_decimal(vector<int> a)
{
int num = 0;
int k = 0;
for (int i = 0; i < a.size(); i++)
{
if (i % 2 == 0)
k = 1 << i;
else
k = (1 << i) * -1;
num = num + a[i] * k;
}
return num;
}
void convert_to_neg(int a, vector<int> &ans)
{
if (a == 0)
{
ans.push_back(0);
return;
}
while (a != 0)
{
int r = a % (-2);
a = a / -2;
if (r < 0)
{
r = r + (2);
a = a + 1;
}
ans.push_back(r);
}
}
void ques()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int k = convert_to_decimal(a);
vector<int> ans;
convert_to_neg(ceil(k / 2.0), ans);
cout << endl;
for (int i = 0; i < ans.size(); i++)
{
cout << ans[i] << " ";
}
}
// -----------------------------------------------------------------
int solve3(vector<int> &a)
{
priority_queue<int, vector<int>, greater<int> > pq;
for (int i = 0; i < a.size(); i++)
{
pq.push(a[i]);
}
int cost = 0;
while (pq.size() > 1)
{
int b = pq.top();
pq.pop();
int c = pq.top();
pq.pop();
cost = cost + (b + c);
pq.push(b + c);
}
return cost;
}
void ques2()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << solve3(a) << endl;
}
// -----------------------------------------------------------------
int solution(const string &s, const vector<int> &c)
{
int left = 0, right = c.size();
while (left <= right)
{
const int mid = (left + right) >> 1;
unordered_set<int> all(c.begin(), c.begin() + mid);
vector<char> have(26, -1);
bool mark = true;
for (int i = 0, temp = -1; mark && i < s.length(); ++i)
{
int p = s[i] - 'a';
if (have[p] > temp)
{
mark = false;
}
have[p] = i;
if (all.count(i + 1))
{
temp = i;
}
}
if (mark)
{
right = mid - 1;
}
else
{
left = mid + 1;
}
}
return ++right > c.size() ? (-1) : right;
}
// -----------------------------------------------------------------
int func(vector<int> &a, int k)
{
sort(a.begin(), a.end());
int n = a.size();
int lef = n / 3 - 1;
int rig = 2 * n / 3;
int answer = 0;
answer = a[rig] - a[lef];
for (int check = 1;; check++)
{
if (rig + check == n)
{
answer += k / (n / 3);
break;
}
else
{
int tomodify = (a[lef] - a[lef - check]) + (a[rig + check] - a[rig]);
if (tomodify * check <= k)
{
k -= tomodify * check;
answer += tomodify;
}
else
{
answer += k / check;
break;
}
a[lef] = a[lef - check];
a[rig] = a[rig + check];
}
}
return answer;
}
// -----------------------------------------------------------------
void solve2()
{
int n, k;
cin >> n >> k;
vector<int> a(n);
for (auto &i : a)
{
cin >> i;
}
cout << func(a, k) << endl;
}
// ----------------------------------------------------------------------------------------
int solution(vector<int> &Y)
{
int n = Y.size();
int top = *max_element(all(Y));
vector<int> holder(top + 1, 0);
vector<int> holder1(top * 2, 0);
for (int i = 0; i < n - 1; i++)
{
int start = Y[i], end = Y[i + 1];
if (end < start)
{
while (end < start)
{
holder[start]++;
holder1[(start * 2) - 1]++;
start--;
}
}
else
{
while (start < end)
{
holder[start]++;
holder1[(start * 2) + 1]++;
start++;
}
}
}
holder[Y[n - 1]]++;
int total = 0, ans = 0;
for (int i = 0; i <= top; i++)
{
if (total < holder[i])
{
total = holder[i];
ans = i;
}
}
for (int i = 0; i < top * 2; i++)
{
if (total < holder1[i])
{
total = holder1[i];
ans = i;
}
}
return total;
}
void solve()
{
int n;
cin >> n;
vector<int> Y(n);
for (int &i : Y)
cin >> i;
unordered_map<int, int> common;
vector<vector<int> > intervals;
FOR(i, 1, n)
{
int l = min(Y[i - 1], Y[i]);
int r = max(Y[i - 1], Y[i]);
intervals.pb({l, r});
if (i != n - 1)
common[Y[i]]++;
}
sort(all(intervals));
int count = 1, max_count = 0;
int start = intervals[0][0], end = intervals[0][1];
vector<vector<int> > cnts;
FOR(i, 1, intervals.size())
{
vector<int> curr_interval = intervals[i];
if (!(end < curr_interval[0] || curr_interval[1] < start))
{
start = max(start, curr_interval[0]);
end = min(end, curr_interval[1]);
max_count = max(++count, max_count);
}
else
{
start = max(start, curr_interval[0]);
end = min(end, curr_interval[1]);
cnts.pb({count, start, end});
count = 1;
}
}
cnts.pb({count, start, end});
int ans = 0;
for (auto v : cnts)
{
int count = v[0],
start = v[1],
end = v[2];
int min_cnt = 1e5;
for (auto [point, cnt] : common)
{
if (start <= point && point <= end)
min_cnt = min(min_cnt, cnt);
}
if (min_cnt == 1e5)
min_cnt = 0;
ans = max(ans, count - min_cnt);
}
cout << ans << "\n";
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
while (t--)
{
solve2();
cout << "\n";
}
}