莫干山奇遇
出题人当然是希望出的题目有关 oxx,于是想方设法给题目配上一些有关 oxx 的背景故事,使得它看起来不那么无趣。但有的时候却无法引入合适的小姐姐,使得 oxx 显得非常可怜。所以出题人删除了故事,只留下一个枯燥乏味的数学问题。
【故事已删除】
给一个长度为 n 的序列 a1,a2,…,an,求一个长度为 m 的序列 b1,b2,…,bm 使得:
a1,a2,…,an 是 b1,b2,…,bm 的子序列(不一定连续),且
存在常数 p>0 使得 b1,b2,…,bm 是一个 p-莫干山序列。
序列 s1,s2,…,sn 是 p-莫干山序列,当且仅当:存在 0≤x<p 对于 1≤i≤n 满足
si=(x+i)mod p。
求 m 的最小值。
Input
第一行一个整数 n (1≤n≤2⋅105)。
第二行 n 个整数用空格隔开 a1,a2,…,an (0≤ai≤1e9)。
Output
输出最小的 m。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+100;
ll n,a[N],ans=0,p=-1;
int main()
{
scanf("%lld",&n);
for(int i=0;i<n;i++){
scanf("%lld",&a[i]);
p=max(p,a[i]);
}
p++;
ans=1;
ll pre=a[0];
for(int i=1;i<n;i++){
if(pre<a[i]){
ans+=(a[i]-pre);
}else{
ans+=(p-pre+a[i]);
}
pre=a[i];
}
//ans+=n;
printf("%lld\n",ans);
return 0;
}