Uva 591 Box of Bricks

最小移动积木堆实现相同高度墙的算法
本文提供了一种算法,帮助Bob通过最小的积木移动次数,将不同高度的积木堆调整为相同的高度。通过计算平均高度并累加超出平均高度的积木数量来确定最少的移动次数。

 Box of Bricks 

Little Bob likes playing with his box of bricks. He puts the bricks one upon another and buildsstacks of different height. ``Look, I've built a wall!'', he tells his older sister Alice. ``Nah, you shouldmake all stacks the same height. Then you would have a real wall.'', she retorts. After a little con-sideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that allstacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimumnumber of bricks moved. Can you help?

Input 

The input consists of several data sets. Each set begins with a line containing the number n of stacksBob has built. The next line contains n numbers, the heights hi of the n stacks. You may assume$1 Ÿ\le n \leŸ 50$and $1 \leŸ h_i Ÿ\le 100$.

The total number of bricks will be divisible by the number of stacks. Thus, it is always possibleto rearrange the bricks such that all stacks have the same height.

The input is terminated by a set starting with n = 0. This set should not be processed.

Output 

For each set, first print the number of the set, as shown in the sample output. Then print the line``The minimum number of moves is k.'', where k is the minimum number of bricks thathave to be moved in order to make all the stacks the same height.

Output a blank line after each set.

Sample Input 

6
5 2 4 1 7 5
0

Sample Output 

Set #1
The minimum number of moves is 5.


题目大意:

给你用积木摞起来的不同高度的柱子,问弄成一样高时,最少的移动次数。

分析:简单题。求出平均数,把超过平均数的加和即可。



#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;
const int N = 10000;
int num[N];
int main() {
	int n,t;
	t=1;
	while (cin >> n) {
		if(n == 0)
			break;
		
		int sum = 0;
		int avg;
		
		for(int i=0;i<n;i++) {
			cin >> num[i];
			sum += num[i];	
		}
		avg = sum/n;
		
		int mov=0;
		for(int i=0;i<n;i++) {
			if(num[i]>avg) {
				mov += (num[i]-avg);	
			}	
		}
		cout<<"Set #"<<t++<<endl;
		cout<<"The minimum number of moves is "<<mov<<"."<<endl<<endl; 
	}
	return 0;
}

### Box Filter 的定义与应用 Box Filter 是一种常见的滤波器,在计算机视觉和图像处理领域被广泛应用于平滑化操作以及降噪。它通过计算局部区域像素值的平均来达到模糊效果,其核心思想是对输入图像中的每一个像素点取一个固定大小的邻域窗口,并对该窗口内的所有像素求均值。 在实际实现中,Box Filter 可以看作是一种特殊的卷积核(Convolutional Kernel),其中所有的权重均为相等的常数[^1]。这种特性使得它的计算非常高效,尤其适合实时应用场景下的快速预处理需求。 #### 使用 TensorFlow/Keras 实现 Box Filter 基于 Keras 提供的功能可以轻松构建并应用 Box Filter 到图像数据上。下面是一个简单的例子展示如何利用 `Keras` 中的 `Conv2D` 层模拟 Box Filter: ```python import numpy as np from tensorflow import keras def create_box_filter(size): """ 创建一个指定尺寸的盒状滤波器 """ filter_weights = np.ones((size, size)) / (size * size) return filter_weights[..., None] box_kernel = create_box_filter(3) # 定义一个 3x3 大小的 box filter input_shape = (None, 64, 64, 1) # 假设输入图片为灰度图,分辨率为 64x64 model = keras.models.Sequential([ keras.layers.InputLayer(input_shape=input_shape[1:]), keras.layers.DepthwiseConv2D( kernel_size=3, depth_multiplier=1, strides=1, padding="same", use_bias=False, weights=[box_kernel], name="box_filter_layer" ) ]) # 测试模型 test_image = np.random.rand(*input_shape).astype(np.float32) output = model.predict(test_image) print("Input shape:", test_image.shape) print("Output shape:", output.shape) ``` 上述代码片段展示了如何创建一个自定义的 Box Filter 并将其作为卷积层的一部分嵌入到神经网络架构之中[^2]。这里需要注意的是,我们手动设置了卷积核参数而不是让它们成为可训练变量,这正是为了保持 Box Filter 的原始功能不变形。 #### 总结 Box Filters 虽然简单却十分有效,尤其是在需要降低噪声或者减少高频细节的时候尤为适用。然而由于缺乏方向性和频率选择能力,对于更复杂的特征提取任务来说可能不够灵活,因此通常会与其他类型的滤波器配合使用,比如高斯滤波器(Gaussian Blur) 或者 Sobel 边缘检测算子等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值