此题为cspccf202112_3题,思路完全理解了,代码能跑70分,避免数字溢出还专门用了long long型,但是不知道为什么csp官网检测会报错
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
int main(){
int n;
long long int sub = 0;
long long int *A, r, N, *f, rf, rg;
long long int length = 0;
scanf("%d %lld", &n, &N);
r = N / (n + 1);
A = (long long int *)calloc(n + 10, sizeof(long long int));
A[0] = 0;
A[n + 1] = N;
f = (long long int *)calloc(N + 10, sizeof(long long int));
//g = (long long int *)calloc(N, sizeof(long long int));
for (int i = 1; i < (n + 1); i++){
scanf("%lld", &A[i]);
if (A[i] < N)f[A[i]] = i;
}
for (long long int i = 1; i < N; i++){//完成g[],f[]
//g[i] = i / r;
if (f[i] == 0)
f[i] = f[i - 1];
else continue;
}
for (int j = 0; j < n + 1; j++){
for (long long int i = A[j]; i < A[j + 1]; i += length){
rf = A[j + 1] - i;//rf = A[j + 1] - 1;
rg = r - i%r;//rg = i + r - 1 - i%r;
length = (rf>rg ? rg : rf);//length = (rf>rg ? rg : rf) - i + 1;
sub += length*(abs(f[i] - i/r));
}
}
printf("%lld", sub);
system("pause");
return 0;
}
错误截图:
求解惑,提供个思路我自己调试也行!