[BOJ] 3020 개똥벌레

Time Lapse :28min 43sec

3020.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
#include <iostream>
#include <algorithm>
#include <queue>
#include <memory.h>
using namespace std;
int n, h, num;
int ans = 987654321, cnt = 0;
int up[500001];
int down[500001];
int main() {
scanf("%d %d",&n, &h);
for(int i = 0; i < n; i++) {
scanf("%d",&num);
i&1 ? ++down[num] : ++up[num];
}
for(int i = h - 1; i > 0; i--) {
up[i] += up[i+1];
down[i] += down[i+1];
}
int l = 0, r = n, m;
while(l < r) {
m = (l + r) / 2;
int cur = 0;
for(int i = 1; i <= h; ++i)
if(up[i] + down[h+1-i] <= m)
++cur;
if(ans > m && cur) {
ans = m;
cnt = cur;
}
cur ? r = m : l = m +1;
}
printf("%d %d",ans,cnt);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/30/PS/BOJ/3020/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.