hdu 3015 Disharmony Trees
来源:互联网 发布:kali centos 编辑:程序博客网 时间:2024/05/03 04:23
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015
题目大意: 在一片树林有n棵树(你<=100,000)中,每棵树有两个值一个是x,h.按照x从小到大排序,给每棵树一个编号D(从1开始),如5棵树,x为3,3,1,3,4,其D值为2,2,1,2,3.同样的再按y从小到大排序,给每棵树另一个编号H(从1开始),编号方法和D一样.两棵树之间有一个不和谐值=abs(Di-Dj)*min(Hi,Hj).要求的是所有的不和谐值之和.
思路:最暴力的解法(n*n),n最大是10万,可以看一下暴力的解法,如下:
假设有4棵树,按照H升序排序
当不需要abs操作时:
ret = (D2+D3+D4-3D1)*H1 + (D3+D4-2D2)*H2 + (D4-D3)*H3
我们可以用线段树或者树状数组快速求出Di+...+Dj的和.
但是悲剧的是题目需要abs操作,所以当Dx<D(当前),就需要-Dx+D(当前),所以我们只要求出比当前D小的和(lowSum)和个数(lowCnt)以及比当前D大的和(upSum)和个数(upCnt).
这样(逆着扫)ret += (upSum[i] - lowSum[i] + (lowCnt[i] - upCnt[i]) * D[i]) * H[i] (1<=i<=n)
lowSum,upSum,lowCnt,upCnt可以用线段树或树状数组写(单点的,求和的,还是用树状数组好,快又省空间,最主要是好写!).
代码:
#include <stdlib.h>#include <string.h>#include <stdio.h>#include <ctype.h>#include <math.h>#include <time.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <string>#include <iostream>#include <algorithm>using namespace std;#define ull unsigned __int64#define ll __int64//#define ull unsigned long long //#define ll long long#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define middle (l+r)>>1#define esp (1e-7)const int INF=0x3F3F3F3F;//const double pi=acos(-1.0);const int N=100010;struct node{ ll x,y,D,H;}a[N];int n,m,k;ll sum[N],cnt[N];bool xcmp(node& p,node& q){return p.x<q.x;}bool ycmp(node& p,node& q){return p.y<q.y;}int lowbit(int x){return x&(-x);}void Update(int x,ll c1,ll c2){ while(x<=n) sum[x]+=c1,cnt[x]+=c2,x+=lowbit(x);}void Query(int x,ll& r1,ll& r2){ r1=r2=0; while(x>=1) r1+=sum[x],r2+=cnt[x],x-=lowbit(x);}int main(){ //freopen("test.in","r",stdin); int i,j,k; ll ret,pre,lowCnt,upCnt,lowSum,upSum; //int T,cas;scanf("%d",&T);for(cas=1;cas<=T;cas++) while(~scanf("%d",&n)){ for(i=1;i<=n;i++) scanf("%I64d%I64d",&a[i].x,&a[i].y); sort(a+1,a+n+1,xcmp); for(i=j=1,pre=a[i].x;i<=n;i++){ if(a[i].x!=pre) pre=a[i].x,j=i; a[i].D=j; } sort(a+1,a+n+1,ycmp); for(i=j=1,pre=a[i].y;i<=n;i++){ if(a[i].y!=pre) pre=a[i].y,j=i; a[i].H=j; } memset(sum,0,sizeof(sum)); memset(cnt,0,sizeof(cnt)); for(i=n,ret=0;i>=1;i--){ Query(a[i].D,lowSum,lowCnt); Query(n,upSum,upCnt); upSum-=lowSum,upCnt-=lowCnt; //printf("**%lld %lld %lld %lld**\n",upSum,lowSum,upCnt,lowCnt); ret+=(upSum-lowSum+(lowCnt-upCnt)*a[i].D)*a[i].H; Update(a[i].D,a[i].D,1); } printf("%I64d\n",ret); } return 0;}
- hdu 3015 Disharmony Trees
- hdu 3015 Disharmony Trees
- HDU 3015 Disharmony Trees
- HDU - 3015 Disharmony Trees
- HDU 3015 Disharmony Trees 树状数组
- HDU 3015 Disharmony Trees (树状数组)
- HDU 3015 Disharmony Trees (线段树)
- hdu 3015 Disharmony Trees(树状数组)
- hdu 3015 Disharmony Trees(树状数组)
- HDU 3015 Disharmony Trees(树状数组)
- 树状数组+公式推导 hdu 3015 Disharmony Trees
- hdu 3015 Disharmony Trees(树状数组+离散)
- Disharmony Trees
- Disharmony Trees
- hdoj 3015 Disharmony Trees(树状数组)
- HDU-3015 Disharmony Trees (离散+树状数组)(2009 Multi-University Training Contest 12)
- HDU 5876 Disharmony Trees(离散化 树状数组)
- HDU3015 Disharmony Trees
- java类生命周期
- 第十二周任务四之类的组合与继承
- Linux环境进程间通信(二下) from IBM developerWorks 作者:郑彦兴 (mlinux@163.com)国防科大
- 字母大小写转换
- C++字符串完全指引之一 —— Win32 字符编码
- hdu 3015 Disharmony Trees
- Linux环境进程间通信(三) from IBM developerWorks 作者:郑彦兴 (mlinux@163.com)国防科大
- 两个数相乘
- 计算圆的面积的实验步骤
- C++字符串完全指引之二 —— 字符串封装类
- extjs技巧
- 怎样在Xcode4中用Git对已有项目进行版本管理
- 工程师才是最酷的~~~详解如何突破MAC地址绑定
- 堆栈,堆栈,堆和栈的区别