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和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...
随机推荐
- SharePoint加K2,将Portal系统与BPM系统完美整合!
K2 blackPearl与Microsoft Office SharePoint 一起为解决人员和流程相互合作的解决方案而提供一个强大的平台. K2“blackpearl”根据企业的需求提供了设计, ...
- 踏着前人的脚印学hadoop——ipc中的Server
1.An abstract IPC service. IPC calls take a single {@link Writable} as a parameter, and return a {@ ...
- iOS开发之App启动原理
iOS程序的启动过程 程序启动的完整过程大致步骤如下: 1.main函数 2.UIApplicationMain * 创建UIApplication对象 * 创建UIApplication的deleg ...
- 主线程中一定不能放耗时操作,必须要开子线程,比如下载文件,不然会不让你拿到输入流--报错显示android.os.NetworkOnMainThreadException
1.必须要开子线程来操作耗时操作,android.os.NetworkOnMainThreadException new Thread(new Runnable() { @Override publi ...
- PHP+MySql字符问题原理分析
假如数据库已经设置了utf-8 ,php文件也设置了utf-8 ,但在php文件的查询语句中未添加了 mysql_query("set names utf8")语句,此时php页面 ...
- 知道创宇研发技能表v3.0
知道创宇研发技能表v3.0 2015/8/21 发布 by @知道创宇(www.knownsec.com) @余弦 & 404团队 后续动态请关注微信公众号:Lazy-Thought 说明 关 ...
- 北大poj-1001
Description Problems involving the computation of exact values of very large magnitude and precision ...
- R.java不能自动更新
1. The type R is already defined. (很多时候我们在导入其他人的程序的时候,会遇到这个错误) 通常在project里有两个R.java,一个在src,一个在gen,通常 ...
- Osmocom-BB多信道修改代码相关
修改bb\src\target\firmware\layer1\prim_rx_nb.c 文件 这个nb表示normalburst,指的是ccch的数据,用专业的术语,应该是,一个ccch的burst ...
- ubuntu下的wps不能使用中文.
首先如果wps不能用中文的话应该是 excell ppt word 都不能用 . 我的办法需要改三个文件 . 先后打开这三个文件 . xpower@xpower-CW65S:~$ sudo vim / ...