zoukankan      html  css  js  c++  java
  • POJ 1990 MooFest

    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

     
    首先简要说明题意:
    给定2个序列 a,b
    输出所有的i,j(i!=j)对应abs(a[j]-a[i])*max(b[i],b[j])
     
    首先枚举所有的n方算法非常好想但是时间复杂度过大
    我们思考这个式子应该如何提取公因式
    abs(a[j]-a[i])这个部分每对i,j都不同
    而max(b[i],b[j]),我们可以考虑对于最大的数,所有与它构成的数对都是乘这个b,然后对于次大的数时,我们就可以不考虑最大的数,其它的数乘这个b,。。。
    如何快速计算各个abs(a[j]-a[i])的和呢?
    我们可以把它分成两部分
    a[j]>a[i]  sum(a[j])-a[i]*sl1;
    a[i]<a[j] a[i]*sl2-sum(a[j])
    首先我们把所有数根据a排序,把它的a值放入树状数组中那么就可以的到sum(a[j])分不同的区间就可以的到比它大和比它小的坐标和
    再根据b的大小排序,从大往小扫,还有我们要把这个数对应的位置变为0,这样才能够做到考虑后面的数不会算入
    还有对于sl1和sl2,我们可以另外建一个树状数组先把每个位置都标为1,然后每做完一个就更改为0,查询的时也像上面一样。
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int N=20005;
    struct A
    {
        int z,wz,id;
    }a[N];
    int c[N][2],n;
    bool cmp1(const A &t1,const A &t2)
    {
        return t1.wz<t2.wz;
    }
    bool cmp2(const A &t1,const A &t2)
    {
        return t1.z<t2.z;
    }
    void add(int x,int h,int xh)
    {
        for(;x<=n;x+=x&-x)
            c[x][xh]+=h;
    }
    int askk(int x,int xh)
    {
        int re=0;
        for(;x;x-=x&-x)
            re+=c[x][xh];
        return re;    
    }
    int main()
    {
        long long ans=0;
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d%d",&a[i].z,&a[i].wz),add(i,1,1);
        sort(a+1,a+n+1,cmp1);
        for(int i=1;i<=n;++i)
            a[i].id=i,add(i,a[i].wz,0);
        sort(a+1,a+n+1,cmp2);
        for(int i=n;i;--i)
        {
            add(a[i].id,-1,1); 
            add(a[i].id,-a[i].wz,0);
            ans+=(long long)a[i].z*(askk(n,0)-2*askk(a[i].id,0)+a[i].wz*(askk(a[i].id,1)*2-askk(n,1)));
        }
        printf("%lld",ans);
        return 0;    
    }
  • 相关阅读:
    【并查集】连接格点C++
    【拓扑排序】威虎山上的分配C++
    CF39D Cubical PlanetC++
    【拓扑排序】排队C++
    [USACO09OCT]Invasion of the Milkweed】乳草的侵占C++
    免杀常用手段
    DELPHI 线程类
    动态注册OCX
    DELPHI 常用文件路径
    套接字(TCP)
  • 原文地址:https://www.cnblogs.com/bzmd/p/9381434.html
Copyright © 2011-2022 走看看