首页 技术 正文
技术 2022年11月21日
0 收藏 659 点赞 4,048 浏览 3479 个字

Disharmony Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 663    Accepted Submission(s): 307

Problem DescriptionOne 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.
hdu 3015 Disharmony Trees (离散化+树状数组)
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 Input210 10020 200410 10050 50020 20020 100 Sample Output113 Source2009 Multi-University Training Contest 12 – Host by FZU Recommendgaojie   |   We have carefully selected several similar problems for you:  3016 3011 3013 3018 3030  

 //203MS    4548K    1948 B    C++
/*
题意:
给出n个树的位置和高度,求每两棵数的位置排名差绝对值与高度排名较小的的积的和。 离散化+树状数组:
不错的一道题,我卡在结果的计算,的确对树状数组计算的能力的理解不够!
用两个排序排出位置和高度的排名,然后再对高度排名在进行计算,计算公式:
ans+=p[i].hid*(cnt*p[i].xid-sum+tsum-sum-(i-cnt-1)*p[i].xid); //每个高度
算法时间复杂度为O(n*lgn) */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 100005
struct node{
int x,h;
int xid,hid;
int id;
}p[N],p1[N],p2[N];
__int64 cnum[N],clen[N];
int cmpx(const void *a,const void*b)
{
return (*(node*)a).x-(*(node*)b).x;
}
int cmph(const void*a,const void*b)
{
return (*(node*)a).h-(*(node*)b).h;
}
int cmp0(const void*a,const void*b)
{
return (*(node*)b).hid-(*(node*)a).hid;
}
int lowbit(int k)
{
return (-k)&k;
}
void update(__int64 c[],int k,int detal)
{
for(;k<N;k+=lowbit(k))
c[k]+=detal;
}
__int64 getsum(__int64 c[],int k)
{
__int64 s=;
for(;k>;k-=lowbit(k))
s+=c[k];
return s;
}
int main(void)
{
int n,x,h;
while(scanf("%d",&n)!=EOF)
{
memset(cnum,,sizeof(cnum));
memset(clen,,sizeof(clen));
for(int i=;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].h);
p[i].id=i;
p1[i]=p2[i]=p[i];
}
qsort(p1+,n,sizeof(p1[]),cmpx);
for(int i=,j=;i<=n;i++){
if(i> && p1[i].x!=p1[i-].x) j=i;
p[p1[i].id].xid=j;
}
qsort(p2+,n,sizeof(p2[]),cmph);
for(int i=,j=;i<=n;i++){
if(i> && p2[i].h!=p2[i-].h) j=i;
p[p2[i].id].hid=j;
}
qsort(p+,n,sizeof(p[]),cmp0);
__int64 ans=;
//for(int i=1;i<=n;i++) printf("*%d %d\n",p[i].hid,p[i].xid);
update(cnum,p[].xid,);
update(clen,p[].xid,p[].xid);
for(int i=;i<=n;i++){
__int64 cnt=getsum(cnum,p[i].xid);
__int64 sum=getsum(clen,p[i].xid);
__int64 tsum=getsum(clen,N-);
ans+=p[i].hid*(cnt*p[i].xid-sum+tsum-sum-(i-cnt-)*p[i].xid);
update(cnum,p[i].xid,);
update(clen,p[i].xid,p[i].xid);
}
printf("%I64d\n",ans);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,944
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,469
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,283
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,099
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,729
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,766