当前位置:网站首页>New solution of 202112-2 sequence query

New solution of 202112-2 sequence query

2022-06-25 02:52:00 Liu Xue

#include <stdio.h>
int main()
{
    
	//n, Enter on behalf of the next line n Number ,N There are representatives of N A serial number , Attention from 0 Start 
	int n, N;	
	scanf("%d %d", &n ,&N);
	int B[N] = {
    0};
	int r;
	int k;
	int res = 0;
	int i, j;
	int sum = 0;	
	// Calculate the scale factor  
	r = N/(n+1);
	int g[N] = {
    0}; // Deposit g(X) 
	// Be careful A[0] Fixed for 0, So from A[1] Start saving , To A[n] end : 
	for(i=1; i<=n; i++)  
	{
    
		scanf("%d", &j); 
		B[j]++;  // Mark the position to change , Temporary use  B  Record 
	}	
	for(i=1; i<N; i++) //B[0] = 0;
	{
    
		B[i] = B[i] + B[i-1]; // Find the real array  B  namely f(X) 
// sum = sum + B[i]; // Sum up 
	// printf("%d ", B[i]);
	}	
	// printf("\n");
	for(i=1; i<N; i++) //g[0] = 0; 
	{
    
		// Calculation g(x)
		g[i] = i/r;
		//printf("%d ",g[i]); 
	}
	for(i=1; i<N; i++) //g[0] = 0;
	{
    
		if(g[i]>=B[i])
		{
    
			k = g[i]-B[i];
		} 
		else k = B[i]-g[i];
		res = res + k;
	}
	printf("%d",res);
	return 0;
 } 


原网站

版权声明
本文为[Liu Xue]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242341153596.html