POJ-3928 Ping pong(树状数组)

Ping pong

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2985   Accepted: 1104

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

写这题顺便练了一下重载运算符

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <iostream>
 9 #include "algorithm"
10 using namespace std;
11 typedef long long LL;
12 const int MAX=100005;
13 int T;
14 int n;
15 int c[MAX];
16 struct Peo{
17     int name;
18     int sco;
19     bool operator <(const Peo &zt) const {
20         if (sco!=zt.sco)
21          return sco<zt.sco;
22         return name<zt.name;
23     }
24 }stu[MAX];
25 void add(int x,int y){
26     for (;x<=n;c[x]+=y,x+=(x&-x));
27 }
28 LL search(int x){
29     LL an(0);
30     for (;x>0;an+=c[x],x-=(x&-x));
31     return an;
32 }
33 int main(){
34     freopen ("tennis.in","r",stdin);
35     freopen ("tennis.out","w",stdout);
36     int i,j;
37     scanf("%d",&T);
38     while (T--){
39     scanf("%d",&n);
40     for (i=1;i<=n;i++)
41     {scanf("%d",&stu[i].sco);
42      stu[i].name=i;
43     }
44     sort(stu+1,stu+n+1);
45     memset(c,0,sizeof(c));
46     LL x1,x2,x3,x4,ans(0);
47     add(stu[1].name,1);
48     for (i=2;i<=n;i++)
49     {x1=search(stu[i].name-1);
50      x2=stu[i].name-1-x1;
51      x3=search(n)-search(stu[i].name);
52      x4=n-stu[i].name-x3;
53      ans=ans+x1*x4+x2*x3;
54      add(stu[i].name,1);
55     }
56     printf("%lld\n",ans);
57     }
58     return 0;
59 }
时间: 2025-01-17 09:05:11

POJ-3928 Ping pong(树状数组)的相关文章

POJ 3928 Ping pong 树状数组模板题

开始用瓜神说的方法撸了一发线段树,早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #includ

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

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

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

UVALive 4329 Ping pong (树状数组)

白书上的例题.花了很多时间在找bug上,刚学树状数组,这道题add操作时要注意上限不是n. #include <bits/stdc++.h> using namespace std; #define ll long long const ll maxn = 1e5 + 10; ll C[maxn]; ll n; inline ll lowbit(ll x) { return x & (-x); } ll sum(ll x) { ll ret = 0; while(x > 0) {

LA4329 Ping pong 树状数组

题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两名选手之间.问一共能组织多少种比赛. 分析:考虑第i个人,假设a1到ai-1中有ci个比ai小,那么就有(i-1)-ci个比ai大:同理,如果ai+1到an中有di个比ai小,那么就有(n-i)-di个比ai大.i当裁判就有:ci * (n-i-di) + (i-1-ci)*di种比赛. 然后问题就

UVA1428 - Ping pong(树状数组)

题目链接 题目大意:有N个人,每个人都有一个技能值ai,现在要开展乒乓球比赛,要求要有两个选手和一个裁判,要求裁判需要在两名选手的中间而且技能值也是在两名选手的中间,问可以开展多少场比赛. 解题思路:对于第i个选手当裁判的话,设它前面位置的选手有ci个技能值比它低的,那么就有i - 1 - ci个比它高的,对于第i选手后面的位置,同样有di个技能值比它低的,那么就有N - i - di个比它高的.组合一下:ci?(N - i - di) + (i - 1 - ci) ? di.那么对于ci的值,

hdu 1541/poj 2352:Stars(树状数组,经典题)

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4052    Accepted Submission(s): 1592 Problem Description Astronomers often examine star maps where stars are represented by points on a plan

POJ 2892 Tunnel Warfare (树状数组+二分)

题目大意: 三个操作 D pos  将pos位置摧毁,让它和周围不相连. Q pos 问和pos 相连的有多少个村庄. R 修复最近摧毁的村庄. 思路分析: 树状数组记录这个区间有多少个1. 如果  [s-e] 有e-s+1个1 的话.那么这个区间是相连的. 这样的话,我们就可以用二分的办法求出与某个位置最大相连的数量. 还有这里二分 while(l<=r) { if(满足) { ans=mid; l=mid+1; } else r=mid-1; } #include <cstdio>