Uva 11300 Spreading the Wealth

本文介绍了一个共产主义政权尝试通过一种特殊的方法重新分配村庄内居民财富的算法问题。该方法要求每个人将所有财产转换为等值硬币,并最终确保所有人持有相同数量的硬币。文章详细解释了如何计算最小硬币转移数,以实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such that the total number of coins is divisible by the number of people in the village. Finally, each person gives a number of coins to the person on his right and a number coins to the person on his left, such that in the end, everyone has the same number of coins. Given the number of coins of each person, compute the minimum number of coins that must be transferred using this method so that everyone
has the same number of coins.

Input

There is a number of inputs. Each input begins with n (n < 1000001), the number of people in the village. n lines follow, giving the number of coins of each person in the village, in counterclockwise order around the table. The total number of coins will fit inside an unsigned 64 bit integer.

Output

For each input, output the minimum number of coins that must be transferred on a single line.

Solution

we assume that M is the final number of coins. And xi means the number of coins that ith people gives to the (i-1)th people and x1 means the number of coins that 1st people gives to the nth people. Why? if people 1 gives 2 coins to people 2 and people 2 gives 4 coins to people 1, we can regard people 2 gives 2 coins to people as this and xi could be negative.

Every people will have M coins in the last. Let Ai be the coins people i have at the beginning time.

As for people 1, we have A1 + x2 - x1 = M ==> x2 = x1 - (A1 - M)
As for people 2, we have
A2 + x3 - x2 = M ==> x3 = x2 - (A2 - M) ==> x3 = x1 - (A2 + A1 - 2M)
and so on
xi = x1 - (A(n-1) + A(n-2) + …. + A1 - (n-1)M )

let Ci = Ai+..+A1 - iM

Now we want |x1| + |x2| + … + |xn| be minimum. In another word is |x1| + |x1-C1| + |x2-C2| + … + |x1-Cn-1| be minimum.

That is to say, we need to find a point in number axis so that the sum of the distances between the point and other points is minimum. And we can find the Median of the C[]. Let x1 be the Median, and the answer is clear.

code:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 1000010;
typedef long long ll;

ll a[maxn],c[maxn];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        ll sum = 0;
        for(int i = 0;i < n;++ i)
        {
            scanf("%lld",&a[i]);
            sum += a[i];
        }
        ll M = sum/n;
        c[0] = 0;
        for(int i = 1;i < n;++ i)
            c[i] = c[i-1] + a[i-1] - M;
        sort(c,c+n);
        ll avg = c[(n-1)/2];
        ll ans = 0;
        for(int i = 0;i < n;++ i)
            ans += fabs(avg-c[i]);
        printf("%lld\n",ans);
    }
    return 0;
}
分数阶傅里叶变换(Fractional Fourier Transform, FRFT)是对传统傅里叶变换的拓展,它通过非整数阶的变换方式,能够更有效地处理非线性信号以及涉及时频局部化的问题。在信号处理领域,FRFT尤其适用于分析非平稳信号,例如在雷达、声纳和通信系统中,对线性调频(Linear Frequency Modulation, LFM)信号的分析具有显著优势。LFM信号是一种频率随时间线性变化的信号,因其具有宽频带和良好的时频分辨率,被广泛应用于雷达和通信系统。FRFT能够更精准地捕捉LFM信号的时间和频率信息,相比普通傅里叶变换,其性能更为出色。 MATLAB是一种强大的数值计算和科学计算工具,拥有丰富的函数库和用户友好的界面。在MATLAB中实现FRFT,通常需要编写自定义函数或利用信号处理工具箱中的相关函数。例如,一个名为“frft”的文件可能是用于执行分数阶傅里叶变换的MATLAB脚本或函数,并展示其在信号处理中的应用。FRFT的正确性验证通常通过对比变换前后信号的特性来完成,比如评估信号的重构质量、信噪比等。具体而言,可以通过计算原始信号与经过FRFT处理后的信号之间的相似度,或者对比LFM信号的关键参数(如初始频率、扫频率和持续时间)是否在变换后得到准确恢复。 在MATLAB代码实现中,通常包含以下步骤:首先,生成LFM信号模型,设定其初始频率、扫频率、持续时间和采样率等参数;其次,利用自定义的frft函数对LFM信号进行分数阶傅里叶变换;接着,使用MATLAB的可视化工具(如plot或imagesc)展示原始信号的时域和频域表示,以及FRFT后的结果,以便直观对比;最后,通过计算均方误差、峰值信噪比等指标来评估FRFT的性能。深入理解FRFT的数学原理并结合MATLAB编程技巧,可以实现对LFM信号的有效分析和处理。这个代码示例不仅展示了理论知识在
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值