poj 3244 (funny problem)

poj 3244


题目链接:http://poj.org/problem?id=3244


思路:max{a,b,c} - min{a,b,c} = ( |  a  -  b | +| b  -  c |+| c -  a| )/2; 题目中的是



那么上面的式子就可以转换为  |(x1  - y1) - ( x2 - y2)|+|(y1 - z1) - (y2 - z2)|+|(z1 - x1) - (z2 - x2)|  ,对于n 个式子,如果去绝对值的话,知道大小就方便了,排序,也就是说,我们要知道对于当前的数,是做了几次减数和几次被减数,也就是比自己小的数量和比自己大的数量。 O(n * lg n);


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define maxn 200010
#define LL long long
LL a[maxn],b[maxn],c[maxn];

bool cmp(LL x,LL y){
    if(x>y) return true;
    return false;
}
int main()
{
    LL n,x,y,z;
    LL sum ;
    while(~scanf("%I64d",&n)){
        if(n == 0) break;
        sum = 0;
        for(LL i=0;i<n;i++){
            scanf("%I64d %I64d %I64d",&x,&y,&z);
            a[i] = x - y;
            b[i] = y - z;
            c[i] = z - x;
        }
        sort(a,a+n,cmp);
        sort(b,b+n,cmp);
        sort(c,c+n,cmp);

        for(int i=0;i<n;i++){
            sum += (a[i] * (n - i) - a[i] * i);
            sum += (b[i] * (n - i) - b[i] * i);
            sum += (c[i] * (n - i) - c[i] * i);
        }
        printf("%I64d\n",sum/2);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值