POJ 3928 Ping pong(树状数组)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3139 | Accepted: 1157 |
Description
Input
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
Sample Input
1
3 1 2 3
Sample Output
1
【分析】没想到此题可以用树状数组做。首先用结构体记录输入数字的大小及下标,然后按数值从小到大排序。然后枚举中间数字,及a[i]<a[k]<a[j]中的a[k]。然后用排序后的数组依次计算,树状数组tree记录的是
到当前位置已经出现过的数的数目,那么到后面,这些数自然就比他小了,感觉很神奇。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e4+;
const int M = ;
const int mod=1e9+;
int tree[N],n,m;
struct man{
int pos,rank;
bool operator< (const man &it)const{
return rank<it.rank;
}
};
void add(int k,int num){
while(k<=n){
tree[k]+=num;
k+=k&(-k);
}
}
int Sum(int k){
int sum=;
while(k>){
sum+=tree[k];
k-=k&(-k);
}
return sum;
}
int main() {
int t;
scanf("%d",&t);
while(t--){
met(tree,);
man a[N];
ll ans=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i].rank);
a[i].pos=i;
}
sort(a+,a+n+);
add(a[].pos,);
for(int i=;i<n;++i){
int ls,lb,rs,rb;
ls=Sum(a[i].pos-);
lb=a[i].pos--ls;
rs=Sum(n)-Sum(a[i].pos);
rb=n-rs-a[i].pos;
ans+=ls*rb+lb*rs;
add(a[i].pos,);
}
printf("%lld\n",ans);
}
return ;
}
POJ 3928 Ping pong(树状数组)的更多相关文章
- POJ 3928 Ping pong 树状数组模板题
開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...
- poj3928 Ping pong 树状数组
http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- Ping pong(树状数组经典)
Ping pong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 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 树状数组
这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...
- HDU 2492 Ping pong (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...
- LA 4329 - Ping pong 树状数组(Fenwick树)
先放看题传送门 哭瞎了,交上去一直 Runtime error .以为那里错了. 狂改!!!!! 然后还是一直... 继续狂改!!!!... 一直.... 最后发现数组开小了.......... 果断 ...
- POJ 3928 Ping pong
题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...
随机推荐
- hashmap和hashtable,arraylist和vector的区别
hashmap线程不安全,hashtable线程安全 hashmap允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同. ...
- Xcode如何查看内存中的数据
在 debug 模式下如何在断点处,查看字符指针变量内存中的值,像vs2008的调试工具一样的内存查看器,现在只能查看第一个内存中的值可以在输出窗口采用gdb命令:x /nfu <addr&g ...
- poj1181 大数分解
//Accepted 164 KB 422 ms //类似poj2429 大数分解 #include <cstdio> #include <cstring> #include ...
- (转)深入浅出 iOS 之生命周期
原文:http://www.cocoachina.com/applenews/devnews/2011/0817/3129.html 深入浅出 iOS 之生命周期 发布于:2011-08-17 10: ...
- BW增强数据源的两种方法
BW增强数据源的两种方法 2009-04-01, by SAPBI 前言:我们经常会遇到系统标准的数据源,或者我们自建的数据源无法满足要求的情况,这个时候在数据源中添加几个相关的字段,可能就能满足我们 ...
- iOS interface guidelines (界面设计指南)<一>
一. 为iOS而设计 1.iOS体现的主题: (1)Deference(顺从):UI的存在就是为了让顾客更加容易理解和进行交互,而不是要和顾客玩智力游戏 (2)Clarity(清晰):在每个 ...
- php大力力 [012节]PHP连接mySQL数据库
php大力力 [012节]PHP连接mySQL数据库 1.用简单的php测试代码,而不是直接进入前端页面,越简单越好 2.在=号前后,不要写空格,万一写了中文空格,排除错误很麻烦. 3.我在mysql ...
- ELF Spec
ELF Spec Generic System V Application Binary Interface,ELF-64 Object File Format AMD64 System V ABI, ...
- iOS LaunchScreen启动图设置
新建的iOS 项目启动画面默认为LaunchScreen.xib 如果想实现一张图片作为启动页,如下图 如果启动不行 记得clear 一下工程 是启动页停留一段时间 只需要在 AppDelegat ...
- 【转】SQL SERVER日志满或过大的处理方法
原文转自:http://blog.chinaunix.net/uid-7953959-id-2543262.html 事务日志文件Transaction Log File是用来记录数据库更新情况的文件 ...