HDU2492 Ping pong

题解:

每个数,求出左右两边比这个数大的和比这个数小的,然后以每个数作为裁判,求和就行了

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define LL long long
#define CLR(x) memset(x,0,sizeof x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=20100;
const int N=100010;
const int mod=1e9+7;
const int INF=1e9;

int a[maxn],l_num[N],r_num[N];
LL L[N],R[N];
int n;

int lowbit(int x){return x&-x;}

void add(int x,int v,int*c){
    while(x<N){
        c[x]+=v;
        x+=lowbit(x);
    }
}

LL sum(int x,int*c){
    LL cnt=0;
    while(x){
        cnt+=c[x];
        x-=lowbit(x);
    }
    return cnt;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        CLR(l_num);CLR(r_num);
        for(int i=1;i<=n;i++){
            L[i]=sum(a[i]-1,l_num);//左边比a[i]小
            R[n+1-i]=sum(a[n+1-i]-1,r_num);//右边比a[n+1-i]小
            add(a[i],1,l_num);
            add(a[n+1-i],1,r_num);
        }
        LL ans=0;
        for(int i=1;i<=n;i++){
            //cout<<L[i]<<" "<<R[i]<<endl;
            ans+=L[i]*(n-i-R[i])+(i-1-L[i])*R[i];
        }
        printf("%lld\n",ans);
    }
    return 0;
}
时间: 2024-12-07 13:06:28

HDU2492 Ping pong的相关文章

Ping pong

Description N(3N20000) 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

POJ 3928 &amp; HDU 2492 Ping pong(树状数组求逆序数)

题目链接: PKU:http://poj.org/problem?id=3928 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492 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 im

uva 1428 - Ping pong(树状数组)

题目链接:uva 1428 - Ping pong 题目大意:一条大街上住着n个乒乓球爱好者,经常组织比赛.每个人都有一个不同的能力值,每场比赛需要3个人,裁判要住在两个选手之间,并且能力值也要在选手之间,问说最多能举行多少场比赛. 解题思路:预处理出bi和ci分别表示说在1~i中能力值比第i个人小的人和i+1~n中能力值比第i个人小的.处理过程用树状数组维护即可. #include <cstdio> #include <cstring> #include <algorith

Motion sensing game (Ping Pong Game)

Project Demonstration Here is the source code of the project based on OpenCV anc C++. Before you run this code on Linux, you should install the OpenCV library  first. #include<opencv/highgui.h> #include<opencv2/opencv.hpp> #include<string&g

poj3928 Ping pong 树状数组

http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2087   Accepted: 798 Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player

【HDOJ】2492 Ping pong

线段树+离散化. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define MAXN 20005 6 #define lson l, mid, rt<<1 7 #define rson mid+1, r, rt<<1|1 8 9 int buf[MAXN], bk[MAXN]; 10 int sum[MAXN<<2], n; 11 12 int

UVAlive - 4329 —— Ping pong 【树状数组】

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 #include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; const int MAXA = 100000 + 5; int n; int c[MAXA]; int sum(int i) { int

HDU 2492 Ping pong (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong 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

UVA - 1428 Ping pong

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