Solution to a problem P2512 [HAOI2008] candy transfer

First, we have to use some of the average card ideas (chiefs have understood this idea skip):

Set $ ​​A_i $ express the original amount of $ i $ candy a child,

Set $ ​​ave $ represent all the candy average number of children,

$ X_i $ candy represents the number of children left $ i $ a pass. which is:

① If $ X_i> 0: \ quad $ $ i $ the first two children left pass $ X_i $ candy;

② Otherwise, if $ X_i <0: \ quad $ $ i $ the first two children left pass $ | X_i | $ candy.

So the problem is the code:

Code:


#include<cstdio>
int a[101];
int n,x,s;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&a[i]);
        x+=a[i];
    }
    x/=n;
    for(int i=1;i<n;++i)
    {
        a[i]-=x;
        if(a[i]!=0)
        {
            ++s;
            a[i+1]+=a[i];
        }
    }
    printf("%d",s);
    return 0;
}

Well now let us return to the original question candy pass up.

Just look at what the set:

Set $ ​​A_i $ express the original amount of $ i $ candy a child,

$ Ave $ denote the number of children the average of all the candy,

$ X_i $ candy represents the number of children left $ i $ a pass.

Equations can be obtained by the title:
$$
A_1 + X_2 = X_1-Ave
$$
$$
A_2 = Ave + X_3-X_2
$$
$$
?????
$$
$$
A_n-X_n = X_1 + Ave
$$
namely:

$$ A_i + X_ {at} + 1 + = X_i took (1 \ p <n) $$

$$A_n+X_1+X_n=ave(i=n)$$

变形得:
$$
X_2=ave+X_1-A_1
$$
$$
X_3=ave+X_2-A_2=ave+(ave+X_1-A_1)-A_2=2ave-A_1-A_2+X_1
$$
$$
···
$$
$$
X_1=ave+X_n-A_n=n·ave-A_1-A_2-…-A_n+X_1
$$

Then we set up:
$$
C_1-ave = A_1
$$
$$
C_2 = A_1 + A_2-ave
$$
$$
· · ·
$$
$$
C_n A_1 + = + ... + A_n A_2 · the n-ave-
$$

That set up:

$$ C_i = \ sum_ {j = 1} ^ iA_j-in · took (1 \ c \ k) $$

There:
$$
X_2 = X_1-C_l
$$
$$
X_3 = X_1-C_2
$$
$$
?????
$$
$$
X_n = X_1-C_n
$$

which is:

$$X_i=X_1-C_i$$

Then we return to look at the request, require you to pass a minimum value,

That is to say, requires minimizing
$$
| X_1 | + | X_2 | + ????? + | X_n |
$$
which is equal to formula
$$
| X_1-C_l | + | X_1-C_2 | + ????? + | X_1 -C_n |
$$

That is seeking:

$$ MIN {\ sum_ {i = 1} ^ nX_i} = MIN {\ sum_ {i = 1} ^ nX_1-C_i} $$

Thus easy to handle, are known $ $ C_i (amount, can be at least pre-out), in order to minimize the above equation, we $ $ C_i as a point on the number line, the problem is now transformed into find a point of $ X_1 $, making her the distance and minimum points on each $ C_i $.

Obviously, the point is that the median $ n $ points. . . (For a while and then give mathematical proof)

Then after obtaining a $ X_1 $, according
$$
X_2 = X_1-C_l
$$
$$
X_3 = X_1-C_2
$$
$$
?????
$$
$$
X_n = X_1-C_n
$$

This cook persimmons, we can find $ X_2, X_3 ... X_n $, then you can get tax evasion are asking:

Minimizing
$$
| X_1 | + | X_2 | + · · · + | X_n |
$$

233 friends ~ ~ ~

Code for better or for worse, we do not hold anything against qwq:

Code:


#include<bits/stdc++.h>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define inline il

typedef long long ll;
typedef long double ld;

const int inf = 0x7fffffff;
const int N = 1e6+1;

ll n;
ll a[N],c[N];
ll ave,ans,mid;

using namespace std;

int main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;++i)
        scanf("%lld",&a[i]),ave+=a[i];
    ave/=n;
    for(int i=1;i<=n;++i)
        c[i]=c[i-1]+ave-a[i-1];
    sort(c+1,c+n+1);
    mid=c[(n+1)/2];
    for(int i=1;i<=n;++i)
        ans+=abs(mid-c[i]);
    printf("%lld",ans);
    return 0;
}

Appendix: mathematical proof

In the n-number line has $ $ points, find a point $ X $, and the minimum distance such that she each point.

Prove: This number is the median of $ n $ number represented by that point.

If we take the number line point paired off with the largest minimum, the second largest of times with a small ......

Point to the nearest point from each of the two intermediate points, then

如果有奇数个点,那么显然中间那个点便为所求。

该点表示的数是这$n$个数的中位数得证。

注:有一些柿子在文中以不同的形式重复出现,主要是为了同时满足大佬和蒟蒻的需要。一些前面写了‘即…’并用

这个东西

框起来的一般是适合大佬的较严(bian)格(tai)数学写法,而其他的则是简明易懂的正常的、不变态的写法。

希望大家喜欢,点个赞哦:)

Guess you like

Origin www.cnblogs.com/oierwyh/p/11124180.html