题面:P2234 [HNOI2002]营业额统计
题解:随便写写
注意:cmath中abs函数返回的是一个浮点数,在bzoj上会ce
代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<set> 5 #define abs(a) ((a)>=0?(a):(-a)) 6 #define ll long long 7 using namespace std; 8 ll ans,N,x,a,b,c; 9 set<ll>A; 10 set<ll>::iterator iter; 11 int main(){ 12 scanf("%lld",&N); 13 for(int i=1;i<=N;i++){ 14 scanf("%lld",&x); 15 iter=A.lower_bound(x); 16 if(i!=1){ 17 a=abs(*iter-x); 18 if(iter!=A.begin()){ 19 iter--; 20 b=abs(x-*iter); 21 if(a<b)ans+=a;else ans+=b; 22 } 23 else ans+=a; 24 } 25 else ans=x; 26 A.insert(x); 27 } 28 printf("%lld ",ans); 29 return 0; 30 }
By:AlenaNuna