博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-3015 Disharmony Trees [数状数组]
阅读量:4471 次
发布时间:2019-06-08

本文共 3041 字,大约阅读时间需要 10 分钟。

Problem Description
One day Sophia finds a very big square. There are n trees in the square. They are all so tall. Sophia is very interesting in them.
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.
 
Input
There 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.
 
Output
For 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

 

题意:求∑( abs(Xi-Xj)*min(Hi,Hj) )

解:离散化X和H,对H升序排序,由于计算对H取的是最小值,因此只要考虑每项对后续的贡献;

对于每一项i,∑( abs(Xi-Xj) )可以表达为 (前项个数*Xi - 前项总和) + (后项总和 - 后项个数*Xi );因此用两个树状数组分别维护个数和总和,遍历一遍求和,对每项计算完贡献后在数状数组中删去。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define ll long long#define ull unsigned long long#define lowbit(x) (x&(-x))#define eps 0.00000001#define PI acos(-1)#define pn printf("\n");using namespace std;const int maxn = 1e5 + 5;int n;struct node{ ll x, h;}arr[maxn];ll cnt[maxn], sum[maxn];void update(ll pos,ll val, ll *c){ while(pos <= maxn) { c[pos] += val; pos += lowbit(pos); }}ll query(ll pos, ll*c){ ll ret = 0; while(pos > 0) { ret += c[pos]; pos -= lowbit(pos); } return ret;}int main(){ while(~scanf("%d", &n)) { memset(cnt, 0, sizeof cnt); memset(sum ,0, sizeof sum); for(int i=0;i

 

转载于:https://www.cnblogs.com/HazelNut/p/10300447.html

你可能感兴趣的文章
如何优化limit
查看>>
几种常用数据库字段类型查询语句
查看>>
提高效率必须改掉的7种习惯
查看>>
Java判断语句中判断条件的执行顺序
查看>>
Windows平台下tomcat+java的web程序持续占cpu问题调试
查看>>
OO第四次博客作业!
查看>>
HDU 吉哥系列故事——完美队形II 騰訊馬拉松初賽第二輪D題
查看>>
[转]SQL Server 性能调优(io)
查看>>
设计模式学习-每日一记(6.原型模式)
查看>>
LeetCode--018--四数之和(java)
查看>>
Redis消息队列
查看>>
电商网站架构设计
查看>>
http://jingyan.baidu.com/article/4dc40848e7b69bc8d946f127.html
查看>>
WCF netTcp配置
查看>>
数据类型转换
查看>>
Nodejs学习笔记(2) 阻塞/非阻塞实例 与 Nodejs事件
查看>>
什么是FreeMaker
查看>>
设计模式学习笔记(总结篇:模式分类)
查看>>
TCP的三次握手/建立连接
查看>>
Python 教程阅读笔记(一):使用解释器
查看>>