POJ 1990 MooFest
来源:互联网 发布:淘宝不能好评返现了 编辑:程序博客网 时间:2024/05/16 14:40
http://poj.org/problem?id=1990
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
题目大意:有n头牛,每头牛有2个值,一个是它的坐标x,另一个是它所能听到的音量的最低要求v,求让所有牛都听到产生的最小音量。音量=(xi-xj)*max(vi,vj).
大概思路:题型:树状数组。
1.如何将题目转换为树状数组可求的模型?这里对每头牛,我们需要求出v小于等于它的牛的数量,然后用n*v。
2.如何求v小于等于它的牛的数量呢?这里一头牛遍历一遍为20000,有20000头牛就需要400000000,所以很明显是不行的。所以需要用到树状数组。
3.为了方便起见,我们先按v排序。我有点不知道怎么说,还是直接解释程序吧。
前面音量和坐标都比它小的:[(x-x1)+(x-x2)+……+(x-xn)]* v[x]= (n *x-Xn) *v[x]
音量比它小但是坐标比它大的:[(x1-x)+(x2-x)+……+(xm-x)]*v[x]=(Xm-m *x) *v[x]
这两个数加起来就是这头牛会产生的音量。
然后具体一点:n怎么求?t1[N]数组用来存储每头牛的n,m怎么求,t2[x]=i-t1[x],因为当前的牛数减去前面的牛就是后面的牛。Xn怎么求?就这么求呗……希望几百年后的自己还能看懂这个程序……
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#define N 20005using namespace std;struct cow{ int v,x; //v为音量,x为坐标}a[N];long long int c[N],d[N],t1[N],t2[N],s1[N],s2[N];bool cmp(cow a,cow b){ return a.v<b.v; //按照音量升序排列}void update1(int x,int val){ for(;x<N;x+=(x&(-x))) //这个函数用来更新音量比它小的牛的数量 c[x]+=val;}long long que1(int x){ long long ret=0; for(;x;x-=(x&(-x))) ret+=c[x]; return ret;}void update2(int x,int val) { for(;x<N;x+=(x&(-x))) d[x]+=val;}long long int que2(int x) //这个是用来求坐标和的。{ long long int ret=0; for(;x;x-=(x&(-x))) ret+=d[x]; return ret;}int main(){ int n,i; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) //注意树状数组从1开始,0处无意义 scanf("%d%d",&a[i].v,&a[i].x); sort(a+1,a+n+1,cmp); memset(c,0,sizeof(c)); //初始化 memset(d,0,sizeof(d)); for(i=1;i<=n;i++) { update1(a[i].x,1); t1[i]=que1(a[i].x); //这个就是这头牛在它前面的牛的数量 t2[i]=i-t1[i]; //这个是音量比它小但是坐标比它大的牛的数量 } //i为所有音量比它小的牛的数量 for(i=1;i<=n;i++) { update2(a[i].x,a[i].x); s1[i]=que2(a[i].x); //这个是所有x坐标比它小的牛的坐标和 s2[i]=que2(20000)-s1[i]; //这个是所有x坐标比它大的牛的坐标和 } //que(20000)为所有出现过的x坐标的总和 long long int ans=0; for(i=1;i<=n;i++) ans+=((t1[i]*a[i].x-s1[i])+(s2[i]-t2[i]*a[i].x))*a[i].v; printf("%lld\n",ans); } return 0;}
现在写博客最幸福的时刻就是给我的博文配图了。~(≧▽≦)/~啦啦啦
因为这道题真的不知道怎么说了,呵呵……
- POJ 1990 MooFest
- poj 1990 MooFest
- POJ - 1990 MooFest
- POJ 1990 MooFest
- Poj 1990 MooFest
- POJ 1990 MooFest
- POJ-1990-MooFest
- POJ 1990 MooFest
- poj 1990 - MooFest
- poj 1990 MooFest
- POJ 1990 MooFest
- POJ 1990 MooFest
- POJ 1990 MooFest
- POJ 1990 MooFest
- POJ 1990 MooFest
- POJ 1990 MooFest
- poj 1990 MooFest
- POJ 1990 MooFest
- localtimestamp
- 【动态规划】Codeforces 698A & 699C Vacations
- Unity3D-加载XML配置表文件
- last_day(d1)
- INTERVAL c1 set1
- POJ 1990 MooFest
- Linux常用软件安装
- java三大框架之spring事务传播行为
- JSPatch 实现原理详解
- extract(c1 from d1)
- dbtimezone
- current_timestamp
- 关于大神--->>>快速集成iOS基于RTMP的视频推流
- linux内核 策略路由之添加