# 洛谷4264 [usaco18feb]teleportation s

## 扫描线的简单应用

Posted by yjjr's blog on January 26, 2019

#题解

# Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=3e5+6;
ll ans=0;int n,tot=0;
struct node{ll c;int t;}s[maxn];
inline bool cmp(node a,node b){return a.c<b.c;}
int main(){
rep(i,1,n){
ans+=abs(a-b);
if(abs(a-b)>abs(a)){
ll tmp=abs(a-b)-abs(a);
s[++tot]={b-tmp,-1};s[++tot]={b+tmp,-1};
s[++tot]={b,2};
}
}
sort(s+1,s+1+tot,cmp);
int pre=s[1].c,sum=s[1].t;
ll re=ans;
rep(i,2,tot){
re+=(s[i].c-pre)*sum;
pre=s[i].c;
sum+=s[i].t;
ans=min(ans,re);
}
cout<<ans<<endl;
return 0;
}