思路很简单,但是要注意两个问题
一是要开long long
二就是用math库中的abs函数时请务必注意 其只能是 int 类型,一旦把long long中大的数塞进去就会报错。这第二条卡了我一中午
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <cstdio> 2 #include <cmath> 3 typedef long long ll; 4 5 6 ll Bing[100005]; 7 int Pos,n,m,p1,s1,s2,End; 8 ll Sum[100005]; 9 ll Cha,Ltot,Rtot,Update; 10 11 12 int main(){ 13 scanf("%d",&n); 14 for(int i = 1; i <= n ; ++i) scanf("%lld",&Bing[i]); 15 scanf("%d %d %d %d",&m,&p1,&s1,&s2); 16 Bing[p1] += s1; 17 for(int i = 1; i < m ; ++i){ 18 Sum[i] = Bing[i] * abs(m-i); 19 Ltot += Sum[i]; 20 } 21 for(int i = m+1; i <= n; ++i){ 22 Sum[i] = Bing[i] * abs(i-m); 23 Rtot += Sum[i]; 24 } 25 Update = abs(Ltot - Rtot); 26 for(Pos = 1; Pos <= m ; ++Pos){ 27 ll linshi = s2 * abs(m - Pos); 28 Cha = abs(Ltot + linshi - Rtot); 29 if(Cha <= Update){ 30 End = Pos; 31 Update = Cha; 32 } 33 } 34 for(Pos = m; Pos <= n ; ++Pos){ 35 ll linshi = s2 * abs(m - Pos); 36 Cha = abs(Rtot + linshi - Ltot); 37 if(Cha <= Update){ 38 End = Pos; 39 Update = Cha; 40 } 41 } 42 printf("%d",End); 43 return 0; 44 }