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 }


  • 相关阅读:
    原生化:AnDevCon 2014 McVeigh 的主题演讲
    MVC 5 App 通过 Facebook OAuth2 登陆(Sign-on)的问题
    MVC 5 第三章 HTML Helper
    Node.js 初探
    MVC 5 第二章 项目结构
    MVC 5 第一章 起航
    新的开始
    Xcode开发如何在调试时轻松找到程序在哪里崩溃?
    100个直接可以拿来用的JavaScript实用功能代码片段(转)
    js得到当前页面的url信息方法(JS获取当前网址信息)
  • 原文地址:https://www.cnblogs.com/RRirring/p/4655649.html
Copyright © 2011-2022 走看看