LA4329:Ping pong(树状数组)
2015-06-12 22:03
429 查看
DescriptionN(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?InputThe 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 ).OutputFor each test case, output a single line contains an integer, the total number of different games.Sample Input
1 3 1 2 3Sample Output
1
题意:
一条大街上住着n个乒乓球爱好者,经常组织比赛。每个人都有一个技能值ai,每场比赛需要3个人:两名选手和一名裁判。规定裁判位置必须在两个选手的中间,而且技能值也必须在两个选手的中间,问一共能组织多少种比赛
思路:
考虑第i个人当裁判的情形,假设a1到a[i-1]中有ci个比ai小,那么就有(i-1)-ci个比ai大,同理,假设a[i+1]到an中有di个比ai小,那么就有(n-i)-di个比ai大,然后根据乘法原理和加法原理,i当裁判有ci(n-i-di)+(i-ci-1)*di,这样问题就转化为求c,d
#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;#define LS 2*i#define RS 2*i+1#define UP(i,x,y) for(i=x;i<=y;i++)#define DOWN(i,x,y) for(i=x;i>=y;i--)#define MEM(a,x) memset(a,x,sizeof(a))#define W(a) while(a)#define gcd(a,b) __gcd(a,b)#define LL long long#define N 1000000#define MOD 1000000007#define INF 0x3f3f3f3f#define EXP 1e-8#define lowbit(x) (x&-x)int a,c,d,x;int s1,s2;int sum(int x){ int ret = 0; while(x>0) { ret+=c[x]; x-=lowbit(x); } return ret;}void add(int x,int d){ while(x<=100005) { c[x]+=d; x+=lowbit(x); }}int main(){ int n,t,i,j,k; scanf("%d",&t); while(t--) { MEM(x,0); scanf("%d",&n); for(i = 1; i<=n; i++) scanf("%d",&a[i]); MEM(c,0);//c数组统计该方向扫描的过程中,a[i]的个数 for(i = 1; i<=n; i++) { add(a[i],1);//放进一个a[i] s1[i] = sum(a[i]-1);//计算比a[i]小的数的个数 } MEM(c,0); for(i = n; i>=1; i--) { add(a[i],1); s2[i] = sum(a[i]-1); } LL ans = 0; for(i = 2; i<=n; i++) { ans+=s1[i]*(n-i-s2[i])+(i-s1[i]-1)*s2[i]; } printf("%lld\n",ans); } return 0;}
相关文章推荐
- Windows xcopy
- Android利用Fiddler进行网络数据抓包
- 使用JS生成Audio元素
- Python入门
- [BZOJ 2083] Poi 2010 Intelligence test · 二分
- 转 值得推荐的C/C++框架和库
- [LeetCode] Binary Tree Upside Down
- leetcode之Maximum Depth of Binary Tree 以及Balanced Binary Tree
- Ubuntu终端安装JDK
- U盘安装Centos 7.0
- nullable,kindof,泛型新特性
- Runloop
- 程序员你造吗?2016年加州将建造子弹头火车,你还在CODE?
- gcc-5.1.0编译笔记
- iOS-iOS应用提交
- MaterialImageView
- 代码雨
- 怎样才能看上去不那末白痴
- opencl入门介绍
- 1402 最大值