1、http://poj.org/problem?id=1990

2、题目大意:

题意:FJ有n头牛,排列成一条直线(不会在同一个点),给出每头牛在直线上的坐标x。另外,每头牛还有一个自己的声调w,如果两头牛(i和j)之间想要沟通的话,它们必须用同个音调max(w[i],w[j]),沟通起来消耗的能量为:max(w[i],w[j]) * 它们之间的距离。问要使所有的牛之间都能沟通(两两之间),总共需要消耗多少能量。

3.题目分析:

思路:树状数组。很好的一道题。把牛按x升序排列,然后将题目中的x考虑成从左到右求比自己当前声音小的牛的个数以及距离求出总共消耗的能量来,再逆序求比自己大的牛的个数及距离,这样就能保证是两两都能听到也就是保证了他们之间的正常沟通问题

1:把沟通分成向左和向右,向左就是:w[右] > w[左],它们之间取右边牛的声调。

2:先求向左的总能量:运用2个树状数组,都以牛的声调做下标,cnum[]用于求某个声调范围内牛的数量,dnum[]用于求某个声调范围内牛的总距离(与起点0的总距离),这对于当前的牛(x,v)来说,用它的音调向左沟通消耗的总能量则为:w* 左边声调比它小的牛和它的距离差之和(sum(w-1, cum)* sum(v-1,dnum))。

3:相同的道理求出向右的总能量。

4、题目:

MooFest
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4791   Accepted: 2009

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

USACO 2004 U S Open

5、AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 20005
__int64 cnum[N];//cnum[i]以i声调为下标比当前牛小的牛的数量
__int64 dnum[N];//dnum[i]表示以声调为下标,距离比当前小的牛的距离和(到原点的距离)
struct node
{int w;int x;
}a[N];
int cmp(node a,node b)
{return a.x<b.x;
}
int lowbit(int i)
{return i&(-i);
}
void update(int x,int v,__int64 *c)
{for(int i=x;i<=N;i+=lowbit(i)){c[i]+=v;}
}
__int64 sum(int x,__int64 *c)
{__int64 sum=0;for(int i=x;i>0;i-=lowbit(i)){sum+=c[i];}return sum;
}
int main()
{int n;while(scanf("%d",&n)!=EOF){__int64 ans=0;for(int i=1;i<=n;i++){scanf("%d%d",&a[i].w,&a[i].x);}sort(a+1,a+n+1,cmp);__int64 cowNum=0;__int64 cowDist=0;memset(cnum,0,sizeof(cnum));memset(dnum,0,sizeof(dnum));for(int i=1;i<=n;i++){cowNum=sum(a[i].w-1,cnum);cowDist=sum(a[i].w-1,dnum);ans+=(a[i].x*cowNum-cowDist)*a[i].w;update(a[i].w,1,cnum);update(a[i].w,a[i].x,dnum);}memset(cnum,0,sizeof(cnum));memset(dnum,0,sizeof(dnum));for(int i=n;i>=1;i--){cowNum=sum(a[i].w,cnum);cowDist=sum(a[i].w,dnum);ans+=(cowDist-a[i].x*cowNum)*a[i].w;update(a[i].w,1,cnum);update(a[i].w,a[i].x,dnum);}printf("%I64d\n",ans);}return 0;
}

附自己开始用for循环做错的代码,其实这么大的数据for一般都是不正确的

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 20005
struct node
{int v;int x;
}a[N];
int main()
{int n;while(scanf("%d",&n)!=EOF){for(int i=1;i<=n;i++){scanf("%d%d",&a[i].v,&a[i].x);}int sum=0;for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){int m=max(a[i].v,a[j].v);sum+=abs(a[i].x-a[j].x)*m;}}printf("%d\n",sum);}return 0;
}

poi 1990 MooFest(树状数组题目,转换成两个树状数组来做)较难的题目****相关推荐

  1. php转化xml数组_PHP实现数组array转换成xml的方法

    本文实例讲述了PHP实现数组array转换成xml的方法.分享给大家供大家参考,具体如下: $elementLevel = 0 ; function array_Xml($array, $keys = ...

  2. linux 串口 字符 间隔,嵌入式linux编程过成中模块从串口读数需要特定的字符段并且需要每两位字符数组元素转换成一个16进制数(提取特定字符串+字符串转16进制)...

    嵌入式linux编程过成中用到zigbee模块 zigbee从串口读数需要特定的字符段并且需要每两位字符数组元素转换成一个16进制数 (提取特定字符串+字符串转16进制) #include #incl ...

  3. 嵌入式linux编程过成中模块从串口读数需要特定的字符段并且需要每两位字符数组元素转换成一个16进制数(提取特定字符串+字符串转16进制)

    嵌入式linux编程过成中用到zigbee模块 zigbee从串口读数需要特定的字符段并且需要每两位字符数组元素转换成一个16进制数 (提取特定字符串+字符串转16进制) #include<st ...

  4. 腾讯视频QLV格式转换成mp4格式,只需这样做!

    到百度首页百度首页登录 腾讯视频QLV格式转换成mp4格式,只需这样做! 淡定人生 百家号17-10-1603:44 腾讯视频单独弄了一个qlv格式,一定程度上也造成了我们使用时的不便利.这几天有个朋 ...

  5. php把数组转换成对象,php怎么将数组转换成对象

    echoecho() 函数输出一个或多个字符串.注释:echo() 函数实际不是一个函数,所以您不必对它使用括号.然而,如果您想要传多于一个参数给 echo(),使用括号将会生成解析错误.print ...

  6. html发送十六进制字符数组,十六进制数组怎么转换成字符串数组?

    本身我有一篇文章专门谈这个事情,结果被说内容不好. 题外话,回答主题: 其实对计算机系统来说,没有什么十六进制转字符,字符在计算机中存储本身就是按一个一个编号来的,在计算机中也是二进制存储处理,注意, ...

  7. asp.net(c#)字符串转换成字符数组 字符串转换成int 数组

    问题: 一个字符串是string   a="a,b,c,d" ,另一个是string   b="1,2,3,4" ,第一个转换成string[]类型的,第二个转 ...

  8. 普通数组如何转换成json数据格式

    我们都知道json有很多种格式,而开发过程中经常遇到格式转换的问题,特别是接口调用的时候,如何将普通数组转成我们需要的json格式呢,下面我提供了一种方法,也是一种思路,希望对大家有所帮助!! (大前 ...

  9. POI + PDFbox将PPT有图表页转换成图片

    使用poi将PPT中有图表页转换成图片存在一些问题,转成图片的时候图表没有了,数据丢失. 所以用一个中转的方法,现将PPT的图表页转换成PDF,再将PDF转换成图片,虽然麻烦点,但是没有出现任何问题. ...

最新文章

  1. python【蓝桥杯vip练习题库】BASIC-3字母图形
  2. angularjs组件间通讯_详解Angular2组件之间如何通信
  3. c++中的引用和python中的引用_对比 C++ 和 Python,谈谈指针与引用
  4. 玩转Google开源C++单元测试框架Google Test系列(gtest)之七 - 深入解析gtest
  5. d3.js 简介和安装
  6. 【转】WebSocket详解(一):初步认识WebSocket技术
  7. 滑动窗口算法_从一道题讲解滑动窗口算法该如何实现
  8. 当前没有可用的服务器_调研Redis高可用两种方案
  9. 10. zf workflow
  10. 组态王的日历时间控件脚本
  11. 最新BT面板静态文件镜像库v7.1.1
  12. 本科毕设完整流程和注意事项
  13. Everything使用攻略和技巧
  14. 5G时代的射频器件革命
  15. 2207.16吃货联盟设计大纲和全部代码
  16. ftp上传工具 免安装,ftp上传工具 免安装绿色破解版好用推荐
  17. miui修改Android,无法修改小米MIUI设备中的系统设置
  18. 大数据与JS实现2014巴西世界杯冠军预测图
  19. 初中使用计算机,初中生使用计算器的利和弊!!!急!!!!!!!!!!!!!!!!!!!!
  20. adblock plus规则下载(包含国内99%网站广告屏蔽规则)

热门文章

  1. PerformancePoint Server 词汇表[转自MSDN]
  2. NVM(node version manager)node版本管理工具
  3. 尝试寻找一些合作伙伴,产品相关培训咨询服务介绍(2B/支持在线)
  4. 硬改无人直播系统-使用小技巧
  5. 京东软件测试岗:惨不忍睹的三面,幸好做足了准备,月薪17k,已拿offer
  6. webpack中dev模式,配置既可以使用localhost,又可以使用ip地址访问
  7. 通信算法之七十六:无人机反制-电磁攻击和火力反制
  8. php 冒泡查找 降序 随机数 封装,又一个PHP实现的冒泡排序算法分享
  9. 时空角怎么理解_天道文化——如何让员工人人以客户为导向?
  10. ed2k linux命令行,Linux_Linux管理应用技巧 amulecmd的使用方法,amulecmd是aMule的命令行操作与管 - phpStudy...