P5016

 

 This is a question. I was ashamed when I looked at the question.

But after carefully reading the title a few times, I found that it was the same.

I think this question is very simple, just enter it first, add s1 engineers at the p1 position, then enumerate and put s2 engineers in all the barracks in turn, calculate the difference between the forces of the two sides each time, and take the minimum

But after writing it, the code timed out

The reason for the timeout is too simple

The reason is that the difference in power is counted every time.

The time complexity is O (n ^ 2) O ( n 2 )

So I considered

Just calculate the difference between the forces of the Dragon and Tiger in advance, and each time you enumerate, just calculate the difference between the new forces.

In accordance with the principle of not seeing the ancestors of long long, the AC code I wrote is the following

#include <bits/stdc++.h>
#define max maxx 
#define min minn
#define ll long long
using namespace std;
ll n,c[100005],m,p1,s1,s2,p2;
ll lon,hu;
ll max(ll a,ll b)
{
    if(a>b) return a;
    else return b;
}
ll min(ll a,ll b)
{
    if(a<b) return a;
    else return b;
}
int main()
{
    ll i;
    cin>>n;
    for(i=1;i<=n;i++) 
        cin>>c[i];
    cin>>m>>p1>>s1>>s2;
    c[p1]+=s1;
    for(i=1;i<m;i++)
        lon+=c[i]*(m-i);
    for(i=m+1;i<=n;i ++) 
        hu + = c [i] * (im);
    if(lon==hu)
        cout<<m<<endl;
    else if(lon<hu)
    {
        p2=m-(hu-lon)/s2;
        p2=max(1,p2);
        p2=min(n,p2);
        if(p2>1 && (abs(lon+s2*(m-p2+1)-hu)<=abs(hu-lon-s2*(m-p2))))
            cout<<p2-1<<endl;
        else cout<<p2<<endl;
    } 
    else
    {
        p2=m+(lon-hu)/s2;
        p2=max(1,p2);
        p2=min(n,p2);
        if(p2<n && (abs(hu+s2*(p2+1-m)-lon)<abs(hu+s2*(p2-m)-lon)))
            cout<<p2+1<<endl;
        else cout<<p2<<endl;
    }
    return 0;
}

 Bye !!!

Guess you like

Origin www.cnblogs.com/--840-114/p/12727460.html