1 条题解

  • 0
    @ 2021-6-15 14:36:41

    C++ :

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define MAXN 100010
    typedef long long ll;
    
    struct T{
    	ll x, y, id;
    }a[MAXN];
    ll tx[MAXN], ty[MAXN];
    bool cmp1(const T& tx,const T& ty){
    	return tx.x < ty.x;
    }
    bool cmp2(const T& tx,const T& ty){
    	return tx.y < ty.y;
    }
    int main()
    {
    	ll sumx, sumy, sum;
    	int n, cas;
    	scanf("%d",&n);
    	sumx = sumy = 0;
    	for(int i = 0;i < n; ++i){
    		scanf("%lld%lld",&a[i].x,&a[i].y);
    		ll xx = a[i].x, yy = a[i].y;
                    a[i].x = xx + yy;
                    a[i].y = xx - yy;
    		a[i].id = i;
    		sumx += a[i].x;
    		sumy += a[i].y;
    	}
    	sort(a,a + n, cmp1);
    	sum = 0;
    	for(int i = 0;i < n; ++i){
    		sum += a[i].x;
    		tx[a[i].id] = sumx - sum - (n-1-i)*a[i].x;
    		tx[a[i].id] += a[i].x*(i+1) - sum;
    	}
    
    
    	sort(a,a + n, cmp2);
    	sum = 0;
    	for(int i = 0;i < n; ++i){
    		sum += a[i].y;
    		ty[a[i].id] = sumy - sum - (n-1-i)*a[i].y;
    		ty[a[i].id] += a[i].y*(i+1) - sum;
    	}
    
    	ll ans = 1ll<<60;
    	for(int i = 0;i < n; ++i) ans = min(ans, tx[i] + ty[i]);
    	printf("%lld\n",ans/2);
    
    }
    
    
    • 1

    信息

    ID
    1026
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者