【題意】c++
直接a[p1]+=s1spa
求一個p2,使得a[p2]+=s2後,abs{∑(m-i)*a[i]}最小code
【解法】blog
求出S=∑(m-i)*a[i]it
當S<0時,對[1..m)進行枚舉class
當S>0時,對(m,n]進行枚舉變量
minS的初始值爲當i=m時,minS=Sim
注意,全部變量都須要longlongd3
【代碼】db
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N = 100000+5; 5 ll n; 6 ll c[N]; 7 ll m,p1,s1,s2; 8 int main() 9 { 10 scanf("%lld",&n); 11 for (int i=1;i<=n;i++) 12 scanf("%lld",&c[i]); 13 scanf("%lld%lld%lld%lld",&m,&p1,&s1,&s2); 14 c[p1]+=s1; 15 ll x=0; 16 for (ll i=1;i<=n;i++) 17 x+=(m-i)*c[i]; 18 ll mm=(x>0)?x:(-x),k=m; 19 for (ll i=1;i<=n;i++) 20 { 21 ll xx=x+(m-i)*s2; 22 if (xx<0) xx=-xx; 23 if (xx<mm) 24 { 25 mm=xx,k=i; 26 } 27 } 28 printf("%lld",k); 29 }