Illustrating P5016

Obviously it was a violent hit into a magical greedy (before also wrong)

Deliver

 

We see the data scale, 1e9, good good, which opened a long long

We can easily influence can be calculated forces hu forces lo and tiger dragon (variable names earth seeking forgiveness), when the count plus Tianjiangshenbing

Then we need to find a point to make this point right point increase s2, update lo, hu, hu then the difference between lo and as small as possible.

Since it is so that the gap between lo and hu as small as possible, we may wish to seek out when not in use s2 their difference, the following is represented by a. Next, we need to do is find the | minimum value, k of | a-s2 * k.

The best course, is divisible s2 a, so that | a-s2 * k | = 0, k = a / s2. But if s2 does not divide a, then we need to compare k1 = a / s2, k2 = a / s2 + 1 ( "/" is divisible) which is more superior.

Such basic ideas there, but this is reflected in the code seems very troublesome ah, but also have to determine divisible discuss lo and hu but also to determine which large numbers of God's smallest horse, very easy to make mistakes with wood there. So we continue to think about.

Just from the point of view of the divisible, so that we now direct k = a / s2 (k is a real number), for k rounded. Why is this right? Because, according to results of rounding k is certainly to be closer to a / s2, also make | a-s2 * k | smaller.

Lo hu we discuss the relationship between the magnitude, i.e. at which end p2 m, if the left end, then p2 = mk, if the right end, p2 = k + m, if p2 <1, let p2 = 1 (most the left is 1)

Code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
long long n,m,s1,s2,c[100009],lo,hu,p1;
long long read()
{
    char ch=getchar();
    long long x=0;bool flag=0;
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')flag=1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar();
    }
    if(flag)x=-x;
    return x;
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)
      c[i]=read();
    m=read();p1=read();s1=read();s2=read();
    c[p1]+=s1;
    for(int i=1;i<=n;i++)
    {
        if(i<m)
        {
            lo+=c[i]*(m-i);
        }
        if(i>m)
        {
            hu+=c[i]*(i-m);
        }
    }
    long long pwp=abs(lo-hu),qaq;
    long long qwq=round(1.0*pwp/s2);
    if(lo>hu)qwq+=m;
    if(lo<hu)qwq=m-qwq;
    if(lo==hu)qwq=m;
    if(qwq<1)qwq=1;
    printf("%d\n",qwq);
}

 

ρωρ

 

Guess you like

Origin www.cnblogs.com/lcez56jsy/p/11097905.html