HDU 3015 Disharmony Trees
题解:在路边有一行树,给出它们的坐标和高度,先按X坐标排序。记录排名,记为rankx,再按它们的高度排序,记录排名,记为rankh。两颗树i,j的差异度为
fabs(rankx[i]-rankx[j])*min(rankh[i],rankh[j])
最后求出任异两颗树差异度的和。
题解:首先,需要解决的是min(rh)的问题,对于这个问题,只要离散化之后按rh从大到小排序顺序求解即可,然后用树状数组维护之前出现rx比当前值小的个数与总和,那么同时就可以计算rx比当前大的个数和总和了,那么就可以依次计算出答案了。
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=100010;
struct node{long long x,h,rx,rh;}a[N];
int s[N],c[N],n;
long long ans,ns;
bool cmp1(node a,node b){return a.x<b.x;}
bool cmp2(node a,node b){return a.h<b.h;}
bool cmp3(node a,node b){return a.h>b.h;}
void updata(int x,int num){while(x<=n)c[x]++,s[x]+=num,x+=x&-x;}
int time(int x){int t=0;while(x>0)t+=c[x],x-=x&-x;return t;}
int sum(int x){int t=0;while(x>0)t+=s[x],x-=x&-x;return t;}
int main(){
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++)s[i]=c[i]=0;
for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].h);
sort(a+1,a+n+1,cmp1);a[1].rx=1;
for(int i=2;i<=n;i++)a[i].rx=(a[i].x==a[i-1].x?a[i-1].rx:i);
sort(a+1,a+n+1,cmp2);a[1].rh=1;
for(int i=2;i<=n;i++)a[i].rh=(a[i].h==a[i-1].h?a[i-1].rh:i);
sort(a+1,a+n+1,cmp3);
ans=ns=0;
for(int i=1;i<=n;i++){
long long t=time(a[i].rx),m=sum(a[i].rx);
ans+=a[i].rh*(a[i].rx*t-m+ns-m-a[i].rx*(i-t-1));
updata(a[i].rx,a[i].rx);
ns+=a[i].rx;
}
cout<<ans<<endl;
}
return 0;
}
HDU 3015 Disharmony Trees的更多相关文章
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 【 树状数组 】
题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx, 再按照它们的高度排序,记为hh 两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[ ...
- Disharmony Trees HDU - 3015
Disharmony Trees HDU - 3015 One day Sophia finds a very big square. There are n trees in the square. ...
- Disharmony Trees 树状数组
Disharmony Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- Disharmony Trees
/* 写完这篇博客有很多感慨,过去一段时间都是看完题解刷题,刷题,看会题解,没有了大一那个时候什么都不会的时候刷题的感觉,这个题做了一天半,从开始到结束都是从头开始自己构思的很有感觉,找回到当初的感觉 ...
- Disharmony Trees HDU - 3015 树状数组+离散化
#include<cstdio> #include<cstring> #include<algorithm> #define ll long long using ...
- HDU 2841 Visible Trees 数论+容斥原理
H - Visible Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- hdu3015 Disharmony Trees
Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...
随机推荐
- 查询离指定日期最近的一条数据(oracle)
select * from ( Select * from t_currency_rate where f_orig_curr='USD' and f_dest_curr='RMB ...
- sql 练习(3)
1.打印九九乘法表 ) ,exp)) A, ,exp))B, ,exp))C, ,exp))D, ,exp))E, ,exp))F, ,exp))G, ,exp))H, ,exp))I from ( ...
- Intellij idea workflow 工作流插件安装
idea提供支持的工作插件名字叫actiBPM,可以在idea中在线安装,但往往会连接不成功安装失败,所以这里提供了硬盘安装的方式: 首先是要去官网下载actiBPM插件,下载地址: http://p ...
- URL伪静态设置 (apache2.4)
` ` 1.修改apche主配置文件 主要是 #LoadModule rewrite_module modules/mod_rewrite.so 改为 LoadModule rewrite_modul ...
- jQuery入门第三
jQuery入门第三 1.HTML 2.CSS 衣服 3.javascript 可以动的人 4.DOM 编程 对html文档的节点操作 5.jQuery 对 javascript的封装 简练的语法 复 ...
- Windows 取得至高无上的权限
第一步:gpedit.msc 第二步:计算机配置-->windows 设置 -->安全设置 -->安全选项 -->用户账户控制 -->以管理员批准模式运行所有管理员 -- ...
- JS严格模式和非严格模式的区别
严格模式和非严格模式的区别 //f1.js 'use strice'; //整个js文件都是严格模式下执行的 var n = 1; var foo = function(){...}; //... v ...
- Hadoop2.0安装
http://blog.csdn.net/samhacker/article/details/18802223 http://blog.csdn.net/crazyhacking/article/de ...
- Windows 系统消息范围和前缀,以及消息大全
Windows系统定义的消息类别消息标识符前缀 消息分类ABM 应用桌面工具栏消息BM 按钮控件消息CB 组合框控件消息CBEM 扩展组合框控件消息CDM 通用对话框消息DBT 设备消息DL 拖曳列表 ...
- HelloX项目github协同开发指南
概述 为了提高协同开发效率,HelloX项目已托管到github网站上.根据目前的开发进展,创建了下列几个子项目: HelloX操作系统内核项目:https://github.com/hellox-p ...