시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
2 초 128 MB 125 31 22 23.158%

문제

지민이는 지뢰 사건에 의해 정문이에게 도망 다니고 있다. 그러던 중 N개의 펜스로 설치된 장애물을 만나게 되었다. 이 펜스들은 x축과 평행하고 i번째 펜스의 y좌표는 i이다. 펜스는 너무 높기 때문에 넘을 수 없고 돌아가는 방법만이 가능하다. 또한 현재 위치에서 x축의 양의 방향 또는 음의 방향으로 진행하던 중 펜스의 끝에 도달하면 y축과 평행하게 다음 펜스를 만날 때까지 내려가는 일을 반복한다.

   +-+-S-+             4번째 펜스
 +-+-+-+               3번째 펜스
     +-+-+-+           2번째 펜스
   +-+-+-+             1번째 펜스
 |=|=|=*=|=|=|         
-3-2-1 0 1 2 3 

위의 그림에서 S는 현재 지민이가 있는 위치이다. 그리고 지민이는 펜스를 잘 피하여 *로 표시된 탈출구까지 도달해야한다. 가능한 경로 중 하나는 일단 현재 위치에서 x축과 평행하게 양의 방향으로 1만큼 이동하면 펜스의 끝에 도달한다. 그리고 y축과 평행하게 아래로 내려가면 2번째 펜스에 도달하게 되고 또 다시 1만큼 x좌표가 양인 방향으로 이동하면 펜스의 끝에 도달하게 된다. 그리고 1만큼 내려간 뒤 x좌표가 음인 방향으로 2만큼 이동하면 탈출구에 도달하여 탈출할 수 있다.  y축을 따라 내려가는 길이는 어떤 식으로 내려가든 일정하기 때문에 고려하지 않는다고 하면 위의  경로로 이동한 것은 x축과 평행하게 총 4만큼 이동한 게 된다. 그리고 위의 경로가 이 예에서 최단 경로이다. 문제는 펜스의 위치가 주어졌을 때 최단경로의 길이를 구하는 것이다.

입력

첫째 줄에 펜스의 개수 N(1 ≤ N ≤ 50,000)과 지민이가 있는 x좌표 S(-100,000 ≤ S ≤ 100,000)가 공백으로 구분되어 주어진다.

두 번째 줄부터 N+1번째 줄까지 펜스의 시작 좌표 x와 끝 좌표 x가 공백으로 구분되어 주어진다. 좌표의 범위는 -100,000 이상 100,000 이하이다.

출력

탈출구로 도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5kIGZvcnRoIGluIHRoZSB4IGRpcmVjdGlvbiByZXF1aXJlZCB0byBnZXQgZnJvbSB0aGUgc3RhcnRpbmcgcG9pbnQgdG8gdGhlIGVuZGluZyBwb2ludCBieSB3YWxraW5nIGFyb3VuZCB0aGUgZmVuY2VzLiBUaGUgZGlzdGFuY2UgaW4gdGhlIHkgZGlyZWN0aW9uIGlzIG5vdCBjb3VudGVkLCBzaW5jZSBpdCBpcyBhbHdheXMgcHJlY2lzZWx5IE4uPFwvcD5cclxuXHJcbjxwPiZuYnNwOzxcL3A+XHJcbiIsImhpbnQiOiIiLCJvcmlnaW5hbCI6IjEiLCJodG1sX3RpdGxlIjoiMCIsInByb2JsZW1fbGFuZ19jb2RlIjoiXHVjNjAxXHVjNWI0In1d

출처

Olympiad > USA Computing Olympiad > 2004-2005 Season > USACO December 2004 Contest > Gold 2번

  • 문제의 오타를 찾은 사람: Acka
  • 문제를 번역한 사람: author6
  • 잘못된 조건을 찾은 사람: ntopia