Codeforces 915B (Browser)

题目时空门

Luba is surfing the Internet. She currently has n opened tabs in her browser, indexed from 1 to n from left to right. The mouse cursor is currently located at the pos-th tab. Luba needs to use the tabs with indices from l to r (inclusive) for her studies, and she wants to close all the tabs that don’t belong to this segment as fast as possible.

Each second Luba can either try moving the cursor to the left or to the right (if the cursor is currently at the tab i, then she can move it to the tab max(i - 1, a) or to the tab min(i + 1, b)) or try closing all the tabs to the left or to the right of the cursor (if the cursor is currently at the tab i, she can close all the tabs with indices from segment [a, i - 1] or from segment [i + 1, b]). In the aforementioned expressions a and b denote the minimum and maximum index of an unclosed tab, respectively. For example, if there were 7 tabs initially and tabs 1, 2 and 7 are closed, then a = 3, b = 6.

What is the minimum number of seconds Luba has to spend in order to leave only the tabs with initial indices from l to r inclusive opened?

Input
The only line of input contains four integer numbers n, pos, l, r (1 ≤ n ≤ 100, 1 ≤ pos ≤ n, 1 ≤ l ≤ r ≤ n) — the number of the tabs, the cursor position and the segment which Luba needs to leave opened.

Output
Print one integer equal to the minimum number of seconds required to close all the tabs outside the segment [l, r].

Examples
inputCopy
6 3 2 4
output
5
inputCopy
6 3 1 3
output
1
inputCopy
5 2 1 5
output
0
Note
In the first test Luba can do the following operations: shift the mouse cursor to the tab 2, close all the tabs to the left of it, shift the mouse cursor to the tab 3, then to the tab 4, and then close all the tabs to the right of it.

In the second test she only needs to close all the tabs to the right of the current position of the cursor.

In the third test Luba doesn’t need to do anything.

题意:

路霸在网上冲浪,她打开了n个网页,她现在的鼠标停留在pos的位置上,现在有用的网页在区间【l,r】上,她现在想把没用的(也就是不在区间上的网页全部关掉)
她现在的步骤有:
1.左移
2.右移
3.将左边的网站全部关掉
4.将右边的网站全部关掉
每个步骤的权值为1
问你把那些不再区间内的网页全部关掉的最小的权值和为多少?

思路:

这个题目其实很简单,读懂了题意基本就会了。这无非就是两个区间的问题。有四种情况:
1.【l,r】是完全包含【1,n】的区间,这时候很明显输出 0//这里其实就是两个一样的区间我只是扩充上了负数
2. l<=1,这样只需要把右边的那些网页关掉就好 最小权值就是 abs(r-pos)+1//abs(r-pos)代表给出的区间移动到最右边 1代表将右遍无用的所有的网站关闭。
3. r>=n,这样只需把左边的那些网页关掉就好 最小权值就是 **abs(pos-l)+1 **//abs(pos-l)代表给出的区间移动到最左边 1代表将左边无用的所有的网站关闭。
4. 【l,r】是区间【1,n】的真子集,这样你需要判断她先移动到最左端l权值小 还是移动到最右端n权值小 min(abs(pos-l),abs(r-pos))
然后无论你移动到最左端最右端 你都需要将鼠标指针移动到相反的那端,所以要加上 r-l 同时你还进行了两次删除操作 再加 2
所以最后的最小权值是 min(abs(pos-l),abs(r-pos))+2+r-l

详情见代码:

//有n个网页节目,鼠标现在停留在pos位置上,问你将区间【l,r】以外的删除最少需要经过多少次
//可以进行的操作有:左移 右移 关闭左边所有网页 关闭右边所有的网页
//每个操作的权值都是1
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>

using namespace std;
const int N=1e5+10;


int main()
{
    ios::sync_with_stdio(false);
    int n,pos,l,r;
    while(cin>>n>>pos>>l>>r)
    {
        if(l<=1&&r>=n)//所有的数包含在给你的区间内
            cout<<"0"<<endl;
        else  if(l<=1&&r<n)//区间左端完全包含1
        {
            cout<<abs(r-pos)+1<<endl;
        }
        else if(r>=n&&l>1)//区间右边完全包含n
        {
            cout<<abs(pos-l)+1<<endl;
        }
        else if(r<n&&l>1)//区间【l,r】是【1,n】的真子集
        {
            cout<<min(abs(pos-l),abs(r-pos))+2+r-l<<endl;
        }

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Puppet__/article/details/79347710