https://vjudge.net/problem/2198221/origin
逆向思维,原题是人出来,我们处理成人进去,算出来每个人的曼哈顿距离,然后从大到小排序,距离长的先入。走的距离+这个人从队伍中走到入口的距离的最小值就是答案
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<cmath> #include<ctime> #include<set> #include<map> #include<stack> #include<cstring> #define inf 2147483647 #define ls rt<<1 #define rs rt<<1|1 #define lson ls,nl,mid,l,r #define rson rs,mid+1,nr,l,r #define N 5000010 #define For(i,a,b) for(int i=a;i<=b;i++) #define p(a) putchar(a) #define g() getchar() using namespace std; int n,m,r,mid,k,ans; struct node{ int x,y,d; bool operator < (const node &a)const{ return d>a.d; } }a[N]; void in(int &x){ int y=1; char c=g();x=0; while(c<'0'||c>'9'){ if(c=='-')y=-1; c=g(); } while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=g(); } x*=y; } void o(int x){ if(x<0){ p('-'); x=-x; } if(x>9)o(x/10); p(x%10+'0'); } int main(){ in(n);in(m);in(r); For(i,1,r){ in(a[i].x);in(a[i].y); if(a[i].y>m) a[i].d=a[i].y-m+n-a[i].x+1; else a[i].d=m-a[i].y+n-a[i].x+2; } sort(a+1,a+r+1); For(i,1,r) ans=max(ans,(k++)+a[i].d); o(ans); return 0; }View Code