Disharmony Trees HDU - 3015
Disharmony Trees HDU - 3015
She finds that trees maybe disharmony and the Disharmony Value between two trees is associated with two value called FAR and SHORT.
The FAR is defined as the following:If we rank all these trees according to their X Coordinates in ascending order.The tree with smallest X Coordinate is ranked 1th.The trees with the same X Coordinates are ranked the same. For example,if there are 5 tree with X Coordinates 3,3,1,3,4. Then their ranks may be 2,2,1,2,5. The FAR of two trees with X Coordinate ranks D1 and D2 is defined as F = abs(D1-D2).
The SHORT is defined similar to the FAR. If we rank all these trees according to their heights in ascending order,the tree with shortest height is ranked 1th.The trees with the same heights are ranked the same. For example, if there are 5 tree with heights 4,1,9,7,4. Then their ranks may be 2,1,5,4,2. The SHORT of two trees with height ranks H1 and H2 is defined as S=min(H1,H2).
Two tree’s Disharmony Value is defined as F*S. So from the definition above we can see that, if two trees’s FAR is larger , the Disharmony Value is bigger. And the Disharmony value is also associated with the shorter one of the two trees.
Now give you every tree’s X Coordinate and their height , Please tell Sophia the sum of every two trees’s Disharmony value among all trees.
InputThere are several test cases in the input
For each test case, the first line contain one integer N (2 <= N <= 100,000) N represents the number of trees.
Then following N lines, each line contain two integers : X, H (0 < X,H <=1,000,000,000 ), indicating the tree is located in Coordinates X and its height is H.OutputFor each test case output the sum of every two trees’s Disharmony value among all trees. The answer is within signed 64-bit integer.Sample Input
2
10 100
20 200
4
10 100
50 500
20 200
20 100
Sample Output
1
13
题意:对于n棵树,给出所在位置和高度,然后分别对它的位置和高度做如下处理:
位置:将位置升序排序,最小的定义等级为 1,次小的定义等级为2,但是,要是位置相同的,则等级定义要相同;
例如:位置 1,2,1,5,2,3
等级 1,3,1,6,3,5
对于高度也是做如上处理;
然后,定义f=两树之间的距离差的绝对值,s=两树中最小的高度,求所有树之间f*s和。
题解:树状数组。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 2e5+;
const int mod = 1e9+; struct node
{
ll x,h;
}T[];
int n; bool cmp(node s1,node s2)
{
return s1.x<s2.x;
}
bool cmp1(node s1,node s2)
{
return s1.h<s2.h;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int i,int val,int *s)
{
while(i<=n)
{
s[i] += val;
i += lowbit(i);
}
}
int sum(int i,int *s)
{
int res=;
while(i>)
{
res += s[i];
i -= lowbit(i);
}
return res;
} int main()
{
while(~scanf("%d",&n))
{
int c[],c1[];
for (int i = ; i <= n; i++)
scanf("%lld %lld", &T[i].x, &T[i].h);
sort(T + , T + n + , cmp);
int xx = T[].x;
T[].x = ;
for (int i = ; i <= n; i++) //将T[i].x按照从小到大排序
{
if (T[i].x == xx)
T[i].x = T[i - ].x;
else {
xx = T[i].x;
T[i].x = i;
}
}
int xxmaxn = T[n].x;
sort(T + ,T + n +,cmp1);
xx = T[].h;
T[].h = ;
for (int i = ; i <= n; i++) //将T[i].h按照从小到大排序
{
if (T[i].h == xx)
T[i].h = T[i - ].h;
else {
xx = T[i].h;
T[i].h = i;
}
}
ll ans = ;
sort(T+,T++n,cmp1);
memset(c,,sizeof c);
memset(c1,,sizeof c1);
for(int i=;i<=n;i++)
{
add(T[i].x,T[i].x,c); // 记录所有比这个数小的和,把每个等级的数放到对应的位置上
add(T[i].x,,c1); //记录所有比这个数小的个数 每个点上记为1
}
int xiao,da; //xiao 表示比对应的数a小,反之亦然
for(int i = ;i<n;i++)
{
xiao = sum(T[i].x-,c1) * T[i].x - sum(T[i].x-,c); //找出比这个数小的个数*这个数-比这个数小的所有数之和
da = (sum(xxmaxn,c) - sum(T[i].x,c)) - (sum(xxmaxn,c1) - sum(T[i].x,c1)) * T[i].x; //找出比这个数大的数和-这个数*比这个数大的个数
ans += (xiao + da) * T[i].h;
add(T[i].x,-T[i].x,c); //把这个用过的数删除
add(T[i].x,-,c1); //把这个数位置上减去1
}
printf("%lld\n",ans);
} }
Disharmony Trees HDU - 3015的更多相关文章
- Disharmony Trees HDU - 3015 树状数组+离散化
#include<cstdio> #include<cstring> #include<algorithm> #define ll long long using ...
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Disharmony Trees 树状数组
Disharmony Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- Disharmony Trees
/* 写完这篇博客有很多感慨,过去一段时间都是看完题解刷题,刷题,看会题解,没有了大一那个时候什么都不会的时候刷题的感觉,这个题做了一天半,从开始到结束都是从头开始自己构思的很有感觉,找回到当初的感觉 ...
- C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥
C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...
- HDU 3015 Disharmony Trees(树状数组)
题意:给你n棵树,每棵树上有两个权值X H 对于X离散化 :3 7 1 5 3 6 -> 2 6 1 4 2 5,对于H一样 然后F = abs(X1-X2) S=min(H1,H2) 求出 ...
- HDU 3015 Disharmony Trees
题解:在路边有一行树,给出它们的坐标和高度,先按X坐标排序.记录排名,记为rankx,再按它们的高度排序,记录排名,记为rankh.两颗树i,j的差异度为 fabs(rankx[i]-rankx[j] ...
- HDU 3015 Disharmony Trees 【 树状数组 】
题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx, 再按照它们的高度排序,记为hh 两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[ ...
- Eat the Trees hdu 1693
Problem DescriptionMost of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...
随机推荐
- ForkJoin有参无返回值、有参有返回值实例
介绍: a . Fork/Join为JKD1.7引入,适用于对大量数据进行拆分成多个小任务进行计算的框架,最后把所有小任务的结果汇总合并得到最终的结果 b . 相关类 public abstract ...
- Http请求get、post工具类
在网上找了好久都没有找到post.get请求的工具类,现在整理了一下分享出来.http工具类如下: package com.qlwb.business.util; import java.io.Buf ...
- Sql server 数据库的备份和还原数据库提示“ 加载的介质已格式化为支持 1 个介质簇,但根据指定的备份设备,应支持 2 个介质簇”
数据库备份和还原总结 在 "M:\2017-Pro\company\other\databak_2014-10\anquanbaowei_db_201704300200.BAK" ...
- Mongodb简介及基本操作
一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性 ...
- bootstrapValidator 重置表单
最近在公司做业务系统后台,用的js框架还是jquery ui框架 是以bootstrap为基础的beyondadmin-v1.4.s3这套,用起来还挺不错,所以公司所有的后台ui都使用这套.这套ui ...
- C# Dictionary的遍历
foreach (KeyValuePair<string, string> kvp in dic) { Console.WriteLine("key:{0},value:{1}& ...
- Spring Boot概要
1.Spring Boot使用“习惯优于配置”(项目中存在大量的配置,此外还内置了一个习惯性的配置)的理念,使用户的项目实现快速运行.通过学习Spring Boot中的配置文件application. ...
- 正则表达式---01 js篇
本文主要针对js中正则表达式的实践操作,来让大家对正则表达式有一个入门清晰的了解. 正则表达式推荐学习网址:http://www.runoob.com/regexp/regexp-tutorial.h ...
- C++ vector类详解
转自http://blog.csdn.net/whz_zb/article/details/6827999 vector简介 vector是STL中最常见的容器,它是一种顺序容器,支持随机访问.vec ...
- java Vamei快速教程13 String类
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 之前的Java基础系列中讨论了Java最核心的概念,特别是面向对象的基础.在Jav ...