※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 15001번 문제인 Frog Leaps이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/15001 

 

15001번: Frog Leaps

It is a little known secret that frogs also like to participate in the BAPC. However, to reach Amsterdam they will need to cross lots of rivers. Fortunately, the frog is in good condition and can jump as far as he wants to go. However, jumping a distance o

www.acmicpc.net

모든 x의 값이 정수이므로 모든 stop 사이의 거리는 1 이상임을 알 수 있다. 이 조건 하에서, 개구리는 항상 가장 가까운 다음 stop까지 뛰는것을 반복해 강을 건너는 것이 에너지를 최소로 소비해 건너는 방법이 된다. 증명은 단순 계산이므로 생략한다.

 

그러므로 모든 두 인접한 stop 사이의 거리의 제곱의 합을 계산하는 것으로 문제를 해결할 수 있다.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;
typedef long long ll;

int N;
ll ans, old;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> N >> old; N--;
	while (N--) {
		ll x; cin >> x;
		ans += (x - old) * (x - old);
		old = x;
	}

	cout << ans;
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 26008 // C++] 해시 해킹  (0) 2022.12.26
[BOJ 26006 // C++] K-Queen  (0) 2022.12.26
[BOJ 26773 // C++] Deski kontratakują  (0) 2022.12.26
[BOJ 9443 // C++] Arrangement of Contest  (0) 2022.12.26
[BOJ 17598 // C++] Animal King Election  (0) 2022.12.26

+ Recent posts