poj3244(公式题)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 2476 | Accepted: 800 |
Description
For every pair of triplets, Ta = (Ia, Ja, Ka) and Tb = (Ib, Jb, Kb), we define the difference
value between Ta and Tb as follows:
D(Ta, Tb) = max {Ia − Ib, Ja − Jb, Ka − Kb} − min {Ia − Ib, Ja − Jb, Ka − 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
Each test case begins with a line containing an integer N, denotes the number of triplets. Assume that we number the triplets as T1, T2, ... , 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
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 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(公式题)的更多相关文章
- 华东交通大学2018年ACM“双基”程序设计竞赛 C. 公式题 (2) (矩阵快速幂)
题目链接:公式题 (2) 比赛链接:华东交通大学2018年ACM"双基"程序设计竞赛 题目描述 令f(n)=2f(n-1)+3f(n-2)+n,f(1)=1,f(2)=2 令g(n ...
- 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 ...
- 牛客网 牛客小白月赛1 E.圆与三角形-公式题
E.圆与三角形 链接:https://www.nowcoder.com/acm/contest/85/E来源:牛客网 这个题把公式推一下, 发现就是1+sinA*r,sinA最大为1,所以 ...
- ZOJ3558 How Many Sets III(公式题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud How Many Sets III Time Limit: 2 Seconds ...
- codeforces 340C Tourist Problem(公式题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Tourist Problem Iahub is a big fan of tou ...
- 89. Gray Code(公式题)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- codeforces GYM 100971F 公式题或者三分
F. Two Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 计蒜客 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 ...
- 【BZOJ1426】收集邮票 概率DP 论文题 推公式题
链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...
随机推荐
- 监控web服务(http,本地 / 远程监控nginx)
监控 httpd 服务一: #!/bin/bash #描述: 秒级别监控 http 服务 while [ 1 -lt 2 ] do sleep 10 ai=`netstat -ntl | grep & ...
- 洛谷 P1171 售货员的难题
P1171 售货员的难题 题目背景 数据有更改 题目描述 某乡有n个村庄(1<n<20),有一个售货员,他要到各个村庄去售货,各村庄之间的路程s(0<s<1000)是已知的,且 ...
- OpenCV图像处理篇之边缘检測算子
3种边缘检測算子 灰度或结构等信息的突变位置是图像的边缘,图像的边缘有幅度和方向属性.沿边缘方向像素变化缓慢,垂直边缘方向像素变化剧烈.因此,边缘上的变化能通过梯度计算出来. 一阶导数的梯度算子 对于 ...
- ZOJ 1654 Place the Robots (二分匹配 )
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=654 Robert is a famous engineer. One ...
- cocos2d-x 2.2.0 怎样在lua中注冊回调函数给C++
cocos2d-x内部使用tolua进行lua绑定.可是引擎并没有提供一个通用的接口让我们能够把一个lua函数注冊给C++层面的回调事件. 翻看引擎的lua绑定代码,我们能够仿照引擎中的方法来做. 值 ...
- 一张图片让你了解android的事件分发机制
- Understanding IIS Bindings, Websites, Virtual Directories, and lastly Application Pools
In a recent meeting, some folks on my team needed some guidance on load testing the Web application ...
- 1.MVC概要与angular概要、模板与数据绑定
转自:https://www.cnblogs.com/best/p/6242011.html#_label3_0_8_2 一.前端MVC概要 1.1.库与框架的区别 1.2.AMD与CMD 1.2.前 ...
- 实现人脸识别性别之路---try语句的使用
Try语句 用法:处理异常信息 存在的形式:try-except X-except T...-except-else-finally(其中X T为错误的类型) 表达意思:try语句是执行正常语句,如果 ...
- gitlab-ce-11.0.1 安装及汉化
1.添加gitlab源(我这里使用了清华大学的源)cat <<EOF> /etc/yum.repos.d/gitlab-ce.repo[gitlab-ce]name=gitlab-c ...