[Silver V] 사과 담기 게임 - 2828
비교적 최근에 푼 문제 중에서는 쉽게 풀린 문제였습니다.
front, back 기준을 잡아두고 계산하면 생각보다 쉽게 진행됩니다.
[Silver V] 사과 담기 게임 - 2828 풀이코드
더보기
더보기
#include <bits/stdc++.h>
using namespace std;
int n, m, j, ret, target, front, back;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
cin >> j;
int front = 1;
int back = m;
for (int i = 0; i < j; i++)
{
cin >> target;
if (target < front)
{
ret += abs(front - target);
front = target;
back = front + (m - 1);
}
else if (target > back)
{
ret += abs(target - back);
back = target;
front = back - (m - 1);
}
else
{
continue;
}
}
cout << ret << '\n';
return 0;
}
[Silver V] 사과 담기 게임 - 2828
비교적 최근에 푼 문제 중에서는 쉽게 풀린 문제였습니다.
front, back 기준을 잡아두고 계산하면 생각보다 쉽게 진행됩니다.
[Silver V] 사과 담기 게임 - 2828 풀이코드
더보기
더보기
#include <bits/stdc++.h>
using namespace std;
int n, m, j, ret, target, front, back;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
cin >> j;
int front = 1;
int back = m;
for (int i = 0; i < j; i++)
{
cin >> target;
if (target < front)
{
ret += abs(front - target);
front = target;
back = front + (m - 1);
}
else if (target > back)
{
ret += abs(target - back);
back = target;
front = back - (m - 1);
}
else
{
continue;
}
}
cout << ret << '\n';
return 0;
}