MooFest_二维树状数组
Description
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
* 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
Sample Input
- 4
- 3 1
- 2 5
- 2 6
- 4 3
Sample Output
- 57
【题意】有n头牛,排列成一条直线,给出每头牛在直线上的坐标d。每头牛有一个v,如果牛i和牛j想要沟通的话,它们必须用max(v[i],v[j]),消耗的能量为:max(v[i],v[j]) * 它们之间的距离.
问要使所有的牛之间都能沟通(两两之间),总共需要消耗多少能量。
【思路】现将v从小到大排列,使得每次取到的是当前最大的v。
c[1]记录当前牛的数量c[2]记录当前所有牛的d之和。(二维树状数组)
- #include<iostream>
- #include<stdio.h>
- #include<string.h>
- #include<algorithm>
- using namespace std;
- const int N=;
- struct node
- {
- int d,v;
- bool operator <(const node &a)const
- //从小到大排序,使得当前获得的v一定是出现过最大的。
- {
- return v<a.v;
- }
- }moo[N+];
- int c[][N+];
- int lowbit(int x)
- {
- return x&(-x);
- }
- void update(int i,int d,int v)
- {
- while(d<=N)
- {
- c[i][d]+=v;
- d+=lowbit(d);
- }
- }
- int get_sum(int i,int d)
- {
- int res=;
- while(d)
- {
- res+=c[i][d];
- d-=lowbit(d);
- }
- return res;
- }
- int main()
- {
- int n;
- while(~scanf("%d",&n))
- {
- memset(c,,sizeof(c));
- for(int i=;i<=n;i++)
- scanf("%d%d",&moo[i].v,&moo[i].d);
- sort(moo+,moo++n);
- int sum=;//记录所有坐标之和
- long long int ans=;
- for(int i=;i<=n;i++)
- {
- int d=moo[i].d;
- sum+=d;
- update(,d,);//c[1]记录牛数量
- update(,d,d);//c[2]记录牛坐标之和
- int n1=get_sum(,d);//在i牛及他前面有多少头
- int n2=get_sum(,d);//在i牛及他前面的牛坐标和为多少
- int tmp1=n1*d-n2;//i左边的坐标差
- int tmp2=sum-n2-d*(i-n1);//i右边的坐标差
- ans+=(long long int)(tmp1+tmp2)*moo[i].v;
- //不用longlong会溢出
- }
- printf("%lld\n",ans);
- }
- return ;
- }
MooFest_二维树状数组的更多相关文章
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
- POJMatrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22058 Accepted: 8219 Descripti ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
随机推荐
- 聚类算法:K-means 算法(k均值算法)
k-means算法: 第一步:选$K$个初始聚类中心,$z_1(1),z_2(1),\cdots,z_k(1)$,其中括号内的序号为寻找聚类中心的迭代运算的次序号. 聚类中心的向量值可任意设 ...
- 登陆验证前对用户名和密码加密之后传输数据---base64加密
以下这种方法是加密传输的简单实现 1,base64.js /** * * Base64 encode / decode * * */ function Base64() { // private pr ...
- mac 无法ssh localhost
mac 无法ssh localhost,错误提示:bash: /usr/local/bin/ssh_session: Permission denied在网上找了很久也没有找到解决方案,最后根据提示自 ...
- iOS8 针对开发者所拥有的新特性汇总如下
iOS8 针对开发者所拥有的新特性汇总如下 1.支持第三方键盘 2.自带网页翻译功能(即在线翻译) 3.指纹识别功能开放:第三方软件可以调用 4.Safari浏览器可直接添加新的插件. 5.可以把一个 ...
- Servlet作业--实现注册和登录
1.注册页面 zhuce.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...
- elasticsearch插件之一:bigdesk
bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu.内存使用情况,索引数据.搜索情况,http连接数等. 可用项目git地址:https:// ...
- Jmeter简单测试elasticsearch服务器
入门知识: Jmeter是一个非常好用的压力测试工具. Jmeter用来做轻量级的压力测试,非常合适,只需要十几分钟,就能把压力测试需要的脚本写好. 顾名思义:压力测试,就是 被测试的系统,在一定 ...
- 创建单例的DbContext
/// <summary> /// 说明: /// 创建日期:2016/9/30 14:49:48 /// 创建人:曹永承 /// </summary> public clas ...
- uiwebview 清缓存。,mark
//清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCook ...
- [开发笔记]-jQuery获取checkbox选中项等操作及注意事项
今天在做一个项目功能时需要显示checkbox选项来让用户进行选择,由于前端不是很熟练,所以做了一个简单的Demo,其中遇到一些小问题,特记录下来,希望能帮到遇到类似问题的同学们. 1. 获取chec ...