POJ 3928-Ping pong(樹狀陣列+加/乘法原理)
Ping pong
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2982 | Accepted: 1103 |
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).
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
題目意思:
一條街上有n個乒乓球愛好者,每個人都有一個不同的技能值 a 。每場比賽需要選3個人:1個裁判,2個選手,有一個奇怪的規定:裁判必須住在選手中間,且裁判的技能值也要在選手中間。求能有多少種比賽。
解題思路:(來源已未可考)
列舉第 i 個人當裁判,設 a0 到 ai-1 中,有 bi 個數比 ai 小,ai+1 到 an-1中有 di 個數比 ai 大,那麼比賽種數為 b[i]*(n-i-1-d[i])+(i-b[i])*d[i].
要求 bi 和 di ,可以採用樹狀陣列。ai 的最大值為100000,那麼BIT的範圍是1-100000,A[1,100000]每個數 A[i] 代表一個人,一開始BIT中的每個位置上的數都是0,表示這個位置上沒有人,C[] 表示BIT中的輔助陣列,初始化為0。每當插入一個 ai ,相當於把值為 ai 的人放在BIT中的相應位置上,即對應位置上的值要加1,從初始的0變為1,此時需要修改相應的C[] 中的值。
由於我要求的是比 ai 小的數的個數,那麼這些比 ai 小的數在BIT中的位置必然是在 ai 之前,也就是這些位置上的值為1,那麼 A[ai] 的字首和就等於這些數的個數。
當要求 ai+1 到 an-1 中比 ai 小的數時,把陣列 a[] 反過來插入BIT中即可。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <map>
#include <vector>
#include <cstdlib>
#define maxn 100010
#define ll long long
using namespace std;
int a[maxn],c[maxn];
int mins[maxn],maxs[maxn];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int d)
{
while(x<=maxn)
{
c[x]+=d;
x+=lowbit(x);
}
}
int sum(int i)
{
int res=0;
while(i>0)
{
res+=c[i];
i-=lowbit(i);
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
memset(c,0,sizeof(c));
int n;
cin>>n;
for(int i=1; i<=n; ++i)
cin>>a[i];
for(int i=1; i<=n; ++i)
{
mins[i]=sum(a[i]);//計算比a[i]小的數的個數
update(a[i],1);//放進一個a[i]
}
memset(c,0,sizeof(c));
for(int i=n; i>0; --i)
{
maxs[i]=sum(a[i]);
update(a[i],1);
}
ll ans=0;
for(int i=1; i<=n; i++)
ans+=(mins[i]*(n-i-maxs[i])+(i-1-mins[i])*maxs[i]);
cout<<ans<<endl;
}
return 0;
}
/**
1
3
1 2 3
**/
相關文章
- POJ 3928 Ping pong(樹狀陣列)陣列
- poj 2481 樹狀陣列陣列
- POJ-2352 Stars(樹狀陣列)陣列
- 【二維樹狀陣列】poj 2155 Matrix陣列
- 二維樹狀陣列-poj2155陣列
- poj 1195 二維樹狀陣列陣列
- POJ 2352-Stars(樹狀陣列-星系)陣列
- POJ 3321-Apple Tree(樹狀陣列)APP陣列
- POJ 2352 Stars(簡單樹狀陣列)陣列
- 二維樹狀陣列--poj1195陣列
- HDU 1541 & POJ 2352 Stars (樹狀陣列)陣列
- POJ 3067-Japan(樹狀陣列-逆序數)陣列
- POJ 1195 Mobile phones(二維樹狀陣列)陣列
- POJ 3321 Apple Tree(dfs+樹狀陣列)APP陣列
- 樹狀陣列陣列
- POJ3321 Apple Tree(DFS序 + 樹狀陣列)APP陣列
- POJ 2299-Ultra-QuickSort(樹狀陣列求逆序數)UI陣列
- 解析樹狀陣列陣列
- POJ3468 A Simple Problem with Integers---樹狀陣列(區間問題)陣列
- POJ 2352(順路講解一下樹狀陣列)陣列
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- 樹狀陣列詳解陣列
- 樹狀陣列基礎陣列
- hdu 3874 樹狀陣列陣列
- 二維樹狀陣列陣列
- 【樹狀陣列 求比其小的個數】poj 2353 Stars陣列
- 樹狀陣列模板題 & (樹狀陣列 1:單點修改,區間查詢)陣列
- 樹狀陣列和逆序對陣列
- hdu 5147 樹狀陣列陣列
- 樹狀陣列快速入門陣列
- POJ 2155-Matrix(二維樹狀陣列-區間修改 單點查詢)陣列
- 樹狀陣列模板+習題集陣列
- 樹狀陣列3種基本操作陣列
- 學習筆記----樹狀陣列筆記陣列
- 樹狀陣列upc1976陣列
- CSU 4441 Necklace (樹狀陣列/LIS)陣列
- 樹狀陣列(我是真小白)陣列
- 線段樹+差分——【模板】樹狀陣列2陣列