hdu2429Ping 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, 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

Source

2008
Asia Regional Beijing

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>

using namespace std;

int an[51000];
int s[110000];
const int N = 109999;
int lb(int x)
{
    return x&(-x);
}
void add(int i)
{
    if(i>N) return;
    s[i]++;
    add(i+lb(i));
}
int get(int i)
{
    if(i == 0)return 0;
    return s[i]+get(i-lb(i));
}
int taa[51000],tab[51000],tba[51000],tbb[51000];

int main()
{
    int z;
    cin>>z;
    while(z--)
    {
        int n,i,j,k;
        cin>>n;
        memset(s,0,sizeof(s));
        memset(taa,0,sizeof(taa));
        memset(tab,0,sizeof(tab));
        for(i = 1;i<=n;i++)
            scanf("%d",&an[i]);
        for(i = 1;i<=n;i++)
        {
            taa[i] = get(an[i]-1);
            tab[i] = i-1-get(an[i]);
            add(an[i]);
        }
        memset(s,0,sizeof(s));
        memset(tba,0,sizeof(tba));
        memset(tbb,0,sizeof(tbb));
        for(i = n;i>=1;i--)
        {
            tba[i] = get(an[i]-1);
            tbb[i] = n-i-get(an[i]);
            add(an[i]);
        }
        long long ans = 0;
        for(i = 1;i<=n;i++)
        {
            ans += taa[i]*tbb[i]+tab[i]*tba[i];
            ans += (i-1-taa[i]-tab[i])*(n-i)+(taa[i]+tab[i])*(n-i-tba[i]-tbb[i]);
        }
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-12-29 11:32:11

hdu2429Ping pong的相关文章

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —&quot;Pong&quot;

In this project, we will build a version of Pong, one of the first arcade video games (1972). While Pong is not particularly exciting compared to today's video games, Pong is relatively simple to build and provides a nice opportunity to work on the s

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

POJ3928Ping pong[树状数组 仿逆序对]

Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3109   Accepted: 1148 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 i

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

Did Pong Lie? (差分系统 判负环)

Did Pong Lie? 时间限制: 5 Sec  内存限制: 128 MB提交: 68  解决: 15[提交][状态][讨论版] 题目描述 Doctor Pong has two arrays of integers : a1 , a2 , ......, aN and b1 , b2 , ......, bM . His student Rong wants to know what these numbers are, but Pong won't tell him the number