Skip to content

Commit 5bedb7f

Browse files
committed
Solved problems 1077[E, F1] from codeforces
1 parent 152b206 commit 5bedb7f

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Idea:
3+
- Try all possible starting values from 1 to `max(a[i])`.
4+
- This solution work in ~ `20 * 2*10^5`.
5+
- To figure why, the smallest starting number is 1
6+
and in each iteration it should be multiplied by 2,
7+
so in 18 steps it will reach 262144 which is greater than
8+
the number of elements in the array.
9+
*/
10+
11+
#include <bits/stdc++.h>
12+
13+
using namespace std;
14+
15+
int const N = 2e5 + 1;
16+
int n, a[N], fr[N];
17+
vector<int> all;
18+
19+
int main() {
20+
scanf("%d", &n);
21+
for(int i = 0; i < n; ++i)
22+
scanf("%d", a + i), all.push_back(a[i]);
23+
24+
sort(all.begin(), all.end());
25+
all.resize(unique(all.begin(), all.end()) - all.begin());
26+
for(int i = 0; i < n; ++i)
27+
a[i] = lower_bound(all.begin(), all.end(), a[i]) - all.begin(),
28+
++fr[a[i]];
29+
all.clear();
30+
31+
for(int i = 0; i < N; ++i)
32+
if(fr[i] != 0)
33+
all.push_back(fr[i]);
34+
sort(all.begin(), all.end());
35+
36+
int res = 0, tmp, idx, cur;
37+
for(int i = 1; i <= all.back(); ++i) {
38+
tmp = 0, idx = -1, cur = i;
39+
while(true) {
40+
idx = lower_bound(all.begin() + idx + 1, all.end(), cur) - all.begin();
41+
if(idx == all.size())
42+
break;
43+
tmp += cur;
44+
cur += cur;
45+
}
46+
res = max(res, tmp);
47+
}
48+
printf("%d\n", res);
49+
50+
return 0;
51+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Idea:
3+
- Dynamic Programming
4+
- dp[idx][prv][rem] represents:
5+
idx: the current index
6+
prv: how many elements discarded before
7+
rem: the remaining elements to take
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
int const N = 2e2 + 1;
15+
int n, k, x, a[N];
16+
long long dp[N][N][N];
17+
18+
long long rec(int idx, int prv, int rem) {
19+
if(rem < 0 || prv >= k)
20+
return -1e15;
21+
if(idx == n)
22+
return rem == 0 ? 0 : -1e15;
23+
24+
long long &ret = dp[idx][prv][rem];
25+
if(ret != -1)
26+
return ret;
27+
ret = -1e15;
28+
29+
ret = max(ret, rec(idx + 1, prv + 1, rem));
30+
ret = max(ret, rec(idx + 1, 0, rem - 1) + a[idx]);
31+
32+
return ret;
33+
}
34+
35+
int main() {
36+
scanf("%d %d %d", &n, &k, &x);
37+
for(int i = 0; i < n; ++i)
38+
scanf("%d", a + i);
39+
40+
memset(dp, -1, sizeof dp);
41+
long long res = rec(0, 0, x);
42+
43+
if(res <= 0)
44+
puts("-1");
45+
else
46+
printf("%lld\n", res);
47+
48+
return 0;
49+
}

CodeForces/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@
575575
- [1077B. Disturbed People](http://codeforces.com/contest/1077/problem/B)
576576
- [1077C. Good Array](http://codeforces.com/contest/1077/problem/C)
577577
- [1077D. Cutting Out](http://codeforces.com/contest/1077/problem/D)
578+
- [1077E. Thematic Contests](http://codeforces.com/contest/1077/problem/E)
579+
- [1077F1. Pictures with Kittens (easy version)](http://codeforces.com/contest/1077/problem/F1)
578580
- [1079A. Kitchen Utensils](http://codeforces.com/contest/1079/problem/A)
579581
- [1079B. Personalized Cup](http://codeforces.com/contest/1079/problem/B)
580582
- [1079C. Playing Piano](http://codeforces.com/contest/1079/problem/C)

0 commit comments

Comments
 (0)