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);
题目链接: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 3320 · Jessica's Reading Problem【尺取法】
- poj 1658 Eva's Problem(水题)
- poj 3468 A Simple Problem with Integers
- http://poj.org/problem?id=3461&&kmp
- POJ 2826 An Easy Problem? 判断线段相交
- http://poj.org/problem?id=3083
- poj 3468 A Simple Problem with Integers 数据结构
- poj 3468 A Simple Problem with Integers(线段树区间更新)
- POJ 3468 A Simple Problem with Integers(线段树)
- poj 3468 A Simple Problem with Integers 树状数组 或 线段树
- POJ 1207 The 3n+1 problem(我的水题之路——输入输出要保持原有顺序)
- POJ 1000 A+B Problem 题解
- http://poj.org/problem?id=3781&&Nth Largest Value
- POJ 2484 A Funny Game(博弈)
- POJ 3468 A Simple Problem with Integers----线段树
- poj 3468 A Simple Problem with Integers
- POJ 3100 Root of the Problem(我的水题之路——取A^N最接近B的A)
- poj 3468: A Simple Problem with Integers (树状数组区间更新)
- POJ 3468 A Simple Problem with Integers
- POJ1681——Painter\'s Problem