1 solutions

  • 0
    @ 2023-11-29 23:43:31

    经过我师傅的改良 写出来的。

    #include <bits/stdc++.h>
    #include <math.h>
    using namespace std;
    typedef long long ll;
    ll a[100005];
    int main()
    {
        ll Long = 0 , hu = 0;
        int n;//棋盘的长度。
        ll m , p , s1 , s2;//分界线,天降神兵的位置,天降神兵的数量,派往支援的个数。 
        cin >> n;
        for(int i = 1 ;i <= n ;i++){
            cin >> a[i];//一开始棋盘上各个点位的士兵数量。 
        }
        cin >> m >> p >> s1 >> s2;
        a[p] = a[p] + s1;
        for(int i = 1 ;i < m ;i++){
            Long += a[i] * (m - i);
        } 
        for(int i = n ;i > m ;i--){
            hu += a[i] * (i - m);
        }
        int p2=m;
        ll k2;//原始的差距,修改之后的差距。 
        ll min = abs(Long-hu);
        if(Long > hu){
            for(int i = n ;i > m ;i--){
                k2 = abs(Long - (hu+(i-m) * s2));
                if(k2 <= min){
                    p2 = i;
                    min = k2;
                }
            }
        }else if(Long == hu){
            p2 = m;
        }else if(Long < hu){
            for(int i = m-1 ;i > 0 ;i--){
                k2 = abs(hu - (Long+(m-i) * s2));
                if(k2 <= min){
                    p2 = i;
                    min = k2;
                }
            }
        }
        cout << p2;
        return 0;
    }
    

    师傅的杰作

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N=1e5+7;
    
    ll n,m;
    ll p1,p2;
    ll s1,s2;
    ll a[N];
    ll sum1,sum2;
    ll sum11,sum22;
    ll d;
    
    void f(ll ind){
        sum11=sum1,sum22=sum2;
        if(ind<m)sum11+=s2\*(m-ind);
        else if(ind>m)sum22+=s2\*(ind-m);
        if(abs(sum11-sum22)<d){
            d=abs(sum11-sum22);
            p2=ind;
        }
    }
    int main(){
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        cin>>m>>p1>>s1>>s2;
        for(int i=1;i<=n;i++)
            if(i<m) sum1+=a[i]\*(m-i);
            else if(i>m)sum2+=a[i]\*(i-m);
        if(p1<m)sum1+=s1\*(m-p1);
        else if(p1>m)sum2+=s1\*(p1-m);
        d=abs(sum1-sum2);
        p2=m;
        for(int i=1;i<=n;i++)
            f(i);
        cout<<p2<<endl;
        return 0;
    }
    
    • 1

    Information

    ID
    6816
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    # Submissions
    37
    Accepted
    4
    Uploaded By