-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2828.cpp
More file actions
48 lines (44 loc) · 848 Bytes
/
2828.cpp
File metadata and controls
48 lines (44 loc) · 848 Bytes
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
// 2828. 사과 담기 게임
// 2019.10.03
// 반복문, 그리디 알고리즘
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
int left = 1; // 바구니 왼쪽끝 위치
int right = m; // 바구니 오른쪽끝 위치
int ans = 0;
int j;
cin >> j;
for (int i = 0; i < j; i++)
{
int k;
cin >> k;
if (k >= left && k <= right)
{
continue;
}
// 사과 떨어지는 위치가 바구니 오른쪽 값보다 큰 경우
else if (k > right)
{
ans += k - right;
left += k - right;
right += k - right;
}
// 사과 떨어지는 위치가 바구니 왼쪽 값보다 작은 경우
else if (k < left)
{
ans += left - k;
right -= left - k;
left -= left - k;
}
}
cout << ans << endl;
return 0;
}