시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
2 초 128 MB 90 18 14 21.212%

문제

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

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

위의 그림에서 S는 현재 지민이가 있는 위치이다. 그리고 지민이는 펜스를 잘 피하여 *로 표시된 탈출구까지 도달해야한다. 가능한 경로 중 하나는 일단 현재 위치에서 x축과 평행하게 양의 방향으로 1만큼 이동하면 펜스의 끝에 도달한다. 그리고 y축과 평행하게 아래로 내려가면 3번째 펜스에 도달하게 되고 또 다시 1만큼 x좌표가 양인 방향으로 이동하면 펜스의 끝에 도달하게 된다. 그리고 2만큼 내려간 뒤 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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HRoZSBzdGFydGluZyBwb2ludCB0byB0aGUgZW5kaW5nIHBvaW50IGJ5IHdhbGtpbmcgYXJvdW5kIHRoZSBmZW5jZXMuIFRoZSBkaXN0YW5jZSBpbiB0aGUgeSBkaXJlY3Rpb24gaXMgbm90IGNvdW50ZWQsIHNpbmNlIGl0IGlzIGFsd2F5cyBwcmVjaXNlbHkgTi48XC9wPlxyXG5cclxuPHA+Jm5ic3A7PFwvcD5cclxuIiwiaGludCI6IiIsIm9yaWdpbmFsIjoiMSIsInByb2JsZW1fbGFuZ19jb2RlIjoiXHVjNjAxXHVjNWI0In1d

출처

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

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