BZOJ[3378][Usaco2004 Open]MooFest 狂欢节 树状数组
来源:互联网 发布:一般淘宝哪些假货多 编辑:程序博客网 时间:2024/05/18 02:16
题目链接http://www.lydsy.com/JudgeOnline/problem.php?id=3378
Description
每一年,约翰的N(1≤N≤20000)只奶牛参加奶牛狂欢节.这是一个全世界奶牛都参加的大联欢.狂欢节包括很多有趣的活动,比如干草堆叠大赛、跳牛栏大赛,奶牛之间有时还相互扎屁股取乐.当然,她们会排成一列嚎叫,来欢庆她们的节日.奶牛们的叫声实在刺耳,以致于每只奶牛的听力都受到不同程序的损伤.现在告诉你奶牛i的听力为vi(l≤Vi≤20000),这表示如果奶牛j想说点什么让她听到,必须用高于vi×dis(i,j)的音量.而且,如果奶牛i和j想相互交谈,她们的音量必须不小于max(Vi,Vj)×dis(i,j).其中dis(i,J)表示她们间的距离.现在N只奶牛都站在一条直线上了,每只奶牛还有一个坐标xi(l≤xi≤20000).如果每对奶牛都在交谈,并且使用最小音量,那所有n(n-1)/2对奶牛间谈话的音量之和为多少?
Input
第1行输入N,之后N行输入Vi和xi.
Output
输出音量之和.
Sample Input
4
3 1
2 5
2 6
4 3
Sample Output
57
可以考虑将每个奶牛的v排序,则插入第i头奶牛时一定是当前最大的一个,问题在于如何与之前的数求贡献?
可以考虑开两个树状数组,
代码如下:
#include<algorithm>#include<ctype.h>#include<cstdio>#define N 20050#define MAXN 20000using namespace std;typedef long long ll;inline int read(){ int x=0,f=1;char c; do{c=getchar();if(c=='-') f=-1;} while(!isdigit(c)); do x=(x<<3)+(x<<1)+c-'0',c=getchar(); while(isdigit(c)); return x*f;}struct Cow{ int v,x;}a[N];bool operator < (const Cow a,const Cow b){return a.v<b.v;}int n,num[N];ll s[N],s1[N];ll ans=0ll;inline ll sum(int x,ll a[]){ ll tmp=0; for(;x;x=x-(x&-x)) tmp=tmp+a[x]; return tmp;}inline void add(int x,int v,ll a[]){ for(;x<=MAXN;x=x+(x&-x)) a[x]=a[x]+v;}inline ll qsum(int l,int r,ll a[]){ return sum(r,a)-sum(l-1,a);}int main(){ n=read(); for(int i=1;i<=n;i++) a[i].v=read(),a[i].x=read(); sort(a+1,a+n+1); for(int i=1;i<=n;i++){ ans=ans+a[i].v*(sum(a[i].x-1,s)*a[i].x-sum(a[i].x-1,s1)+qsum(a[i].x+1,MAXN,s1)-qsum(a[i].x+1,MAXN,s)*a[i].x);//我承认这一行缩得很令人恶心 add(a[i].x,1,s); add(a[i].x,a[i].x,s1); } printf("%lld",ans);return 0;}
阅读全文
0 0
- BZOJ[3378][Usaco2004 Open]MooFest 狂欢节 树状数组
- 3378: [Usaco2004 Open]MooFest 狂欢节
- 【bzoj 3378】MooFest 狂欢节(树状数组)
- bzoj 3356: [Usaco2004 Jan]禁闭围栏 离散化+树状数组
- POJ1990 MooFest,树状数组
- poj1990 moofest 树状数组
- POJ 1990 MooFest(树状数组)
- POJ 1990 MooFest 树状数组
- POJ 1990 MooFest(树状数组)
- POJ 1990 MooFest (树状数组)
- poj 1990 MooFest 树状数组
- poj 1990 MooFest (树状数组)
- POJ1990 MooFest(树状数组)
- MooFest - POJ 1990 树状数组
- POJ 1990 MooFest 树状数组
- 【树状数组】 POJ 1990 MooFest
- POJ 1990 MooFest 树状数组
- POJ 1990 MooFest 树状数组
- 用户信息的添加与删除
- 算法分析与设计丨第六周丨LeetCode(10)——Best Time to Buy and Sell Stock(Easy)
- ArcSDE与SQL sevrver的连接、ArcSDE导入数据、ArcGIS Server发布地图服务
- sudo chown -R $(whoami) /usr/local/lib/pkgconfig
- JavaScript定时函数
- BZOJ[3378][Usaco2004 Open]MooFest 狂欢节 树状数组
- HDU1520-Anniversary party(树形dp)
- 学习插入排序
- 体验店员工绩效考核表
- 线性表的插入
- hdu 1142 A Walk Through the Forest 最短路+记忆化搜索 解题报告
- viewpager自动轮播
- linux中的快捷键
- 一种串口高效收发思路及方案