MooFest
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5697   Accepted: 2481

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

 
读题真是个大问题啊!!!一开始读了好久也没读懂,还是看的别人题解才明白了题意。
好吧,题意好歹明白了,可是问题又来了,怎么办?
归根到底就是一个动态改值并求和的问题,树状数组(线段树当然也可)。
我发现我的树状数组真不会用,就是用的不熟练,以前做的都直接套,我都没弄懂啥意思,一次锻炼吧。
往往成功就差那么一步啊,确是咫尺天涯。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 20005 struct P{
ll v, p;
bool operator < (const P& t) const {
return t.v > v;
}
}cow[MAXN];
ll c_ount[MAXN] = {};
ll total[MAXN] = {};
ll sum_tot[MAXN] = {};
int n; ll lowbit(ll x)
{
return x & (-x);
} void add(int x, int d, ll c[])
{
while(x < MAXN) {
c[x] += d;
x += lowbit(x);
}
} ll Sum(ll x, ll c[])
{
ll ret = ;
while(x > )
{
ret += c[x];
x -= lowbit(x);
}
return ret;
} int main()
{
scanf("%d", &n);
repu(i, , n + ) scanf("%lld%lld", &cow[i].v, &cow[i].p);
sort(cow + , cow + n + );
repu(i, , n + ) sum_tot[i] = sum_tot[i - ] + cow[i].p;
ll sum = , num_cow = , sum_total = ;
add(cow[].p, , c_ount);
add(cow[].p, cow[].p, total);
repu(i, , n + ) {
num_cow = Sum(cow[i].p, c_ount);
sum_total = Sum(cow[i].p, total);
sum += cow[i].v * (num_cow * cow[i].p - sum_total
+ (sum_tot[i - ] - sum_total - (i - - num_cow) * cow[i].p));
add(cow[i].p, , c_ount);
add(cow[i].p, cow[i].p, total);
}
printf("%lld\n", sum);
return ;
}

I-MooFest(POJ 1990)的更多相关文章

  1. MooFest POJ - 1990 (树状数组)

    Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gather ...

  2. ●POJ 1990 MooFest

    题链: http://poj.org/problem?id=1990 题解: 树状数组 把牛们按x坐标从小到大排序,依次考虑每头牛对左边和对右边的贡献. 对左边的贡献:从左向右枚举牛,计算以当前牛的声 ...

  3. POJ 1990 MooFest(zkw线段树)

    [题目链接] http://poj.org/problem?id=1990 [题目大意] 给出每头奶牛的位置和至少要多少分贝的音量才能听到谈话 现在求奶牛两两交流成功需要的分贝*距离的总和. [题解] ...

  4. POJ 1990 MooFest(树状数组)

                                                                        MooFest Time Limit: 1000MS   Mem ...

  5. 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 ...

  6. poj 1990 MooFest

    题目大意: FJ有n头牛,排列成一条直线(不会在同一个点),给出每头牛在直线上的坐标x.另外,每头牛还有一个自己的声调v,如果两头牛(i和j)之间想要沟通的话,它们必须用同个音调max(v[i],v[ ...

  7. POJ 1990:MooFest(树状数组)

    题目大意:有n头牛,第i头牛声调为v[i],坐标为x[i],任意两值牛i,j沟通所需的花费为abs(x[i]-x[j])*max(v[i],v[j]),求所有牛两两沟通的花费. 分析: 我们将奶牛按声 ...

  8. POJ 1990 MooFest【 树状数组 】

    题意:给出n头牛,每头牛有一个听力v,坐标x,两头牛之间的能量为max(v1,v2)*dist(v1,v2),求总的能量值 先将每头牛按照v排序,排完顺序之后,会发现有坐标比当前的x小的,会有坐标比当 ...

  9. poj 1990

    题目链接 借鉴cxlove大神的思路 题意:听力v,位置x,2个牛交流声音为max(v1,v2)*(x1-x2),求总的 10000^2 tle 用的树状数组做的,排序,2个,小于vi的牛的总数和距离 ...

随机推荐

  1. servlet&jsp高级:第五部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. [Effective Java]第十章 并发

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  3. Codeforces Round #288 (Div. 2) C. Anya and Ghosts 模拟

    C. Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. BeanUtils框架的简单运用

    Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单.易用的API操作Bean的属性——BeanUtils Beanutils工具包的常用类: •BeanUt ...

  5. 08 高效的SQL

    编写高效 SQL 需要以下知识 有关所查询内容的物理组织的知识 数据库能做什么的知识, 例如: 如果你不知道跳跃扫描索引及其用途, 那么你可能会看着模式说”索引丢了” SQL 所有错综复杂的知识 对目 ...

  6. web设计经验<一> 提升移动设备响应式设计的8个建议

    今天看到一些关于web设计的一些建议和设计经验,拿出来分享分享. 第一篇: 提升移动设备响应式设计的8个建议 一.直观性和易用性 在使用移动设备时,对于杂乱.复杂或者不直观的设计造成的混乱不佳的用户体 ...

  7. iOS开发之 Xcode 一个工程 Project 添加多个 target

    http://www.360doc.com/content/14/1203/11/19119980_430056974.shtml# 根据项目需求,同一个工程有多个版本,每个版本只有细微的不同.所以, ...

  8. C++——使用类

    一.运算符重载 1.运算符重载 C++允许将运算符重载扩展到用户定义的类型. 要重载运算符,需使用被称为运算符函数的特殊函数形式.运算符函数的格式如下: operatorop(argument lis ...

  9. mysql下sql语句 update 字段=字段+字符串

    mysql下sql语句 update 字段=字段+字符串   mysql下sql语句令某字段值等于原值加上一个字符串 update 表明 SET 字段= 'feifei' || 字段; (postgr ...

  10. VB6 GDI+ 入门教程[4] 文字绘制

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[4] 文字绘制 2009 年 6 月 18 日 7条评论 ...