LA 4329 树状数组
来源:互联网 发布:淘宝有哪家af代购正品 编辑:程序博客网 时间:2024/06/05 17:36
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 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 (1T20) , 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 ( 1ai100000 , 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
题意:每场比赛需要3个人,裁判必须住在两个选手之间,技能值也在两个选手之间,求一共能组织多少种比赛
思路:考虑第I人当裁判的情形,种数有前i-1中比他小的*后(n-i)中比他大的和前i-1中比他大的*后(n-i)中比他小的。
问题转化为动态地修改单个元素值求前缀和!这正是bit的标准用法!
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 100080#define LL long long intint c[maxn],key[maxn],leftsmall[maxn],rightsmall[maxn];int lowbit(int x){return x & (-x);}int sum(int x){int ans = 0;while(x > 0){ans += c[x];x -= lowbit(x);}return ans;}void update(int x,int add){while(x < maxn){c[x] += add;x += lowbit(x);}}int main(){//freopen("in.txt","r",stdin);int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);memset(c,0,sizeof(c));memset(leftsmall,0,sizeof(leftsmall));memset(rightsmall,0,sizeof(rightsmall));for(int i = 1;i <= n;i++){scanf("%d",&key[i]);leftsmall[i] = sum(key[i] - 1);update(key[i],1);}memset(c,0,sizeof(c));for(int i = n;i >= 1;i--){rightsmall[i] = sum(key[i]-1);update(key[i],1);}LL ans = 0;for(int i = 1;i <= n;i++){ans += (LL)(leftsmall[i] * (n - i - rightsmall[i])) + (LL)(rightsmall[i] * (i - 1 - leftsmall[i]));}cout << ans << endl;//printf("%d\n",ans);}return 0;}
- LA 4329 树状数组
- LA 4329 树状数组
- LA 4329(树状数组)
- LA 4329(树状数组)
- LA 4329 树状数组入门
- LA 4329/uvalive 4329 (树状数组)
- LA 4329 Ping pong 树状数组
- LA 4329 Ping pong / 树状数组
- LA 4329 - Ping pong 树状数组
- LA 4329 - Ping pong(树状数组)
- UVA 1428 || LA 4329 PingPang (树状数组)
- (LA 4329) Ping Pang --树状数组
- 树状数组(乒乓比赛,LA 4329)
- LA 4329 Ping pong [树状数组]
- LA 4239 树状数组
- LA - 4329 - Ping pong(树状数组/线段树)
- LA 4329 - Ping pong 树状数组(Fenwick树)
- 树状数组 LA 4329 亚洲赛北京赛区题
- 注册程序的简单实现
- KMP算法
- 对“卸磨杀驴”的理解
- 发送飞信免费短信API
- 密码学
- LA 4329 树状数组
- 2138 图结构练习——BFSDFS——判断可达性
- 安装K/3 Cloud过程中发现的两个新问题。
- 关于Go语言中nil和interface的问题
- struts2的配置元素package
- HDU 1087 Super Jumping! Jumping! Jumping!
- 随想录(科技企业中股票和融资)
- 14 MVCC的可见性
- c++中getline函数