zoukankan      html  css  js  c++  java
  • LA 4329(树状数组)

    题目描述:

    N <tex2html_verbatim_mark>(3$ le$N$ le$20000) <tex2html_verbatim_mark>ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank, they often compete with each other. If two players want to compete, they must choose a referee among other ping pong players and hold the game in the referee's house. For some reason, the contestants can't choose a referee whose skill rank is higher or lower than both of theirs. The contestants have to walk to the referee's house, and because they are lazy, they want to make their total walking distance no more than the distance between their houses. Of course all players live in different houses and the position of their houses are all different. If the referee or any of the two contestants is different, we call two games different. Now is the problem: how many different games can be held in this ping pong street?

    Input 

    The first line of the input contains an integer T <tex2html_verbatim_mark>(1$ le$T$ le$20) <tex2html_verbatim_mark>, indicating the number of test cases, followed by T <tex2html_verbatim_mark>lines each of which describes a test case.

    Every test case consists of N + 1 <tex2html_verbatim_mark>integers. The first integer is N <tex2html_verbatim_mark>, the number of players. Then N <tex2html_verbatim_mark>distinct integers a1a2...aN <tex2html_verbatim_mark>follow, indicating the skill rank of each player, in the order of west to east ( 1$ le$ai$ le$100000 <tex2html_verbatim_mark>, i = 1...N <tex2html_verbatim_mark>).

    Output 

    For each test case, output a single line contains an integer, the total number of different games.

    Sample Input 

    1
    3 1 2 3
    

    Sample Output 

    1


    分析:对于每个a[i],算出从a[1]到a[i-1]比a[i]小的数有c[i]个,a[i+1]到a[n]比a[i]小的有d[i]个,则考虑当i为裁判时,可能的方案数为:c[i](n-i-d[i])+d[i](i-c[i]-1)。关键是算c[i]、d[i]。运用树状数组可以算出c[i],d[i]。


     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstring>
     4 #include <ctime>
     5 #include <iostream>
     6 #include <algorithm>
     7 #include <set>
     8 #include <vector>
     9 #include <sstream>
    10 #include <queue>
    11 #include <typeinfo>
    12 #include <fstream>
    13 #include <map>
    14 #include <stack>
    15 using namespace std;
    16 const int maxn=20010;
    17 const int maxv=100010;
    18 int a[maxn],c[maxv],cc[maxn],d[maxn],n;
    19 int lowbit(int x){
    20     return x&-x;
    21 }
    22 int sum(int x){
    23     int res=0;
    24     while(x>0){
    25         res+=c[x];
    26         x-=lowbit(x);
    27     }
    28     return res;
    29 }
    30 void add(int x,int v){
    31     while(x<=maxv){
    32         c[x]+=v;
    33         x+=lowbit(x);
    34     }
    35 }
    36 int main()
    37 {
    38     int t;
    39     scanf("%d",&t);
    40     while(t--){
    41         scanf("%d",&n);
    42         memset(c,0,sizeof(c));
    43         for(int i=1;i<=n;i++)
    44             scanf("%d",&a[i]);
    45         for(int i=1;i<=n-1;i++){
    46             cc[i]=sum(a[i]-1);
    47             add(a[i],1);
    48         }
    49         memset(c,0,sizeof(c));
    50         for(int i=n;i>=2;i--){
    51             d[i]=sum(a[i]-1);
    52             add(a[i],1);
    53         }
    54         ll ans=0;
    55         for(int i=1;i<=n-1;i++)
    56             ans+=cc[i]*(n-i-d[i])+(i-cc[i]-1)*d[i];
    57         printf("%lld
    ",ans);
    58     }
    59     return 0;
    60 }


  • 相关阅读:
    django项目环境搭建备忘
    Python IDE的选择和安装
    MAC上python环境搭建
    hadoop1.2.1+hbase0.90.4+nutch2.2.1+elasticsearch0.90.5配置(伪分布式)
    ubuntu下hadoop完全分布式部署
    ubuntu下集群设置静态ip
    C语言调用库函数实现生产者消费者问题
    poj 1703(带权并查集)
    poj 1330
    poj1724
  • 原文地址:https://www.cnblogs.com/RRirring/p/4655649.html
Copyright © 2011-2022 走看看