POJ 1990 树状数组
来源:互联网 发布:sql for in 循环语句 编辑:程序博客网 时间:2024/05/17 03:45
弱第一次知道了树状数组与线段树的差别。。。。实在太弱了。
言归正传:题目:
MooFest
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 7683 Accepted: 3455
Description
Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of course, mooing. When the cows all stand in line for a particular event, they moo so loudly that the roar is practically deafening. After participating in this event year after year, some of the cows have in fact lost a bit of their hearing.
Each cow i has an associated "hearing" threshold v(i) (in the range 1..20,000). If a cow moos to cow i, she must use a volume of at least v(i) times the distance between the two cows in order to be heard by cow i. If two cows i and j wish to converse, they must speak at a volume level equal to the distance between them times max(v(i),v(j)).
Suppose each of the N cows is standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), and every pair of cows is carrying on a conversation using the smallest possible volume.
Compute the sum of all the volumes produced by all N(N-1)/2 pairs of mooing cows.
Each cow i has an associated "hearing" threshold v(i) (in the range 1..20,000). If a cow moos to cow i, she must use a volume of at least v(i) times the distance between the two cows in order to be heard by cow i. If two cows i and j wish to converse, they must speak at a volume level equal to the distance between them times max(v(i),v(j)).
Suppose each of the N cows is standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), and every pair of cows is carrying on a conversation using the smallest possible volume.
Compute the sum of all the volumes produced by all N(N-1)/2 pairs of mooing cows.
Input
* Line 1: A single integer, N
* Lines 2..N+1: Two integers: the volume threshold and x coordinate for a cow. Line 2 represents the first cow; line 3 represents the second cow; and so on. No two cows will stand at the same location.
* Lines 2..N+1: Two integers: the volume threshold and x coordinate for a cow. Line 2 represents the first cow; line 3 represents the second cow; and so on. No two cows will stand at the same location.
Output
* Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows.
Sample Input
43 12 52 64 3
Sample Output
57
首先,关于树状数组,见这篇博客:http://blog.csdn.net/int64ago/article/details/7429868
讲的很详细。。。本弱就不自己解释了。
此题需反复多次进行查找,适合使用树状数组
先对所有牛按照位置进行排序,然后按照牛声音的高低建两棵树,一棵存储牛的只数,另一棵存放距离。按照位置进行排序后,保证每次处理都能分清楚牛的前后关系,以方便计算距离。
代码:
//By Sean Chen#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#define MAX 20005using namespace std;int lowbit(int x) //树状数组的重要操作{ return x&(-x);}struct data{ int x,w;}cow[MAX];int bitNum[MAX],bitDis[MAX],n;bool cmp(data a,data b){ return a.x<b.x;}void add(int i,int *ar,int w) //添加结点{ while(i<MAX) { ar[i]+=w; i+=lowbit(i); } return;}long long sum(int i,int *ar){ long long ans=0; while(i>0) { ans+=ar[i]; i-=lowbit(i); } return ans;}int main(){ int i; long long totcow,totdis; scanf("%d",&n); for(i=0;i<n;i++) scanf("%d%d",&cow[i].w,&cow[i].x); sort(cow,cow+n,cmp); long long ans=0; memset(bitNum,0,sizeof(bitNum)); memset(bitDis,0,sizeof(bitDis)); for(i=0;i<n;i++) //向左计算 { totcow=sum(cow[i].w-1,bitNum); totdis=sum(cow[i].w-1,bitDis); ans+=(totcow*cow[i].x-totdis)*cow[i].w; add(cow[i].w,bitNum,1); add(cow[i].w,bitDis,cow[i].x); } memset(bitNum,0,sizeof(bitNum)); memset(bitDis,0,sizeof(bitDis)); for(i=n-1;i>=0;i--) //向右计算 { totcow=sum(cow[i].w,bitNum); totdis=sum(cow[i].w,bitDis); ans+=(totdis-totcow*cow[i].x)*cow[i].w; add(cow[i].w,bitNum,1); add(cow[i].w,bitDis,cow[i].x); } printf("%lld\n",ans); return 0;}
0 0
- poj 1990【树状数组】
- poj 1990 树状数组
- POJ 1990 树状数组
- POJ 1990 树状数组
- POJ 1990 MooFest(树状数组)
- POJ 1990 MooFest 树状数组
- POJ 1990 MooFest(树状数组)
- POJ 1990 MooFest (树状数组)
- poj 1990 MooFest 树状数组
- poj 1990 MooFest (树状数组)
- MooFest - POJ 1990 树状数组
- POJ 1990 MooFest 树状数组
- 【树状数组】 POJ 1990 MooFest
- POJ 1990 MooFest 树状数组
- POJ 1990 MooFest 树状数组
- POJ 1990 MooFest --树状数组
- POJ-1990(树状数组)
- POJ 1990 MooFest 【树状数组】
- codeforces 798B
- 升级 SpringMVC 4.2.5 com/fasterxml/jackson/core/JsonProcessingException
- CentOS下go 语言开发环境源码安装
- openssl-1.0.2编译和安装
- ZOJ 1489
- POJ 1990 树状数组
- 数字三角形
- SpringBoot非官方教程 | 终章:文章汇总
- EL表达式 & JSTL(详解)
- springmvc 用fasterxml.jackson返回son数据
- 输入身份证号码计算两者的年龄差
- 幸幸苦苦写了半天
- Html史上最大的坑--换行产生默认边距
- Microsoft Office Document Imaging 2007提取图片中的文字