poj 1990 MooFest

    xiaoxiao2022-06-22  19

    MooFest

    Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6971 Accepted: 3134

    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


    【分析】 做多了树状数组再做题就感觉很轻松啊…虽然WA了一次,不过是因为开成int了…原谅自己。 首先以v为关键字从小到大排序,然后再逐个插入w,咱们先考虑w左端,于是维护两个树状数组,一个用来求 1~w-1 中总共有多少个数出现过,一个用来求 1~w-1 中所有被插入的w的总和,然后花式相减再乘个v完事。右区间同理,只是涉及了减法而已。 (顺便默默吐槽cin的读入速度…用scanf竟然比cin快了500ms还多…)


    【代码】

    //POJ 1990 MooFest #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define ll long long #define fo(i,j,k) for(i=j;i<=k;i++) using namespace std; int n,m,k; ll ans; ll c[20050],d[20050]; struct node { ll v,w; }a[20050]; inline bool comp(const node &x,const node &y) {return x.v<y.v;} inline ll lowbit(ll x) {return x&(-x);} inline void add(ll x) { int i,j; for(i=x;i<=20000;i+=lowbit(i)) c[i]+=x,d[i]+=1; } inline ll get(ll x,ll y) { ll i,j,sum=0; if(y==1) for(i=x;i;i-=lowbit(i)) sum+=c[i]; else for(i=x;i;i-=lowbit(i)) sum+=d[i]; return sum; } int main() { ll i,j; scanf("%d",&n); fo(i,1,n) scanf("%lld%lld",&a[i].v,&a[i].w); sort(a+1,a+n+1,comp); fo(i,1,n) { ll tmp_1=get(a[i].w-1,1),tmp_2=get(a[i].w-1,2); ans+=(a[i].w*tmp_2-tmp_1)*a[i].v; tmp_1=get(20000,1)-get(a[i].w,1); tmp_2=get(20000,2)-get(a[i].w,2); ans+=(tmp_1-tmp_2*a[i].w)*a[i].v; add(a[i].w); } printf("%lld\n",ans); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1123033.html

    最新回复(0)