题面:P2234 [HNOI2002]营业额统计
题解:随便写写
注意:cmath中abs函数返回的是一个浮点数,在bzoj上会ce
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<set>
#define abs(a) ((a)>=0?(a):(-a))
#define ll long long
using namespace std;
ll ans,N,x,a,b,c;
set<ll>A;
set<ll>::iterator iter;
int main(){
scanf("%lld",&N);
for(int i=;i<=N;i++){
scanf("%lld",&x);
iter=A.lower_bound(x);
if(i!=){
a=abs(*iter-x);
if(iter!=A.begin()){
iter--;
b=abs(x-*iter);
if(a<b)ans+=a;else ans+=b;
}
else ans+=a;
}
else ans=x;
A.insert(x);
}
printf("%lld\n",ans);
return ;
}
By:AlenaNuna