Skip to content

Commit a0adc35

Browse files
committed
Solved problem 84D from codeforces
1 parent ed87b8f commit a0adc35

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

CodeForces/84D. Doctor.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Idea:
3+
- We can reduce `k` if we try to remove animals from the smallest number of
4+
examinations to the biggest.
5+
- Then we can reduce `k` even more, we know that in the current state we
6+
cannot remove any animal, but we can remove some examinations from the
7+
animals by divide the remaining `k` by the remaining animals.
8+
- Finally, the remaining `k` will be less than 10^5, so we can brute force
9+
it.
10+
*/
11+
12+
#include <bits/stdc++.h>
13+
14+
using namespace std;
15+
16+
int const N = 1e5 + 10;
17+
int n, a[N];
18+
long long k, to, tt;
19+
vector<int> v;
20+
map<int, int> mp;
21+
queue<pair<int, int> > q;
22+
23+
int main() {
24+
scanf("%d %lld", &n, &k);
25+
for(int i = 0; i < n; ++i) {
26+
scanf("%d", a + i);
27+
v.push_back(a[i]);
28+
++mp[a[i]];
29+
to += a[i];
30+
}
31+
32+
if(to < k) {
33+
puts("-1");
34+
return 0;
35+
}
36+
37+
sort(v.begin(), v.end());
38+
v.resize(unique(v.begin(), v.end()) - v.begin());
39+
40+
int cnt = n;
41+
for(int i = 0, prv = 0; i < v.size(); ++i) {
42+
if(k >= 1ll * (v[i] - prv) * cnt) {
43+
k -= 1ll * (v[i] - prv) * cnt;
44+
tt += (v[i] - prv);
45+
cnt -= mp[v[i]];
46+
} else
47+
break;
48+
49+
prv = v[i];
50+
}
51+
52+
for(int i = 0; i < n; ++i)
53+
a[i] -= tt, a[i] = max(0, a[i]);
54+
55+
if(cnt == 0)
56+
return 0;
57+
58+
int rem = k / cnt;
59+
k -= 1ll * rem * cnt;
60+
61+
for(int i = 0; i < n; ++i)
62+
if(a[i] > 0)
63+
a[i] -= rem, q.push({a[i], i + 1});
64+
65+
for(int i = 0; i < k; ++i) {
66+
pair<int, int> p = q.front();
67+
q.pop();
68+
if(--p.first == 0)
69+
continue;
70+
q.push(p);
71+
}
72+
73+
while(!q.empty()) {
74+
printf("%d ", q.front().second);
75+
q.pop();
76+
}
77+
puts("");
78+
79+
return 0;
80+
}

CodeForces/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- [75D. Big Maximum Sum](http://codeforces.com/contest/75/problem/D)
4646
- [80A. Panoramix's Prediction](http://codeforces.com/problemset/problem/80/A)
4747
- [81A. Plug-in](http://codeforces.com/contest/81/problem/A)
48+
- [84D. Doctor](http://codeforces.com/contest/84/problem/D)
4849
- [94D. End of Exams](http://codeforces.com/contest/94/problem/D)
4950
- [96A. Football](http://codeforces.com/problemset/problem/96/A)
5051
- [96B. Lucky Numbers (easy)](http://codeforces.com/problemset/problem/96/B)

0 commit comments

Comments
 (0)