POJ1990--POJ 1990 MooFest(树状数组)
Time Limit: 1000MS
Memory Limit: 30000K
Total Submissions: 8141
Accepted: 3674
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.
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.
Output
* Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows.
Sample Input
- 4
- 3 1
- 2 5
- 2 6
- 4 3
Sample Output
- 57
Source
题意:
给定一个序列,序列两点之间的花费为 两点之间的距离*(两点之间价值大者),求所有N(N-1)/2个点对的花费之和
思路:
对某个点来说,其等级可以作为乘数的情况是 它与它左右两边比它的等级小的组成点对
设某个点,坐标为 Xc,它的等级为val,其左边比它小的有n1个,右边比它小的有n2个,左边比比它小的坐标为Xi(1<=i<=n1),右边比它小的坐标为 Xj(1<=j<=n2)
则经过推导,我们可以得到其对答案的贡献为:
(n1-n2) * Xc * val + val ( segma(Xi) – segma(Xj) )
所以我们需要统计两个类型的值:左右两边比它等级小的点的个数,左右两边比它等级小的点的坐标和。
这两个类型的值需要分别求出来,但只要知道了其中一个就可得到另外一个(知道了左边的就可以推导出右边的)
我们利用树状数组来统计这些值,具体做法如下:
将所有点按照等级排序,则某个点现在的标号即为比它小的所有点(包括左和右)的总个数。
我们使用两个树状数组,从左到右一次插入每个节点,一个树状数组统计左边比它等级小的点的个数,另一个统计点的坐标和
第一个树状数组好理解,主要困难在于构造第二个树状数组
只需要每次 update(x[i], x[i])即可
这样的话, x[i] 插入之前,getsum(x[i])就是左边比i点等级小的点的坐标和
代码:


- 1 /*
- 2 * @FileName: D:\代码与算法\2017训练比赛\七月训练四\1013.cpp
- 3 * @Author: Pic
- 4 * @Date: 2017-08-04 21:31:01
- 5 * @Last Modified time: 2017-08-05 20:21:10
- 6 */
- 7
- 8 #include <iostream>
- 9 #include <algorithm>
- 10 #include <cstdio>
- 11 #include <string.h>
- 12 #include <queue>
- 13 using namespace std;
- 14 typedef __int64 ll;
- 15 const int MAXN=20000+30;
- 16 //注意,这道题的vol有相同的情况出现。这样的话就不能先按x坐标排序,统计左右比这个点小的个数,然后按照vol排序统计左右比这个点小的点的vol和
- 17 //而是应该同步地统计这两个量,防止相同的情况
- 18 struct BIT{
- 19 void init()
- 20 {
- 21 memset(Tree_sum,0,sizeof(Tree_sum));
- 22 }
- 23 ll Tree_sum[MAXN];//存储树状数组的数组
- 24 //int maxn=20000+30; //树状数组的下标最大值
- 25 ll lowbit(ll x) //lowbit函数, 找到x与与 *最近的一个末位连续0比他多的数* 的距离
- 26 {
- 27 return x&(-x);
- 28 }
- 29 ll getsum(ll x) //获取0至x区间的和
- 30 {
- 31 ll sum=0;
- 32 for(;x>0;x-=lowbit(x)){
- 33 sum+=Tree_sum[x];
- 34 }
- 35 return sum;
- 36 }
- 37 void update(ll x,ll v) //更新某点的值
- 38 {
- 39 for(;x<=MAXN;x+=lowbit(x)){
- 40 Tree_sum[x]+=v;
- 41 }
- 42 }
- 43 };
- 44 BIT tr,tr2;
- 45 struct node
- 46 {
- 47 ll vol,x,id;
- 48 }a[MAXN];
- 49 ll n1[MAXN];
- 50 bool cmp(node a,node b)
- 51 {
- 52 return a.vol<b.vol;
- 53 }
- 54 bool cmp1(node a,node b)
- 55 {
- 56 return a.x<b.x;
- 57 }
- 58 int main(){
- 59 //freopen("data.in","r",stdin);
- 60 ll n;
- 61 while(~scanf("%I64d",&n)){
- 62 for(int i=0;i<n;i++){
- 63 //scanf("%d%d",&a[i].vol,&a[i].x);
- 64 //cin>>a[i].vol>>a[i].x;
- 65 scanf("%I64d%I64d",&a[i].vol,&a[i].x);
- 66 a[i].id=i;
- 67 }
- 68 tr.init();
- 69 tr2.init();
- 70 sort(a,a+n,cmp1);
- 71 sort(a,a+n,cmp);
- 72 ll res=0,sum=0,sumn=0;
- 73 for(int i=0;i<n;i++){
- 74 sumn=tr.getsum(a[i].x);
- 75 ll suma=tr2.getsum(a[i].x);
- 76 res+=((sum-suma-suma)*a[i].vol);
- 77 res+=((2*sumn-i)*a[i].vol*a[i].x);
- 78 //cout<<res<<endl;
- 79 tr2.update(a[i].x,a[i].x);
- 80 tr.update(a[i].x,1);
- 81 sum+=a[i].x;
- 82 }
- 83 //printf("%lld\n",res);
- 84 //cout<<res<<endl;
- 85 printf("%I64d\n",res);
- 86 }
- 87 return 0;
- 88 }
POJ1990--POJ 1990 MooFest(树状数组)的更多相关文章
- POJ 1990 MooFest --树状数组
题意:牛的听力为v,两头牛i,j之间交流,需要max(v[i],v[j])*dist(i,j)的音量.求所有两两头牛交谈时音量总和∑(max(v[i],v[j])*abs(x[j]-x[i])) ,x ...
- poj 2229 Ultra-QuickSort(树状数组求逆序数)
题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- poj 2155 Matrix (树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16797 Accepted: 6312 Descripti ...
- poj 3067 - Japan(树状数组)
先按第一个数从大到小排序,相等的情况下,第二个数按照从大到小排序..... 预处理后,照着树状数组写就行了... 注意:k的最大值应取1000*1000 代码如下: include <cstdi ...
- poj 2481 - Cows(树状数组)
看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...
- POJ 3468(树状数组的威力)
之前说过这是线段树的裸题,但是当看了http://kenby.iteye.com/blog/962159 这篇题解后我简直震惊了,竟然能如此巧妙地转化为用树状数组来处理,附上部分截图(最好还是进入原网 ...
- POJ 2352 【树状数组】
题意: 给了很多星星的坐标,星星的特征值是不比他自己本身高而且不在它右边的星星数. 给定的输入数据是按照y升序排序的,y相同的情况下按照x排列,x和y都是介于0和32000之间的整数.每个坐标最多有一 ...
- POJ 2182【树状数组】
题意: 每头牛有编号,他们乱序排成一排,每头牛只知道前边比自己序号小的有几位. 思路: 递推,最后一只牛的编号是确定的,然后不断进行区间更新,直到找到某个空位前方恰好有n个空位. 这题跟某道排队的题思 ...
- POJ 2309 BST 树状数组基本操作
Description Consider an infinite full binary search tree (see the figure below), the numbers in the ...
随机推荐
- mssql 数据库“查询处理器用尽了内部资源,无法生成查询计划。”问题的处理
在项目中动态拼接sql语句,使用union all连接结果集,每个查询语句都使用了in(几百个数值).语句如: ,,,..............................) UNION ALL ...
- 【原创】大叔经验分享(71)docker容器中使用jvm工具
java应用中经常需要用到jvm工具来进行一些操作,如果java应用部署在docker容器中,如何使用jvm工具? 首先要看使用的docker镜像, 比如常用的openjdk镜像分为jdk和jre,只 ...
- CentOS/RHEL 安装EPEL第三方软件源
EPEL源简介 EPEL(Extra Packages for Enterprise Linux) 是由 FedORA 社区打造,为 RHEL 及衍生发行版如 CentOS等提供高质量软件包的项目.装 ...
- 关于登陆界面,页面没有刷新完毕,点击登陆跳转到一个接口的bug
现象 输入完密码点击登陆就跳转到了如下的页面 分析原因: 第一:查看html页面 页面中的html 登陆用的是form表单 表单中还写了属性 action 即允许跳到某一个接口,这里是没 ...
- webconfig中的&符号问题解决
第一种解决方案 解决方法是将“&”,用“*”代替,取的时候再替换 第二种解决方案 用“&”替换“&”
- 使用hbuilder打包时,调用地图和相机
<template> <div class="comCon"> <!-- 你是头部区域的内容 --> <headback class=&q ...
- centos8下jdk13和tomcat9的安装
首先下载JDK13和tomcat9在对应的官网上: 通过xftp传到linux服务器上的对应的目录,如/usr/local apache-tomcat-9.0.27.tar.gz ,jdk-13.0 ...
- Win10系统的开机启动项如何去关闭?
我们在使用电脑时会安装许多的应用程序,而有些应用程序会默认在电脑开机时自行启动,不仅影响开机速度,还会在开机后占用电脑内存资源. 那么在Win10系统中,我们该如何查看有哪些开机启动项呢?我们又该怎么 ...
- inputrc命令
问题:搭建ubuntu系统后,输入命令的第一个字符+上页按键,发现不能找到历史命令,找了好久才发现是因为/etc/inputrc没有对键盘映射的上页键和下页键进行设置. 解决方法: 修改文件/etc/ ...
- spring-02
spring-02 1.谈谈你对 Spring 的理解 Spring 是一个开源框架,为简化企业级应用开发而生.Spring 可以是使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.S ...