首页 > 代码库 > HDU 2492 pingpang
HDU 2492 pingpang
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, 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?
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
2008 Asia Regional Beijing
一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术,每个人都有一个不同的技能值a(i)。每场比赛需要3个人:两名选手,一名裁判。他们有一个奇怪的规定,即裁判必须住在两名选手的中间,并且技能值也在两名选手之间。问一共能组织多少种比赛。
【输入格式】输入第一行为数据组数T(1<=T<=20)每组数据占一行,首先是整数n(3《=你《=20000),然后是n个不同的整数,即a(1),a(2)……a(n)(1<=a(i)<=100000),按照住所从左到右的顺序给出每个乒乓爱好者的技能值。
【输出格式】对于每组数据,输出比赛总数的值。
【分析】我们考虑第i个人当裁判的情形。假设a(1)到a(i-1)中有c(i)个比a(i)小,那么就有i-1-c(i)个比a(i)大;同理,假设a(i+1)到a(n)中有d(i)个比a(i)小,那么就是n-i-d(i)个比a(i)大,根据乘法原理和加法原理,i当裁判时有c(i)*(n-i-d(i))+d(i)*(i-1-c(i))种比赛。这样问题就转换为了求c(i)和d(i)。
c(i)可以这么求,从左到右扫描所有的a(i),令x[j]表示目前为止已经考虑过的所有a(i)中是否存在一个a(i)=j(x[j]=0表示不存在,=1表示存在),则c(i)就是前缀和x[1]+x[2]+...x[a(i)-1]。]
这道题就是树状数组的典型应用,当然也可以用线段树解,但是代码冗长效率还不高,下面贴上我写的代码
#include<stdio.h> #include<string.h> #include<stdlib.h> #define A 100010 #define N 20010 int C[A],c[N],d[N]; int a[N]; int maxa=0; int max(int a,int b) { if(a>b) return a; return b; } int lowbit(int x) { return x&-x; } 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<=maxa){//注意不是n,是a[]中最大的那个值 C[x]+=d; x+=lowbit(x); } } int main() { int t; scanf("%d",&t); while(t--){ memset(C,0,sizeof(C)); memset(a,0,sizeof(a)); memset(c,0,sizeof(c)); memset(d,0,sizeof(d)); int n; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); maxa=max(maxa,a[i]); } for(int i=1;i<=n;i++){ add(a[i],1); c[i]=sum(a[i]-1);//左边比a[i]小的人的个数 } memset(C,0,sizeof(C)); for(int i=n;i>=1;i--){ add(a[i],1); d[i]=sum(a[i]-1);//右边比a[i]小的人的个数 } long long ans=0; for(int i=1;i<=n;i++){ ans+=(long long)(c[i]*(n-i-d[i]))+(long long)(d[i]*(i-1-c[i])); } printf("%I64d\n",ans); } return 0; } /* 3 3 1 2 3 5 1 4 9 8 2 5 1 3 2 4 5 */
HDU 2492 pingpang
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。