Difference between Triplets
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 2476   Accepted: 800

Description

For every pair of triplets, T= (IaJaKa) and Tb = (IbJb, Kb), we define the difference
value
 between Ta and Tb as follows:

D(Ta, Tb) = max {Ia − IbJa − JbKa − Kb} − min {Ia − IbJa − JbKa − Kb}

Now you are given N triplets, could you write a program to calculate the sum of the difference values between every unordered pair of triplets?

Input

The input consists of several test cases. 

Each test case begins with a line containing an integer N, denotes the number of triplets. Assume that we number the triplets as T1T2, ... , TN. Then, there are following N lines,
each line contains three integers, giving the elements of each triplet. 

A case with N = 0 indicates the end of the input. 

Output

For each case, output a line with the sum of difference values between every unordered pair of triplets.

Sample Input

  1. 2
  2. 1 2 3
  3. 3 2 1
  4. 3
  5. 1 3 2
  6. 4 0 7
  7. 2 2 9
  8. 0

Sample Output

  1. 4
  2. 20

Hint

Case 1: D(T1,T2)=4 

Case 2: D(T1,T2)+D(T1,T3)+D(T2,T3)=8+8+4=20 



You can assume that N, the number of triplets in each case, will not exceed 200,000 and the elements in triplets fit into [-106,106]. 

The size of the input will not exceed 5 MB. 

Source

题目的意思很easy理解,我就不多说了。

用n^2算法必跪的啦,必需要O(n)

先要理解max{a,b,c}-min{a,b,c}=(|a-b|+|b-c|+|c-a|)/2

如果a>b>c,那么max{a-b-c}-min{a-b-c}=a-c

而(|a-b|+|b-c|+|c-a|)/2=(a-b+b-c+c-a)/2=a-c=max{a-b-c}-min{a-b-c}

而由位置的对称性,不管a,b,c关系怎样,我们总能够交换其似的a',b',c'保持a'>b'>c',同一时候结果总是一致。

而| (Ia-ka) -(Ib-kb)| = |(Ia-Ib)-(ka-kb)|

所以我们对于Ti=(Ia,Ib,Ic)能够先计算Ia-Ib,Ib-Ic,Ic-Ia

这样我们要计算D(Ti,Tj),则是求(|a-b|+|b-c|+|c-a|)/2.能够保持线性。仅仅要保证a>b, b>c, c>a,则能够将绝对值去掉。

进行排序,排序后a[i],b[i],c[i],a[i]做头的是i次,被减的是n-i-1次(从0起的坐标)

这样,ans=sum(i*(a[i]+b[i]+c[i])-(n-i-1)*(a[i]+b[i]+c[i]))

具体见代码。

思路的确比較坑。

  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. #define N 200005
  5. long long a[N],b[N],c[N];
  6. int main()
  7. {
  8. int i,n;
  9. long long x,y,z;
  10. cin.sync_with_stdio(false);
  11. while(cin>>n,n){
  12.  
  13. for(i=0;i<n;i++){
  14. cin>>x>>y>>z;
  15. a[i]=x-y,b[i]=y-z,c[i]=z-x;
  16.  
  17. }
  18. sort(a,a+n);
  19. sort(b,b+n);
  20. sort(c,c+n);
  21. long long ans=0LL;
  22. for(i=0;i<n;i++){
  23. ans+=(i*(a[i]+b[i]+c[i])-(n-i-1)*(a[i]+b[i]+c[i]));
  24. }
  25. cout<<ans/2<<endl;
  26.  
  27. }
  28. return 0;
  29. }

poj3244(公式题)的更多相关文章

  1. 华东交通大学2018年ACM“双基”程序设计竞赛 C. 公式题 (2) (矩阵快速幂)

    题目链接:公式题 (2) 比赛链接:华东交通大学2018年ACM"双基"程序设计竞赛 题目描述 令f(n)=2f(n-1)+3f(n-2)+n,f(1)=1,f(2)=2 令g(n ...

  2. HDU 4762 Cut the Cake (2013长春网络赛1004题,公式题)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. 牛客网 牛客小白月赛1 E.圆与三角形-公式题

    E.圆与三角形   链接:https://www.nowcoder.com/acm/contest/85/E来源:牛客网     这个题把公式推一下, 发现就是1+sinA*r,sinA最大为1,所以 ...

  4. ZOJ3558 How Many Sets III(公式题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud How Many Sets III Time Limit: 2 Seconds   ...

  5. codeforces 340C Tourist Problem(公式题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Tourist Problem Iahub is a big fan of tou ...

  6. 89. Gray Code(公式题)

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  7. codeforces GYM 100971F 公式题或者三分

    F. Two Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  8. 计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)

    A. An Olympian Math Problem 54.28% 1000ms 65536K   Alice, a student of grade 66, is thinking about a ...

  9. 【BZOJ1426】收集邮票 概率DP 论文题 推公式题

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

随机推荐

  1. mysql每个表总的索引大小

    /* 指定的数据库 每个表的索引 不包含主键索引的大小*/ ,),,),'mb') as index_size from information_schema.tables where TABLE_S ...

  2. MySql中允许远程连接

    要达到这个目的需要实现两点 开通用户权限 解除本地绑定 开通用户权限 首先登陆服务器端的mysql //不使用空格可以直接登陆 mysql -u用户名 -p密码 mysql> use mysql ...

  3. POJ——T 2449 Remmarguts' Date

    http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30754   ...

  4. HTML学习----------DAY2第四节

    HTML 文档是由 HTML 元素定义的. HTML 元素 HTML 元素指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 注释:开始标签常被称为开放标签(opening ...

  5. Codeforces 528A Glass Carving STL模拟

    题目链接:点击打开链接 题意: 给定n*m的矩阵.k个操作 2种操作: 1.H x 横向在x位置切一刀 2.V y 竖直在y位置切一刀 每次操作后输出最大的矩阵面积 思路: 由于行列是不相干的,所以仅 ...

  6. BZOJ 1112 线段树

    思路: 权值线段树 (找中位数用的) 记录下出现的次数和sum 一定要注意 有可能中位数的值有许多数 这怎么办呢 (离散化以后不去重就行了嘛--.) (为什么他们想得那么麻烦) //By Sirius ...

  7. SQL Server 2005高可用性模式下创建数据库镜像

    SQL Server 2005高可用性模式下创建数据库镜像   高可用性模式下创建数据库镜像 第一步: --创建镜像用数据库-在主服务器上操作 create database db_mirror on ...

  8. CentOS上搭建Tomcat环境并配置服务自启动

    下载安装JDK 卸载原装的OpenJDK(如果有) # 查看是否安装Java java -version # 查看Java的安装包信息 rpm -qa | grep java # 卸载原装Java,& ...

  9. css line-height详解

    行高指的是文本行的基线间的距离(更简单来说,行高是指文字尺寸与行距之间的和). 而基线(Base line),指的是一行字横排时下沿的基础线, 基线并不是汉字的下端沿,而是英文字母x的下端沿,同时还有 ...

  10. I帧、P帧和B帧的特点

    I帧:帧内编码帧 I帧特点: 1.它是一个全帧压缩编码帧.它将全帧图像信息进行JPEG压缩编码及传输; 2.解码时仅用I帧的数据就可重构完整图像; 3.I帧描写叙述了图像背景和运动主体的详情; 4.I ...