zoukankan      html  css  js  c++  java
  • poj1990树状数组

    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
    我觉得我需要收回当初说树状数组比线段树简单这句话。。太坑了,一题比一题坑。。完全按题解写的
    题意:给v【i】,x【i】要求所以的牛的音量和即x【i】-x【j】*max(v【i】,v【j】)之和
    题解:两个树状数组数组一起使用,一个求x之前的比x坐标小的数(a),一个求x之前的比x坐标小的坐标和(b);
    那么比x小的坐标和x的坐标的总坐标差是a*(e[i].x)-b;比x大的坐标和x的坐标的总坐标差是总坐标-b-(i-1-a)*e[i].x
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    
    using namespace std;
    
    const double g=10.0,eps=1e-9;
    const int N=20000+5,maxn=32000+5,inf=0x3f3f3f3f;
    
    int s[2][N];
    struct edge{
        ll v,x;
    }e[N];
    
    bool comp(const edge &a,const edge &b)
    {
        return a.v<b.v;
    }
    void add(int i,ll x,int d)
    {
        while(i<=N){
            s[d][i]+=x;
            i+=i&(-i);
        }
    }
    ll sum(int i,int d)
    {
        ll ans=0;
        while(i>0){
            ans+=s[d][i];
            i-=i&(-i);
        }
        return ans;
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
     //   cout<<setiosflags(ios::fixed)<<setprecision(2);
        int n;
        while(cin>>n){
            memset(s,0,sizeof s);
            for(int i=1;i<=n;i++)cin>>e[i].v>>e[i].x;
            sort(e+1,e+1+n,comp);
            ll ans=0;
            for(int i=1;i<=n;i++)
            {
                ll a=sum(e[i].x,0),b=sum(e[i].x,1);
            //    cout<<sum(N,1)-b-(i-1-a)*e[i].x<<endl;
                ans+=(a*e[i].x-b+sum(N,1)-b-(i-1-a)*e[i].x)*e[i].v;
                add(e[i].x,1,0);//0是比x小的牛的个数
                add(e[i].x,e[i].x,1);//1是比x小的牛的距离和
            }
            cout<<ans<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    大数据时代:基于微软案例数据库数据挖掘知识点总结(Microsoft 时序算法)
    大数据时代:基于微软案例数据库数据挖掘知识点总结(结果预测篇)
    svg图片的缩放拖拽
    计算机网络之以太网,通俗点的说法
    yield 与生成器
    安卓开发第一记 android stdio 安装后 新建测试项目报错
    Javascript的异常捕获机制
    小而实用的工具插件集锦(JQGrid,zTree)
    proxifier 注册码 +电脑全局代理设置
    雷林鹏分享:jsp 发送邮件
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6776245.html
Copyright © 2011-2022 走看看