主题链接:

PKU:http://poj.org/problem?id=3928

HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492

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


PS:
分别求每个数的左右两边比它大的个数和小的个数!最后再交叉相乘就可以!

代码例如以下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100017;
int n;
int a[maxn], c[maxn];
int leftMax[maxn], leftMin[maxn];
int rightMax[maxn], rightMin[maxn];
typedef __int64 LL; int Lowbit(int x) //2^k
{
return x&(-x);
} void update(int i, int x)//i点增量为x
{
while(i <= maxn)//注意此处
{
c[i] += x;
i += Lowbit(i);
}
}
int sum(int x)//区间求和 [1,x]
{
int sum=0;
while(x>0)
{
sum+=c[x];
x-=Lowbit(x);
}
return sum;
} int main()
{
int t;
int n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
}
memset(c,0,sizeof(c));
for(int i = 1; i <= n; i++)
{
leftMin[i] = sum(a[i]);//计算左边小的个数
leftMax[i] = i-leftMin[i]-1;//计算左边大的个数
update(a[i],1);
}
memset(c,0,sizeof(c));//再次清零
for(int i = n,j = 1; i >= 1; i--,j++)
{
rightMin[i] = sum(a[i]);
rightMax[i] = j-rightMin[i]-1;
update(a[i],1);
}
LL ans = 0;
for(int i = 1; i <= n; i++)
{
ans+=leftMax[i]*rightMin[i] + leftMin[i]*rightMax[i];//交叉相乘取和
}
printf("%I64d\n",ans);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ 3928 &amp; HDU 2492 Ping pong(树阵评价倒数)的更多相关文章

  1. HDU 2492 Ping pong (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...

  2. POJ 3928 &amp; hdu 2492 &amp; Uva1428 PingPong 【树状数组】

    Ping pong                                                   Time Limit: 2000/1000 MS (Java/Others)   ...

  3. HDU 2492 Ping pong(数学+树状数组)(2008 Asia Regional Beijing)

    Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street ...

  4. HDU 2492 Ping pong (数状数组)

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. poj3928 Ping pong 树状数组

    http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  6. Ping pong(树状数组经典)

    Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. UVA 1428 - Ping pong(树状数组)

    UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...

  8. POJ 3928 Ping pong 树状数组模板题

    開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...

  9. LA 4329 Ping pong 树状数组

    对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...

随机推荐

  1. Quartz(GUI)图形界面程序----Quartz Web

    下载.设置和运行Quartz(GUI)图形界面程序----Quartz Web 一.获取Quartz Web程序(Quartz GUI).早期的 Quartz 框架开发者意识到一个 GUI 对于某类用 ...

  2. Java String类的比较运算

    面试题:(多选)以下返回true的有() A. "beijing" == "beijing" B. "beijing".equals(new ...

  3. ZOJ 3795 Grouping(Tarjan收缩点+DAG)

    Suppose there are N people in ZJU, whose ages are unknown. We have some messages about them. The i-t ...

  4. mfc 链接时错误 文件函数重复定义

    我在HeaderFile里新建了一个函数,然后在程序里调用,一直出现这个错误,说这个函数重复定义, 发现是VS自动加到External dependencies里面了.把HeaderFile里的函数文 ...

  5. 关于.NET,.NET Framework 和ASP.NET的总结

    .NET 1.1.        .NET是 Microsoft XML Web services 平台和技术. 1.2.        一个.NET应用是一个运行于.NET Framework之上的 ...

  6. Centos根据系统VPS安装SendMail组件使WordPress支持E-mail

    1.在putty在链接: yum install sendmail 2.启动SendMail: service sendmail start 3.检查SendMail是否在监听默认的25port: n ...

  7. Xenomai 3 和 PREEMPT_RT 有哪些优势相比,

    Q: 我可以在我的开发板PREEMPT_RT直接在内核环境中执行POSIX应用, 使用Xenomai3 这是什么原因它? A:假设你的应用程序已经完全是POSIX,而且性能也满足,则,而且也没有理由去 ...

  8. Spring相框:AOP详细说明

    AOP中国的名字叫做面向方面编程.这个名字是很形象.因为你真的可以把像面包切系统.并直接增加面包的修改.科而异,对整个系统,小到一定的方法. AOP它有什么用?有关示例,各组分可以含有安全.事务.,A ...

  9. SGU 548 Dragons and Princesses

    意甲冠军: n个月格儿  所有的格龙或公主的儿子  从勇士1走n  不杀  杀死有钱拿  路过公主  假设之前杀龙的数量满足公主要求就会停止行走  问  勇士想多拿钱  可是必需要满足n格子的公主  ...

  10. (大数据工程师学习路径)第一步 Linux 基础入门----简单的文本处理

    介绍 这一节我们将介绍这几个命令tr(注意不是tar),col,join,paste.实际这一节是上一节关于能实现管道操作的命令的延续,所以我们依然将结合管道来熟悉这些命令的使用. 一.常用的文本处理 ...