您的位置:首页 > 其它

poj 3244 (funny problem)

2013-09-26 22:07 302 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj