시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
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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0IGZyb20gdGhlIHN0YXJ0aW5nIHBvaW50IHRvIHRoZSBlbmRpbmcgcG9pbnQgYnkgd2Fsa2luZyBhcm91bmQgdGhlIGZlbmNlcy4gVGhlIGRpc3RhbmNlIGluIHRoZSB5IGRpcmVjdGlvbiBpcyBub3QgY291bnRlZCwgc2luY2UgaXQgaXMgYWx3YXlzIHByZWNpc2VseSBOLjxcL3A+XHJcblxyXG48cD4mbmJzcDs8XC9wPlxyXG4iLCJoaW50IjoiIiwib3JpZ2luYWwiOiIxIiwicHJvYmxlbV9sYW5nX2NvZGUiOiJcdWM2MDFcdWM1YjQifV0=

출처

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

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