CF575F.Bulbo

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Bananistan is a beautiful banana republic. Beautiful women in beautiful dresses. Beautiful statues of beautiful warlords. Beautiful stars in beautiful nights.

In Bananistan people play this crazy game – Bulbo. There’s an array of bulbs and player at the position, which represents one of the bulbs. The distance between two neighboring bulbs is 1. Before each turn player can change his position with cost |pos__new - pos__old|. After that, a contiguous set of bulbs lights-up and player pays the cost that’s equal to the distance to the closest shining bulb. Then, all bulbs go dark again. The goal is to minimize your summed cost. I tell you, Bananistanians are spending their nights playing with bulbs.

Banana day is approaching, and you are hired to play the most beautiful Bulbo game ever. A huge array of bulbs is installed, and you know your initial position and all the light-ups in advance. You need to play the ideal game and impress Bananistanians, and their families.

香蕉斯坦是一个美丽的香蕉共和国。美丽女子身着美丽长裙,美丽雕像矗立于美丽军阀之侧,美丽星辰闪耀于美丽夜空之中。

在香蕉斯坦,人们热衷于一种疯狂的游戏——灯泡(Bulbo)。游戏中有一排灯泡,玩家位于其中一个灯泡的位置上。相邻两个灯泡之间的距离为 11。每回合开始前,玩家可改变自身位置,代价为 ∣posnew−posold∣\lvert \text{pos}_{\text{new}} - \text{pos}_{\text{old}} \rvert。随后,一段连续的灯泡区间被点亮,玩家需支付等于其到最近亮起灯泡之距离的代价。之后,所有灯泡均熄灭。游戏目标是使总代价最小化。我告诉你,香蕉斯坦人整夜都在玩这些灯泡。

香蕉节即将来临,你受雇参与有史以来最盛大的灯泡游戏。一个超大灯泡阵列已被安装完毕,且你已预先获知自己的初始位置以及所有将要亮起的灯泡区间。你需要完美地进行游戏,以震撼香蕉斯坦人及其家人。

输入格式

The first line contains number of turns n and initial position x. Next n lines contain two numbers l__start and l__end, which represent that all bulbs from interval [l__start, l__end] are shining this turn.

  • 1 ≤ n ≤ 5000
  • 1 ≤ x ≤ 109
  • 1 ≤ l__start ≤ l__end ≤ 109

第一行包含回合数 nn 和初始位置 xx。接下来的 nn 行每行包含两个数 lstartl_{\text{start}} 和 lendl_{\text{end}},表示本轮中区间 [lstart, lend][l_{\text{start}},\,l_{\text{end}}] 内的所有灯泡均处于点亮状态。

  • 1 ≤ n ≤ 50001 ≤ n ≤ 5000
  • 1 ≤ x ≤ 1091 ≤ x ≤ 10^9
  • 1 ≤ lstart ≤ lend ≤ 1091 ≤ l_{\text{start}} ≤ l_{\text{end}} ≤ 10^9

输出格式

Output should contain a single number which represents the best result (minimum cost) that could be obtained by playing this Bulbo game.

输出应为一个数字,表示通过玩此 Bulbo 游戏所能获得的最佳结果(最小代价)。

输入输出样例

  • 输入#1

    5 4
    2 7
    9 16
    8 10
    9 17
    1 6

    输出#1

    8

说明/提示

Before 1. turn move to position 5

Before 2. turn move to position 9

Before 5. turn move to position 8

第 1 次移动前,移动到位置 5

第 2 次移动前,移动到位置 9

第 5 次移动前,移动到位置 8

输入解题思路,AI测评打分。不知道怎么写?

首页