题目
代码
package ccf;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Reader {
static StringTokenizer token =new StringTokenizer("");
static BufferedReader reader =new BufferedReader (new InputStreamReader(System.in)) ;
static String next() throws IOException {
while(!token.hasMoreTokens()) {
token =new StringTokenizer(reader.readLine()) ;
}
return token.nextToken() ;
}
static int nextInt() throws IOException {
return Integer.parseInt(next()) ;
}
}
public class Main{
public static void main(String[] args) throws IOException{
int n=Reader.nextInt();
int N=Reader.nextInt();
int A[]=new int[N+1];
int f[]=new int[N+1];
int s=0;
int r=(int) Math.floor(N/(n+1));
for(int i=0;i<n;i++){
A[Reader.nextInt()]=1;
}
f[0]=0;
for (int i = 1; i < N; i++){
f[i]=f[i-1]+A[i];
}
for (int i = 0; i < N; i++) {
s+=Math.abs((int)(Math.floor(i/r))-f[i]);
}
System.out.println(s);
}
}
小结
考场70分
数组前缀和
// 1 维
S[i] = a[1] + a[2] + ... a[i]
a[l] + ... + a[r] = S[r] - S[l - 1]
// 2 维
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+arr[i][j];
S[i, j] = 第i行j列格子左上部分所有元素的和
(x1, y1)为左上角,(x2, y2)为右下角的子矩阵的和为:
S[x2, y2] - S[x1 - 1, y2] - S[x2, y1 - 1] + S[x1 - 1, y1 - 1]