AcWing 4281. 序列查询新解
原题链接
简单
作者:
YAX_AC
,
2024-12-06 11:58:53
,
所有人可见
,
阅读 8
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int N = 100010;
int a[N];
int n,m;
LL sum;
int main()
{
cin>>n>>m;
for(int i = 1; i<=n; i++) cin>>a[i];
a[n+1] = m;//边界
int r = m/(n+1);
int f,g,d;
//分段代替枚举
for(int i = 0; i<=n; i++)
{
f = i;
for(int j = a[i]; j<a[i+1]; j+=d)//按照g值分段
{
g = j/r;
int last = (g+1)*r-1;//g(x)值为g的最后一个数
last = min(last,a[i+1]-1);//last值不能超过a[i + 1]
LL cnt = last-j+1;
LL t = abs(f-g);
sum += t*cnt;
d = cnt;
}
}
cout<<sum;
return 0;
}