HDU 2492 Ping pong (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492
Ping pong
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?
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
话不多说 直接上代码
解释都在代码中标注了
#include<stdio.h>
#include<string.h>
#define size 100100
int n,c[size],x1[size],x2[size],y1[size],y2[size],a[size]; int Lowbit(int k)
{
return (k&-k);
}
void update(int pos,int num)
{
while(pos<size)//重要 是size 而不是<=n
{
c[pos]+=num;
pos+=Lowbit(pos);
}
}
int sum(int pos)
{
int s=;
while(pos>)
{
s+=c[pos];
pos-=Lowbit(pos);
}
return s;
} int main()
{
int i,cas;
scanf("%d",&cas);
while(cas--)
{
memset(c,,sizeof(c));
scanf("%d",&n);
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
int k1;
k1=sum(a[i]);
x1[i]=k1; //输入的i个数中 有k1个比a[i]小
x2[i]=i--k1; //输入的i个数中 有k1个比a[i]大
update(a[i],);
}
memset(c,,sizeof(c));
int j=;//代表现在输入的数的个数
for(i=n;i>=;i--,j++)
{
int k1;
k1=sum(a[i]);
y1[i]=k1;//输入a[i]后输入的那些数中有多少个比a[i]小的
y2[i]=j--k1; //输入a[i]后输入的那些数中有多少个比a[i]大的
update(a[i],);
}
__int64 ans=;
for(i=;i<=n;i++)
{
// printf("x1[%d]=%d x2[%d]=%d y1[%d]=%d y2[%d]=%d\n",i,x1[i],i,x2[i],i,y1[i],i,y2[i]);
ans+=x1[i]*y2[i]+x2[i]*y1[i];
// ans+=x1[i]*x2[i];
}
printf("%I64d\n",ans);
} }
HDU 2492 Ping pong (树状数组)的更多相关文章
- Ping pong(树状数组经典)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- poj3928 Ping pong 树状数组
http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- UVA 1428 - Ping pong(树状数组)
UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...
- LA 4329 Ping pong 树状数组
对于我这样一名脑残ACMer选手,这道题看了好久好久大概4天,终于知道怎样把它和“树状数组”联系到一块了. 树状数组是什么意思呢?用十个字归纳它:心里有数组,手中有前缀. 为什么要用树状数组?假设你要 ...
- LA4329 Ping pong 树状数组
题意:一条大街上住着n个乒乓球爱好者,经常组织比赛切磋技术.每个人都有一个能力值a[i].每场比赛需要三个人:两名选手,一名裁判.他们有个奇怪的约定,裁判必须住在两名选手之间,而裁判的能力值也必须在两 ...
- UVALive - 4329 Ping pong 树状数组
这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...
- POJ 3928 Ping pong 树状数组模板题
開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...
- LA 4329 - Ping pong 树状数组(Fenwick树)
先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...
- hdu 6203 ping ping ping(LCA+树状数组)
hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...
随机推荐
- python class metaclass instance
>>> class CObj(object):... pass...>>> dir()['CObj', '__builtins__', '__doc__', '__ ...
- tp5 model 中的查询范围(scope)
查询范围scope在model中定义,在controller中使用 namespace app\index\model; use think\Model; class User extends Mod ...
- c/c++的Soap应用
1. 关于soap 在许多项目中团队中,我们常常会听到这样的话:我们这里是用webservice交互的.而说话的场景往往就是交互对象双方比较异构,所谓异构.即双方是不同的开发语言.不同的运行环境等.比 ...
- StringBuffer与StringBuilder的简单理解
联系:两者都适用于字符串的操作,都可以随便对字符串的内容进行变更操作,都继承至AbstractStringBuilder. 区别:StringBuffer是线程安全的,方法都加了synchronize ...
- WordPress一键部署网站
每个人心里都有一个建站梦,所以今天作为我第一篇文章,就给大家圆了这场梦. 今天我来详细的一步一步带领大家利用WordPress程序来建立自己的小站以及解决直接域名访问(本地安装wordpress请阅读 ...
- 高性能缓存系统Redis安装与使用
在互联网后台架构中,需要应付高并发访问数据库,很多时候都会在数据库上层增加一个缓存服务器来保存经常读写的数据以减少数据库压力,可以使用LVS.Memcached或Redis,Memcached和Red ...
- centos7下操作防火墙
引言 最近使用centos7系统比较频繁,在配置服务器的时候,总是遇到能够ping通服务器,但是就是没有办法访问80端口,这个时候我的直觉告诉我,肯定是防火墙的原因,但是使用iptables却怎么都找 ...
- 手把手教你玩转nginx负载均衡(五)----配置后端服务器组
引言 在前面几篇中,我们成功的搭建起了一台nginx服务器,所以我们要重复前面的步骤,把服务器的数量增加到3台以上,我这里已经建好了另外两台,分别是centos7-22,centos7-23,对应的i ...
- 通过JSch编写上传、下载文件
package com.hct.util; /** * @作者: HCT * @时间:2016年12月29日下午3:13:20 * @描述: * */ import java.io.*; import ...
- 使用Fiddler关于“由于目标计算机积极拒绝,无法连接。”的解决方案
今天使用Fiddler的时候遇到下面这个问题:在地址栏想打开个一般处理程序,出现连接本机失败的提示,如下图: 而这在我没打开Fiddler的时候是显示正常的. 查看Fiddler,在嗅探 -> ...