문제 링크: https://www.acmicpc.net/problem/14465
14465번: 소가 길을 건너간 이유 5
문제 농부 존의 농장에 원형 길이 있다고 했지만, 길은 그뿐만이 아니다. 그 옆에 일자형 길이 있는데, 1번부터 N번까지의 번호가 붙은 횡단보도 N (1 ≤ N ≤ 100,000)개로 이루어져 있다. 교통사고를 방지하기 위해 존은 각 횡단보도에 신호등을 설치해 놓았다. 그러던 어느 날, 강력한 뇌우로 인해 몇몇 신호등이 망가졌다. 존은 연속한 K개의 신호등이 존재하도록 신호등을 수리하고 싶다. 이번에도 우리가 존을 도와주자. 입력 첫 줄에 N, K, B
www.acmicpc.net
1. Sliding Window, Line Sweeping
처음 k개중에 작동하는 신호등의 갯수를 센 후에, 계속 k개 구간을 유지하면서 작동하는 신호등의 갯수를 update시켜주면 됨.
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
|
#include <bits/stdc++.h>
using namespace std;
int n, k, b, broken[100001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> b;
while(b--) {
int x;
cin >> x;
broken[x] = 1;
}
int ans = 0;
int cnt = 0;
for(int i = 1; i <= n; i++) {
if(i <= k) {
if(broken[i] == 0) cnt++;
continue;
}
if(broken[i] == 0) cnt++;
if(broken[i - k] == 0) cnt--;
ans = max(ans, cnt);
}
cout << k - ans << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
2. Prefix Sum
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
|
#include <bits/stdc++.h>
using namespace std;
int n, k, b, broken[100001], pSum[100001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> b;
while(b--) {
int x;
cin >> x;
broken[x] = 1;
}
for(int i = 1; i <= n; i++)
pSum[i] = pSum[i - 1] + (1 - broken[i]);
int ans = 1e9;
for(int i = k; i <= n; i++)
ans = min(ans, k - (pSum[i] - pSum[i - k]));
cout << ans << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'USACO > Silver' 카테고리의 다른 글
백준 14464 소가 길을 건너간 이유 4 (USACO February 2017 Silver 1번) (0) | 2020.04.19 |
---|---|
백준 14454 Secret Cow Code (USACO January 2017 Silver 3번) (0) | 2020.04.10 |
백준 14453 Hoof, Paper, Scissors (USACO January 2017 Silver 2번) (0) | 2020.04.04 |
백준 14452 Cow Dance Show (USACO January 2017 Silver 1번) (0) | 2020.04.03 |
백준 14172 Moocast (USACO December 2016 Silver 3번) (0) | 2020.04.03 |