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 12 and 7 are closed, then a = 3b = 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 nposlr (1 ≤ n ≤ 1001 ≤ pos ≤ n1 ≤ 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].

Example
Input
6 3 2 4
Output
5
Input
6 3 1 3
Output
1
Input
5 2 1 5
Output
0

题目意思是有 1- n 个网页,一开始在 pos 处,有四个操作:移动到左边的网页,移动到右边的网页,关掉左边所有的网页,关掉右边所有的网页。把除了 [ l , r ] 区间的网页都关闭。求最少的操作数。

这题我一开始是很多分类讨论,如下:

如果 [ l , r ] 和 [ 1 , n ] 重合,不用操作;

如果 pos <= l , 出发点在 区间左边,这时候还要考虑 l == 1 和 r == n 的情况

如果 pos >= r , 出发点在 区间右边,这时候还要考虑 l == 1 和 r == n 的情况

还有 pos 在 [ l , r ] 区间中间的情况,同样要考虑 l == 1 和 r == n 的情况

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std ;

int main(){
	int n , pos , l , r ;
	while( ~scanf( "%d%d%d%d" , &n , &pos , &l , &r ) ){
		if( l == 1 && r == n ) printf( "0\n" ) ;
		else if( pos <= l ) printf( "%d\n" , r == n ? l-pos+1 : ( l == 1 ) ? r-pos+1 : l-pos+1 + r-l + 1 ) ;
		else if( pos >= r ) printf( "%d\n" , l == 1 ? pos-r+1 : ( r == n ) ? pos-l+1 : pos-r+1 + r-l + 1 ) ;
		else{
			if( l == 1 ) printf( "%d\n" , r-pos+1 ) ;
			else if( r == n ) printf( "%d\n" , pos-l+1 ) ;
			else printf( "%d\n" , min( pos-l , r-pos ) + 1 + r-l + 1 ) ;
		} 
	}
}

但是其实,从上面我们可以看出,每次都要判断 l == 1和 r == n ,可以简化成四种情况: 

l == 1 && r == n 

l == 1 

r == n 

l != 1 && r != r 

其实,画一画就知道,四种情况,

1. 要删除网页的区间不存在

2. 要删除的网页全部在区间左边

3. 要删除的网页全部在区间右边

4. 要删除的网页在区间两边,选择先删除距离更短的网页,因为删完了,还要回去删除另一边的网页,r-l 这一段是固定的,就看先往哪边移动距离更短 。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std ;

int main(){
	int n , pos , l , r ;
	scanf( "%d%d%d%d" , &n , &pos , &l , &r ) ; 
	if( l == 1 && r == n ) printf( "0\n" ) ;
 	else if( l == 1 ) printf( "%d\n" , abs( pos-r ) + 1 ) ;
	else if( r == n ) printf( "%d\n" , abs( pos-l ) + 1 ) ;
	else printf( "%d\n" , min( abs( pos-l ) , abs( r-pos ) ) + 2 + r-l ) ; 
	return 0 ;
}
如有错误,敬请指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值