考虑
k=1
k
=
1
的情况
设桥的位置是
x
x
,那么
只要把
a,b
a
,
b
放一起排序取中位数就可以了
那么
k=2
k
=
2
时怎么办
发现每个人
(x,y)
(
x
,
y
)
一定会走离中位数(
x+y2
x
+
y
2
)最近的那个桥
也会有一个位置
mid
m
i
d
使所有位置
xi+yi2≤mid
x
i
+
y
i
2
≤
m
i
d
的人都走一个桥,
xi+yi2>mid
x
i
+
y
i
2
>
m
i
d
的人走另一个桥
那么我们可以把所有人按
x+y
x
+
y
排序,从头到尾把每个人当成断点,上下两部分按方法一处理就可以了
动态的中位数可以用平衡树!
代码如下:
#include<algorithm>
#include<ctype.h>
#include<cstdio>
#define N 100050
using namespace std;
inline int read(){
int x=0,f=1;char c;
do c=getchar(),f=c=='-'?-1:f; while(!isdigit(c));
do x=(x<<3)+(x<<1)+c-'0',c=getchar(); while(isdigit(c));
return x*f;
}
typedef long long LL;
char c1,c2;
int n,k,x,y,x1,y1,tot,top,ave;
LL ans,ans1,t;
int s[N<<1];
struct Data{
int x,y;
Data(int _=0,int __=0):x(_),y(__){}
}a[N];
inline bool cmp(Data a,Data b){
return a.x+a.y<b.x+b.y;
}
struct Node{
Node *ch[2],*fa;
int x,cnt,siz;
long long sum;
inline int dir(){
if(fa->ch[0]==this) return 0;
if(fa->ch[1]==this) return 1;
return -1;
}
inline void maintain(){
siz=cnt+ch[0]->siz+ch[1]->siz;
sum=1ll*cnt*x+ch[0]->sum+ch[1]->sum;
return;
}
inline int cmp(int k){
if(k==x) return -1;
return k<x?0:1;
}
Node(int);
}*null,*root[2],*tmp;
Node::Node(int _):x(_),sum(_){
ch[0]=ch[1]=fa=null;
cnt=siz=1;
}
inline void init(){
null=new Node(0);
null->ch[0]=null->ch[1]=null->fa=null;
null->cnt=null->siz=null->sum=0;
root[0]=root[1]=null;
return;
}
inline void Rotate(Node *x,int d){
Node *k=x->ch[d^1];
x->ch[d^1]=k->ch[d];
if(x->ch[d^1]!=null) x->ch[d^1]->fa=x;
k->ch[d]=x;
if(x->fa!=null) x->fa->ch[x->dir()]=k;
k->fa=x->fa;x->fa=k;
x->maintain();k->maintain();
return;
}
inline void Splay(Node *x,Node *y,int k){
while(x->fa!=y){
if(x->fa->fa!=y && x->dir()==x->fa->dir())
Rotate(x->fa->fa,x->dir()^1);
Rotate(x->fa,x->dir()^1);
}
if(y==null) root[k]=x;
return;
}
Node *LowerPointer(int k,Node *x){
if(x==null) return null;
if(x->x>=k) return LowerPointer(k,x->ch[0]);
tmp=LowerPointer(k,x->ch[1]);
return tmp==null?x:tmp;
}
Node *UpperPointer(int k,Node *x){
if(x==null) return null;
if(x->x<=k) return UpperPointer(k,x->ch[1]);
tmp=UpperPointer(k,x->ch[0]);
return tmp==null?x:tmp;
}
void Insert(int k,Node *&x,Node *fa){
if(x==null){
x=new Node(k);
tmp=x;
x->fa=fa;
return;
}
int d=x->cmp(k);
if(!~d){
x->cnt++,x->siz++;
x->sum+=k;
tmp=x;
}
else Insert(k,x->ch[d],x);
x->maintain();
return;
}
inline void AddNew(int k,int x){
Insert(k,root[x],null);
Splay(tmp,null,x);
return;
}
inline void Delete(int x,int k){
Node *a=LowerPointer(x,root[k]),*b=UpperPointer(x,root[k]);
if(a==null && b==null){
if(root[k]->cnt>1){
root[k]->cnt--;root[k]->siz--;
root[k]->sum-=root[k]->x;
}
else root[k]=null;
return;
}
if(a==null){
Splay(b,null,k);
if(root[k]->ch[0]->cnt>1){
root[k]->ch[0]->cnt--;root[k]->ch[0]->siz--;
root[k]->ch[0]->sum-=root[k]->ch[0]->x;
}
else root[k]->ch[0]=null;
root[k]->maintain();
return;
}
if(b==null){
Splay(a,null,k);
if(root[k]->ch[1]->cnt>1){
root[k]->ch[1]->cnt--;root[k]->ch[1]->siz--;
root[k]->ch[1]->sum-=root[k]->ch[1]->x;
}
else root[k]->ch[1]=null;
root[k]->maintain();
return;
}
Splay(a,null,k);Splay(b,a,k);
if(root[k]->ch[1]->ch[0]->cnt>1){
root[k]->ch[1]->ch[0]->cnt--;root[k]->ch[1]->ch[0]->siz--;
root[k]->ch[1]->ch[0]->sum-=root[k]->ch[1]->ch[0]->x;
}
else root[k]->ch[1]->ch[0]=null;
root[k]->ch[1]->maintain();root[k]->maintain();
return;
}
Node *K_th(int k,Node *x){
if(k>x->ch[0]->siz && k<=x->ch[0]->siz+x->cnt)
return x;
int d=k<=x->ch[0]->siz?0:1;
return K_th(k-(d?x->ch[0]->siz+x->cnt:0),x->ch[d]);
}
inline LL Query(int k){
if(root[k]==null) return 0;
tmp=K_th((root[k]->siz+1)>>1,root[k]);
Splay(tmp,null,k);
t=1ll*root[k]->x*root[k]->ch[0]->siz-root[k]->ch[0]->sum+root[k]->ch[1]->sum-1ll*root[k]->x*root[k]->ch[1]->siz;
return t;
}
void print(Node *x){///Debug
if(x==null) return;
print(x->ch[0]);
for(int i=1;i<=x->cnt;i++) printf("%d ",x->x);
print(x->ch[1]);
}
int main(){
init();
k=read();n=read();
for(int i=1;i<=n;i++){
do c1=getchar(); while(!isalpha(c1));
x=read();
do c2=getchar(); while(!isalpha(c2));
y=read();
if(c1==c2) ans+=abs(x-y);
else a[++top]=Data(x,y);
}
ans+=top;
if(k==1){
for(int i=1;i<=top;i++){
s[++tot]=a[i].x;s[++tot]=a[i].y;
}
sort(s+1,s+tot+1);
for(int i=1;i<=tot;i++) ans=ans+abs(s[tot>>1]-s[i]);
printf("%lld",ans);
return 0;
}
sort(a+1,a+top+1,cmp);
for(int i=1;i<=top;i++){
AddNew(a[i].x,1);
AddNew(a[i].y,1);
}
ans1=Query(1);
for(int i=1;i<=top;i++){
Delete(a[i].x,1);Delete(a[i].y,1);
AddNew(a[i].x,0);AddNew(a[i].y,0);
ans1=min(ans1,Query(0)+Query(1));
}
printf("%lld\n",ans1+ans);
return 0;
}