Disharmony Trees

原创 2016年08月29日 15:39:48
链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3015

题目: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.

题意:给一排树的x坐标和高度,求总不和谐值,不和谐值指任意两棵树的横坐标(要求离散化)的差乘以两者的最小高度(要求离散化)。

分析:当时能想到用树状数组。不过在最后一步不会处理。后来了解到用两个树状数组,分别记录高度和x坐标,并且是从靠后的开始对之前存进去的树进行计算,计算方法还是比较诡异的。我暂时还没有找到很好的理解方式。

题解:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
using namespace std;

int n;

#define BIT_SIZE 100011
class BIT
{
public:
	BIT(){for(int i=1;i<BIT_SIZE;i++)c[i]=0;}
	void update(int i,__int64 val){while(i<BIT_SIZE){c[i]+=val;i+=i&(-i);}}
	__int64 sum(int i){__int64 ans=0;while(i>0){ans+=c[i];i-=i&(-i);}return ans;}
	__int64 get(int i){if(i>0)return sum(i)-sum(i-1);return 0;}
	void clear(){for(int i=1;i<BIT_SIZE;i++)c[i]=0;}
private:
	__int64 c[BIT_SIZE];
};

struct tre{
	int x;
	int h;
}s[100010],st[100010];

bool cmp1(tre p,tre q)
{
	return p.x<q.x;
}

bool cmp2(tre p,tre q)
{
	return p.h<q.h;
}

int main()
{
	//freopen("in.txt","r",stdin);
	while(~scanf("%d",&n))
	{
		BIT bita,bitb;
		memset(s,0,sizeof s);
		for(int i=1;i<=n;i++)
			scanf("%d %d",&s[i].x,&s[i].h);
		sort(s+1,s+n+1,cmp1);
		memcpy(st,s,sizeof s);
		for(int i=1;i<=n;i++){
			if(s[i].x!=s[i-1].x)
				st[i].x=i;
			else
				st[i].x=st[i-1].x;
		}
		sort(st+1,st+n+1,cmp2);
		memcpy(s,st,sizeof s);
		for(int i=1;i<=n;i++){
			if(st[i].h!=st[i-1].h)
				s[i].h=i;
			else
				s[i].h=s[i-1].h;
		}
		__int64 ans=0,temp=0;
		for(int i=n;i>0;i--){
			ans+=bitb.sum(s[i].x-1)*s[i].x*s[i].h;
			ans-=s[i].h*bita.sum(s[i].x-1);
			ans+=s[i].h*(bita.sum(BIT_SIZE-1)-bita.sum(s[i].x));
			ans-=(bitb.sum(BIT_SIZE-1)-bitb.sum(s[i].x))*s[i].x*s[i].h;
			bita.update(s[i].x,s[i].x);
			bitb.update(s[i].x,1);
		}
		printf("%I64d\n",ans);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU - 3015 Disharmony Trees

题意:给你n棵树的坐标x,高度h,让你分别按坐标,高度排序后,得到一新的序列,也可以理解为一颗组合成的新树,这棵树的坐标,高度都是排序来的,看了别人的解释,还是理解了老半天,然后就是求花费了,每任意两...
  • u011345136
  • u011345136
  • 2014年04月07日 12:49
  • 798

hdu 3015 Disharmony Trees

点击打开hdu 3015 思路: 树状数组 分析: 1 题目给定n棵树的横坐标和高度,然后给定横坐标排序后的rank_f已及树的高度排序后的rank_s,规定两颗树的FAR为abs(rank_f...
  • cgl1079743846
  • cgl1079743846
  • 2013年08月19日 14:45
  • 1260

hdu3015 Disharmony Trees

Problem Description One day Sophia finds a very big square. There are n trees in the square. They...
  • Kirito_Acmer
  • Kirito_Acmer
  • 2015年06月10日 20:32
  • 557

hdu3015 Disharmony Trees(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3015 题意:在路边有一行树,给出它们的坐标和高度,先按X坐标排序。记录排名,记为rankx,再按它们...
  • fire__ice
  • fire__ice
  • 2012年09月30日 17:38
  • 487

hdu 3015 Disharmony Trees

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015   题目大意: 在一片树林有n棵树(你   思路:最暴力的解法(n*n),n最大是...
  • gotoac
  • gotoac
  • 2012年05月08日 22:52
  • 424

HDU3015 Disharmony Trees

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other...
  • eeeaaaaa
  • eeeaaaaa
  • 2014年07月19日 16:59
  • 201

HDU 3015 Disharmony Trees

转载请注明出处:忆梦http://blog.csdn.net/yimeng2013/article/details/13033965 题目链接:http://acm.hdu.edu.cn/sho...
  • fjy4328286
  • fjy4328286
  • 2013年10月29日 17:25
  • 508

HDU 3015 Disharmony Trees 树状数组

来源:http://acm.hdu.edu.cn/showproblem.php?pid=3015 题意:有一些树,这些树的高度和位置给出。现在高度和位置都按从小到大排序,对应一个新的rank,任意...
  • wmn_wmn
  • wmn_wmn
  • 2012年09月29日 21:38
  • 1131

【TOJ 3401.】Disharmony Trees【树状数组】

题意:给出n个树的两个变量,一个是x
  • u010089558
  • u010089558
  • 2014年08月13日 13:33
  • 292

HDU 3015 Disharmony Trees (线段树)

题目大意:给定一组x[], 一组h[], 求所有的sum(min(h1,h2)*abs(x1,x2))。     解题报告:首先,h1,h2间的较小值,我们可以将h[]从大到小排序,每次取得的h都比先...
  • kbdwo
  • kbdwo
  • 2013年09月26日 13:44
  • 475
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Disharmony Trees
举报原因:
原因补充:

(最多只允许输入30个字)