• Ping pong(树状数组求序列中比某个位置上的数小的数字个数)


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492

    Ping pong

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4961    Accepted Submission(s): 1811


    Problem Description
    N(3<=N<=20000) 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(1<=T<=20), indicating the number of test cases, followed by T lines each of which describes a test case.


    Every test case consists of N + 1 integers. The first integer is N, the number of players. Then N distinct integers a1, a2 … aN follow, indicating the skill rank of each player, in the order of west to east. (1 <= ai <= 100000, i = 1 … N).
     
    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

     题意:

    一条大街上住着n个乒乓球爱好者,经常组织比赛。每个人都有一个技能值ai,每场比赛需要3个人:两名选手和一名裁判。规定裁判位置必须在两个选手的中间,而且技能值也必须在两个选手的中间,问一共能组织多少种比赛

    题解:考虑第i个人当裁判的呃情况,求当前某一个位置的前面和后面有多少个数字比这个数字小,思路有点像求逆序对数的题目,这个题来自于刘汝佳大神的树状数组练习题,P197页很详细的分析,这里注意读题,他的题解都是建立在每个人有独特的技术值,即每个数在树状数组中只会出现一次,还要注意,如果数字太大的话要先离散化,但是这个题不离散也可以但是要用long long输出

    代码:
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<string>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 #define LL long long
     8 const LL N = 100005;
     9 LL mp[N];
    10 LL tree[N];
    11 LL lowbit(LL x) {
    12     return x&(-x);
    13 }
    14 LL sum(LL x){
    15     LL ans = 0;
    16     while(x>0){
    17         ans += tree[x];
    18         x-=lowbit(x);
    19     }
    20     return ans;//记得写返回值
    21 }
    22 int n;
    23 void add(LL x){
    24     while(x<N){
    25         tree[x]++;
    26         x+=lowbit(x);
    27     }
    28 }
    29 void init()
    30 {
    31     for(int i = 0; i < N; i++)
    32         tree[i] = 0;
    33 }
    34 LL c[N];//当前位置前面有多少人技能值比它小
    35 LL d[N];//当前位置后面有多少人技能值比它小
    36 int main()
    37 {
    38     int T;
    39     scanf("%d",&T);
    40     while(T--)
    41     {
    42         scanf("%d",&n);
    43         for(int i = 0; i < n; i++)
    44         {
    45             scanf("%lld",&mp[i]);
    46         }
    47 
    48         LL ans = 0;
    49         init();
    50         for(int i = 0; i < n ; i++){//枚举所有的位置上的人当裁判的比赛局数
    51             c[i] = sum(mp[i]);add(mp[i]);
    52         }
    53         init();
    54         for(int j = n-1; j >=0; j--){
    55             d[j] = sum(mp[j]);add(mp[j]);
    56         }
    57         for(int i = 0; i < n; i++){
    58             ans = ans + c[i]*(n-i-d[i]-1)+(i-c[i])*d[i];
    59         //printf(" c = %lld d = %lld ans = %lld ",c[i], d[i], ans);
    60         }
    61         printf("%lld
    ",ans);
    62     }
    63   return 0;
    64 }
     
  • 相关阅读:
    并查集
    强联通分量,缩点
    最短路径
    最小生成树
    拓扑排序
    图的遍历
    图论基础知识
    数据库四种隔离级别
    MySQL 索引 乐观锁 悲观锁
    MYSQL+正则
  • 原文地址:https://www.cnblogs.com/shanyr/p/5220480.html
Copyright © 2020-2023  润新知