N (3N
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?
The first line of the input contains an integer T(1T
20) <tex2html_verbatim_mark>, indicating the
number of test cases, followed by T<tex2html_verbatim_mark>lines each of which
describes a test case.
Every test case consists of N +
1 integers. The first integer is N <tex2html_verbatim_mark>, the number
of players. Then N distinct
integersa1, a2...aN <tex2html_verbatim_mark>follow,
indicating the skill rank of each player, in the order of west to east
( 1ai
100000 <tex2html_verbatim_mark>, i =
1...N).
For each test case, output a single line contains an integer, the total number of different games.
1 3 1 2 3
1
题目大意:
一条大街上住着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
种比赛。(感觉这种思路简直碉堡了)
然后问题就转化为了计算数组c和数组d。这样的话就很容易想到使用树状数组去计算前缀和。
见代码:
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define INF 0x3f3f3f3f 16 #define inf -0x3f3f3f3f 17 #define lson k<<1, L, mid 18 #define rson k<<1|1, mid+1, R 19 #define mem0(a) memset(a,0,sizeof(a)) 20 #define mem1(a) memset(a,-1,sizeof(a)) 21 #define mem(a, b) memset(a, b, sizeof(a)) 22 #define FOPENIN(IN) freopen(IN, "r", stdin) 23 #define FOPENOUT(OUT) freopen(OUT, "w", stdout) 24 25 template<class T> T CMP_MIN(T a, T b) { return a < b; } 26 template<class T> T CMP_MAX(T a, T b) { return a > b; } 27 template<class T> T MAX(T a, T b) { return a > b ? a : b; } 28 template<class T> T MIN(T a, T b) { return a < b ? a : b; } 29 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; } 30 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } 31 32 //typedef __int64 LL; 33 typedef long long LL; 34 const int MAXN = 20005; 35 const int MAXM = 100005; 36 const double eps = 1e-12; 37 38 int T, n, a[MAXN], c[MAXN], d[MAXN], x[MAXM]; 39 40 int lowbit(int x) 41 { 42 return x & (-x); 43 } 44 45 int getSum(int k) 46 { 47 int ans = 0; 48 while(k>0) 49 { 50 ans += x[k]; 51 k -= lowbit(k); 52 } 53 return ans; 54 } 55 56 void edit(int k) 57 { 58 while(k <= 100000) 59 { 60 x[k] += 1; 61 k += lowbit(k); 62 } 63 } 64 65 int main() 66 { 67 // FOPENIN("in.txt"); 68 // FOPENOUT("out.txt"); 69 while(~scanf("%d", &T)) while(T--) 70 { 71 mem0(x); mem0(c); mem0(d); 72 scanf("%d", &n); 73 for(int i=1; i<=n; i++) scanf("%d", &a[i]); 74 for(int i=1; i<=n; i++) { edit(a[i]); c[i] = getSum(a[i]-1); } 75 mem0(x); 76 for(int i=n; i>=1; i--) { edit(a[i]); d[i] = getSum(a[i]-1); } 77 LL ans = 0; 78 for(int i=2;i<n;i++) 79 { 80 ans += (LL)c[i]*(n-i-d[i]) + (LL)d[i]*(i-c[i]-1); 81 } 82 printf("%lld\n", ans); 83 } 84 return 0; 85 }
LA4329 Ping pong(树状数组与组合原理),布布扣,bubuko.com
原文:http://www.cnblogs.com/gj-Acit/p/3584645.html