1. /*
  2. 写完这篇博客有很多感慨,过去一段时间都是看完题解刷题,刷题,看会题解,没有了大一那个时候什么都不会的时候刷题的感觉,
    这个题做了一天半,从开始到结束都是从头开始自己构思的很有感觉,找回到当初的感觉
  3. */

Disharmony Trees

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 90 Accepted Submission(s): 56
 
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
  1. 2
  2. 10 100
  3. 20 200
  4. 4
  5. 10 100
  6. 50 500
  7. 20 200
  8. 20 100
 
Sample Output
  1. 1
  2. 13
 
 
Source
2009 Multi-University Training Contest 12 - Host by FZU
 
Recommend
gaojie
 
  1. /*
  2. 错误思路:
  3. 按照高度排好序之后整个的求值过程就变成了h[1]*abs((n*a[1]-(a[2]+a[3]+..+a[n])))+h[2]*abs(((n-1)*a[2]-(a[3]+a[4]+..+a[n])))+.....
  4. 然后用树状数组维护a序列的值
  5. 解析:
  6. 因为绝对值的问题所以(n*a[1]-(a[2]+a[3]+..+a[n]))这个位置不能直接减去,要考虑大小才能把括号去掉
  7.  
  8. 正确思路:
  9. 按照高排好序之后,每棵树的min_h肯定是本身的h,abs_a的话需要考虑从i到n的a与本身的大小
  10. 树状数组维护两个值比自己小的有多少个,还有维护a数组,
  11. */
  12. #include<bits/stdc++.h>
  13. #define N 100005
  14. #define ll long long
  15. #define lowbit(x) x&(-x)
  16. using namespace std;
  17. struct node
  18. {
  19. ll x,h;
  20. ll rankx,rankh;
  21. node(){}
  22. node(ll a,ll b)
  23. {
  24. x=a;
  25. h=b;
  26. }
  27. };
  28. ll n;
  29. ll x,h;
  30. ll c[N];//用来构建树状数组维护到当前结点比当前结点小的个数
  31. ll sum[N];//用来维护到当前结点比当前结点小的数的总和
  32. node f[N];
  33. bool cmp1(node a,node b)
  34. {
  35. return a.x<b.x;
  36. }
  37. bool cmp2(node a,node b)
  38. {
  39. return a.h<b.h;
  40. }
  41. bool cmp3(node a,node b)//这次排序从大到小排序,这样能使从0到i的所有计算中用到的h都是h[i];
  42. {
  43. return a.h>b.h;
  44. }
  45. void init()
  46. {
  47. memset(c,,sizeof c);
  48. memset(sum,,sizeof sum);
  49. }
  50. void update1(ll x,ll val)
  51. {
  52. while(x<N)
  53. {
  54. c[x]+=val;
  55. x+=lowbit(x);
  56. }
  57. }
  58. void update2(ll x,ll val)
  59. {
  60. while(x<N)
  61. {
  62. sum[x]+=val;
  63. x+=lowbit(x);
  64. }
  65. }
  66. ll getsum1(ll x)//这个是获取有多少个比自己小的
  67. {
  68. ll s=;
  69. while(x>)
  70. {
  71. s+=c[x];
  72. x-=lowbit(x);
  73. }
  74. return s;
  75. }
  76. ll getsum2(ll x)//这个是获取比自己小的和
  77. {
  78. ll s=;
  79. while(x>)
  80. {
  81. s+=sum[x];
  82. x-=lowbit(x);
  83. }
  84. return s;
  85. }
  86. int main()
  87. {
  88. //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
  89. while(scanf("%lld",&n)!=EOF)
  90. {
  91. init();
  92. f[]=node(-,-);
  93. for(int i=;i<=n;i++)
  94. {
  95. scanf("%lld%lld",&x,&h);
  96. f[i]=node(x,h);
  97. }//处理输入
  98.  
  99. sort(f+,f+n+,cmp1);//先按照坐标的大小进行排序
  100. for(int i=;i<=n;i++)
  101. {
  102. if(f[i].x==f[i-].x)
  103. f[i].rankx=f[i-].rankx;
  104. else
  105. f[i].rankx=i;
  106. }
  107.  
  108. sort(f+,f+n+,cmp2);//然后按照树的高度的大小进行排序
  109. for(int i=;i<=n;i++)
  110. {
  111. if(f[i].h==f[i-].h)
  112. f[i].rankh=f[i-].rankh;
  113. else
  114. f[i].rankh=i;
  115. }
  116. sort(f+,f+n+,cmp3);//第三次排序保证每次运算用到的h都是h[i](也就是使得h[i]在0到i中最小;
  117. ll cur=;
  118. ll total=;
  119. ll same=;//和自己相同的个数
  120. ll low=;//比自己小的个数
  121. ll big=;//比自己大的个数
  122. ll low_sum=;//比自己小的和
  123. ll big_sum=;//比自己大的和
  124. //for(int i=1;i<=n;i++)
  125. // cout<<f[i].rankx<<" ";
  126. //cout<<endl;
  127. for(int i=;i<=n;i++)
  128. {
  129. total+=f[i].rankx;//表示到当前位置的总坐标值
  130.  
  131. /*更新当前的信息*/
  132. update1(f[i].rankx,);
  133. update2(f[i].rankx,f[i].rankx);
  134. //和当前位置相同的个数
  135. same=getsum1(f[i].rankh)-getsum1(f[i].rankh-);
  136. //比自己小的个数;
  137. low=getsum1(f[i].rankx-);
  138. //比自己大的个数
  139. big=i-same-low;
  140. //比自己小的和
  141. low_sum=getsum2(f[i].rankx-);
  142. //比自己大的和
  143. big_sum=total-low_sum-same*f[i].rankx;
  144. //cout<<"total ="<<total<<endl;
  145. //cout<<"getsum1(x) ="<<getsum1(x)<<endl;
  146. //cout<<"i-getsum1(x)-1 ="<<i-getsum1(x)-1<<endl;
  147. //cout<<"getsum2(x) ="<<getsum2(x)<<endl;
  148. //cout<<"total-getsum2(x)="<<total-getsum2(x)<<endl;
  149. //cout<<endl;
  150.  
  151. //比自己大的和减去比当前值大的和
  152. cur+=f[i].rankh*((low*f[i].rankx-low_sum)+(big_sum-big*f[i].rankx));
  153. //a[i]乘上比自己小的个数减去比自己小的和
  154. }
  155. printf("%lld\n",cur);
  156. }
  157. return ;
  158. }

Disharmony Trees的更多相关文章

  1. Disharmony Trees 树状数组

    Disharmony Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  2. hdu 3015 Disharmony Trees (离散化+树状数组)

    Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. Disharmony Trees HDU - 3015

    Disharmony Trees HDU - 3015 One day Sophia finds a very big square. There are n trees in the square. ...

  4. hdu3015 Disharmony Trees

    Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...

  5. HDU-3015 Disharmony Trees [数状数组]

    Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...

  6. 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) 求出 ...

  7. HDU 3015 Disharmony Trees

    题解:在路边有一行树,给出它们的坐标和高度,先按X坐标排序.记录排名,记为rankx,再按它们的高度排序,记录排名,记为rankh.两颗树i,j的差异度为 fabs(rankx[i]-rankx[j] ...

  8. HDU 3015 Disharmony Trees 【 树状数组 】

    题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx, 再按照它们的高度排序,记为hh 两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[ ...

  9. Disharmony Trees HDU - 3015 树状数组+离散化

    #include<cstdio> #include<cstring> #include<algorithm> #define ll long long using ...

随机推荐

  1. java集合系列——List集合之LinkedList介绍(三)

    1. LinkedList的简介 JDK 1.7 LinkedList是基于链表实现的,从源码可以看出是一个双向链表.除了当做链表使用外,它也可以被当作堆栈.队列或双端队列进行操作.不是线程安全的,继 ...

  2. UI自动化测试简介及Selenium工具的介绍和环境搭建

    自动化测试简介 1.1何为自动化测试? 是把以人为驱动的测试转化为机器执行的一种过程,它是一种以程序测试程序的过程.换言之,就是以程序实现的方式来代替手工测试. 1.2自动化测试分类 分为功能自动化测 ...

  3. vue-resource传参数到后端,后端取不到数据的问题

    先上一段代码: this.$http.post('xxx',{Search_Text:this.search_text}).then(function(response){ // 响应成功回调 thi ...

  4. URL不能过长,否则summit方法提交失败

    MVC5.0+EF6.0,和浏览器的版本有关系.IE最多1024KB. URL不能过长,否则summit方法提交失败.

  5. 【转】 文档与笔记利器 reStructuredText 和 Sphinx

    关于制作文档和笔记这种事,我已经纠结了很久,网上解决方案也一大推,我试过几样,ScrapBook 和 Zotero,编辑不太方便,同步麻烦.Google Note 过于格式简单,现在也不更新了,Goo ...

  6. zoj1383 zoj3418 二进制 基础

    传送门 题目大意:从末位到首位输出所在位置的值是1的位置. 可以用除---->  num>>1  或减----> -(n^(-n)) #include<cstdio> ...

  7. MSSQL查询数据分页

    这几天刚好碰到数据的分页查询,觉得不错,Mark一下,方法有两种,都是使用select top,效率如何就不在这讨论 方法1:利用select top配合not in(或者not exists),查询 ...

  8. SqlServer和Oracle中一些常用的sql语句8 触发器和事务

    --创建和执行事后触发器 --更新仓库备份表中记录时自动创建数据表且插入三条记录 create trigger db_trigger1 on 仓库备份 for update as begin if E ...

  9. java优雅的使用elasticsearch api

    本文给出一种优雅的拼装elasticsearch查询的方式,可能会使得使用elasticsearch的方式变得优雅起来,使得代码结构很清晰易读. 建立elasticsearch连接部分请参看另一篇博客 ...

  10. 深刻理解反射(Reflection)

    最近公司在搞自动化测试,由于版权问题,无法用 '录制脚本' 进行,也就没法用 VS 自带的 UITest 框架(蛋疼), 所以只能开源的 FlaUI 框架来搞了.其中不可避免的涉及到反射的应用,但自己 ...