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

2
1 2 3
3 2 1
3
1 3 2
4 0 7
2 2 9
0

Sample Output

4
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]))

具体见代码。

思路的确比較坑。

#include <iostream>
#include <algorithm>
using namespace std;
#define N 200005
long long a[N],b[N],c[N];
int main()
{
int i,n;
long long x,y,z;
cin.sync_with_stdio(false);
while(cin>>n,n){ for(i=0;i<n;i++){
cin>>x>>y>>z;
a[i]=x-y,b[i]=y-z,c[i]=z-x; }
sort(a,a+n);
sort(b,b+n);
sort(c,c+n);
long long ans=0LL;
for(i=0;i<n;i++){
ans+=(i*(a[i]+b[i]+c[i])-(n-i-1)*(a[i]+b[i]+c[i]));
}
cout<<ans/2<<endl; }
return 0;
}

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. perl模块

    查看perl模块安装目录:find `perl -e ‘print “@INC”‘` -name ‘*.pm’ -print 为什么要写或要模块呢?简言之:代码重用,更多见于写一组工具集,有很多地方是 ...

  2. rsync---远程数据同步工具

    rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而 ...

  3. Oracle基础入门(三)

    一:PLsql一些基本操作 调节plsql的字体大小 二:创建表,如果学过sql server的数据库就会发现其实Oracle跟的一些新建表和新增修改其实是差不多的 新建表 Create table ...

  4. CMSIS-RTOS 信号量

    信号量Semaphores 和信号类似,信号量也是一种同步多个线程的方式,简单来讲,信号量就是装有一些令牌的容器.当一个线程在执行过程中,就可能遇到一个系统调用来获取信号量令牌,如果这个信号量包含多个 ...

  5. CSU 1249 竞争性酶抑制剂和同工酶

    1249: 竞争性酶抑制剂和同工酶 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 109  Solved: 49 Description 人体内很多化学 ...

  6. poj3169 差分约束系统

    题意: 从1到n,n个数,从左向右依次排列. 给定两种形式的约束条件: 1.xi与yi的最大距离为dk 2.xi与yi的最小距离为dk 问满足这些限定条件的情况下,数1和n的最大距离是多少?(若约束条 ...

  7. OpenCV【2】---读取png图片显示到QT label上的问题

    问题一:   操作图片test.png是一个365x365的PNG图片   通过OpenCV自带的GUI显示出来图像是没问题的,例如以下操作代码所看到的: QStringfileName=QFileD ...

  8. Android活动状态和生存期

    活动状态 1.运行状态(顶层的活动) 2.暂停状态(非顶层的,可见的活动) 3.停止状态(非顶层的,不可见的活动) 4.销毁状态(保证手机的内存充足) 活动的生存期 完整的生存期 onCreate活动 ...

  9. Entity Framework之Model First开发方式

    一.Model First开发方式 在项目一开始,就没用数据库时,可以借助EF设计模型,然后根据模型同步完成数据库中表的创建,这就是Model First开发方式.总结一点就是,现有模型再有表. 二. ...

  10. RedHat Linux 下安装、测试摄像头

    RedHat Linux 下安装.测试摄像头(全文见附件)     随着视频电话的迅速发展我相信大家一定有过在Windows 下安装摄像头的经历,然而大多数 都不支持Linux .我现以罗技摄像头为例 ...