跳到正文

题解

题解:P13993 【MX-X19-T2】「LAOI-14」SPECIALZ

XVI_GR约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年9月6日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 2 分钟
题目
P13993

版本

  1. v2当前
  2. v1

贪心题。

思路

首先观察题目内容,可以发现,我们可以让向下移动时结果都为 11(贪心策略)。然后,我们设它能到达的区间为 [l,r][l,r],那么很明显它的结果只能是 r−l+1r-l+1,因为这样才能让结果更小。

那么问题来了,怎样让 r−l+1r-l+1 尽量小?

这也不难,只要贴近边缘就行了,还有,就是瞬移时要选离边缘最近的那个点,这样就能保证它的范围更小了。

最后,注意一个细节,千万要把原来的位置去掉!!!

AC 代码:

C++
#include<bits/stdc++.h>
#define int long long
#define ld long double
#define PII pair<int,int>
#define endl "\n"
using namespace std;
const int N=1e6+5;
const int INF=1e9;
const double EPS=1e-6;
const int MOD=1e9+7;
int n,a[N],m,ans=1;
void solve(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	int y=1;
	for(int i=1;i<=n;i++){
		int l=max(1LL,y-a[i]),r=min(m,y+a[i]),f1=0;
		if(y==1)l=2,f1=1;
		if(y==m)r=m-1,f1=1;
		if(l-1>m-r)y=r;
		else y=l;
	//	cout<<l<<" "<<r<<" "<<y<<" "<<f1<<" ";
		ans+=(r-l+1+f1);
		if(i!=n)ans++;
	//	cout<<ans<<endl;
	}
	cout<<ans;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int T=1;
//	cin>>T;
	while(T--)solve();
	return 0;
}
原文