HDU 2492 Ping pong

来源:互联网 发布:投资软件 编辑:程序博客网 时间:2024/06/06 04:49
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): 4243    Accepted Submission(s): 1570


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
13 1 2 3
 

Sample Output
1
 

Source
2008 Asia Regional Beijing
 

Recommend
gaojie
  
树状数组用来求 比某个数小的数的数量 和stars 那道题目类似。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
return a>b;
}
int c[100002];
int a[20002],b[20002],d[20002];
int lowbit(int i)
{
return i&(-i);
}
void add(int i,int d)
{
while(i<=100000)
{
c[i]+=d;
i+=lowbit(i);
}
}
int sum(int i)
{
int ret=0;
while(i>0)
{
ret+=c[i];
i-=lowbit(i);
}
return ret;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,n;
scanf("%d",&t);
while(t--)
{
cle(b),cle(c);
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
add(a[i],1);
b[i]=sum(a[i]-1);
}
cle(c);
for(int i=n;i>=1;i--)
{
add(a[i],1);d[i]=sum(a[i]-1);
}
ll ans=0;
for(int i=2;i<n;i++)
{
ans+=((ll)b[i]*(n-i-d[i])+(ll)(i-b[i]-1)*d[i]);
}
printf("%I64d\n",ans);
}
return 0;
}
HDU 2492 Ping pong - 风未定 - NGUNAUJ
 
0 0
原创粉丝点击